C# 加载屏幕/线程问题
在我的主表单加载之前,它会要求用户检查更新。当他们单击“确定”时,我将显示主窗体并创建一个包含一些标签和带有动画 gif 的图片框的面板。
动画 gif 没有移动,这通常是因为主线程很忙,但我已经线程化该线程来完成工作,但没有运气让动画播放。
这是我所拥有的。
Thread CheckVersion = new Thread(new ThreadStart(VersionCheck));
this.Show(); //bring up the main form
this.BringToFront();
pCheckingVersions.Visible = true; //this contains the animated gif
Application.DoEvents(); //refresh ui so my box
CheckVersion.Start(); //start thread
CheckVersion.Join(); //wait for thread to exit before moving on
pDownloading.Visible = false;
Before my main form loads it asks the user to check for updates. When they click ok i make the main form show and make a panel that contains some labels and a picture box with an animated gif.
The animated gif is not moving which normally is because the main thread is busy but I have threaded the thread doing the work and no luck getting the animation to play.
Here is what I have.
Thread CheckVersion = new Thread(new ThreadStart(VersionCheck));
this.Show(); //bring up the main form
this.BringToFront();
pCheckingVersions.Visible = true; //this contains the animated gif
Application.DoEvents(); //refresh ui so my box
CheckVersion.Start(); //start thread
CheckVersion.Join(); //wait for thread to exit before moving on
pDownloading.Visible = false;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 Thread.Join() 将阻塞调用线程,直到您正在等待的线程完成。
相反,您应该对此类活动使用异步模型。在这里,BackgroundWorker 是理想的选择:
这只是一个粗略的实现示例,但与我过去所做的许多示例类似。
The problem is that Thread.Join() is going to block the calling thread until the thread you are waiting on completes.
Instead you should use an asynchronous model for this kind of activity. A BackgroundWorker would be ideal here:
This is just a rough example of an implementation, but similar to many I have done in the past.
CheckVersion.Join() 调用使您的 UI 线程等待 CheckVersion 线程完成,这会阻塞。这使得 GIF 动画暂停。
尝试使用 BackgroundWorker 类,并使用
RunWorkerCompleted
事件向 UI 线程发出后台操作已完成的信号。The CheckVersion.Join() call is making your UI thread wait for the CheckVersion thread to complete, which blocks. That makes the GIF animation pause.
Try using the BackgroundWorker class, and use the
RunWorkerCompleted
event to signal to your UI thread that the background operation is done.