我一直在寻找一种方法来创建一个很好的进度条.我找到了一些我用过的代码,如果我使用了循环,它的效果很好.但现在我想在一个真正体面的应用程序中使用它,它给了我很多麻烦.我有一个应用程序,我在IMDB上查找数据,所以我连接到IMDB例如500 movietitles,所以这需要一段时间.所以我想展示一个进度条,其中每个电影的条形图都会增长,并且在movietitle上有一些额外的信息.
我使用以下类:
public partial class ProgressDialog : Window, IProgressContext
{
public ProgressDialog()
{
InitializeComponent();
IconBitmapDecoder ibd = new IconBitmapDecoder(
new Uri("IMDB.ML.ico", UriKind.RelativeOrAbsolute),
BitmapCreateOptions.None, BitmapCacheOption.Default);
this.Icon = ibd.Frames[0];
}
public void UpdateProgress(double progress)
{
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { Progress.SetValue(ProgressBar.ValueProperty, progress); }, null);
}
public void UpdateStatus(string status)
{
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { StatusText.SetValue(TextBlock.TextProperty, status); }, null);
}
public void Finish()
{
Dispatcher.BeginInvoke(DispatcherPriority.Background,
(SendOrPostCallback)delegate { Close(); }, null);
}
}
public interface IProgressContext
{
void UpdateProgress(double progress);
void UpdateStatus(string status);
void Finish();
} …Run Code Online (Sandbox Code Playgroud) 我对进度条显示值有疑问.
我有这个主线程
private void button1_Click(object sender, EventArgs e)
{
progress prog = new progress();
progress.progressEvent += new progress.progressEventHandler(progressEvent);
for(int i=0;i<100;i++)
{
Thread.Sleep(100);
prog.incA();
}
}
void progressEvent(object sender)
{
if (progressBar1.InvokeRequired)
{
//Tady mi to caka az kym nedobehne cyklus for a pak zacne tohleto fungovat
progressBar1.Invoke(new ChangeProgressBarValue(ProgressStep));
}
else
{
ProgressStep();
}
}
public void ProgressStep()
{
progressBar1.PerformStep();
}
public class progress
{
private ThreadStart ts;
private Thread th;
private bool status = true;
public delegate void progressEventHandler(object sender); …
Run Code Online (Sandbox Code Playgroud)