C# 加载屏幕/线程问题

发布于 2024-12-09 14:39:22 字数 548 浏览 7 评论 0原文

在我的主表单加载之前,它会要求用户检查更新。当他们单击“确定”时,我将显示主窗体并创建一个包含一些标签和带有动画 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅浅 2024-12-16 14:39:22

问题是 Thread.Join() 将阻塞调用线程,直到您正在等待的线程完成。

相反,您应该对此类活动使用异步模型。在这里,BackgroundWorker 是理想的选择:

class MyForm
{
  private BackgroundWorker _backgroundWorker;

  public Myform()
  {
    _backgroundWorker = new BackgroundWorker();
    _backgroundWorker.DoWork += CheckVersion;
    _backgroundWorker.RunWorkerCompleted += CheckVersionCompleted;

    // Show animation
    // Start the background work
    _backgroundWorker.DoWork();
  }

  private void CheckVersion()
  {
    // do background work here
  }

  private CheckVersionCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
    // hide animation
    // do stuff that should happen when the background work is done
  }
}

这只是一个粗略的实现示例,但与我过去所做的许多示例类似。

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:

class MyForm
{
  private BackgroundWorker _backgroundWorker;

  public Myform()
  {
    _backgroundWorker = new BackgroundWorker();
    _backgroundWorker.DoWork += CheckVersion;
    _backgroundWorker.RunWorkerCompleted += CheckVersionCompleted;

    // Show animation
    // Start the background work
    _backgroundWorker.DoWork();
  }

  private void CheckVersion()
  {
    // do background work here
  }

  private CheckVersionCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
    // hide animation
    // do stuff that should happen when the background work is done
  }
}

This is just a rough example of an implementation, but similar to many I have done in the past.

漫雪独思 2024-12-16 14:39:22

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文