在任务栏中显示进度条我做错了什么?

发布于 2024-12-28 08:03:27 字数 637 浏览 4 评论 0原文

在我上一篇文章之后,我试图完成这项工作

,我有以下代码(已编辑):

 public partial class Form1 : Form
 {
   ...
   private TaskDialog Taskbar = new TaskDialog();
 }

 private void timer1_Tick(object sender, EventArgs e)
    {
        Taskbar.ProgressBar.Value = progressBar1.Value;
    }

 private void button2_Click(object sender, EventArgs e)
    {
      bgWorker.RunWorkerAsync();
    }

 bgWorker_dowork()
 {
   timer1.Enabled = true;
      timer1.Start();

      while(progressBar1.Value < progressBar1.Maximum)
      {
        progressBar1.Value++;
      } 
  }

我做错了什么?

编辑:我无法让它在任务栏上最小化时显示进度条,尽管我做了建议的更改。

Following my previous post iam trying to make this work

I have the following code (Edited):

 public partial class Form1 : Form
 {
   ...
   private TaskDialog Taskbar = new TaskDialog();
 }

 private void timer1_Tick(object sender, EventArgs e)
    {
        Taskbar.ProgressBar.Value = progressBar1.Value;
    }

 private void button2_Click(object sender, EventArgs e)
    {
      bgWorker.RunWorkerAsync();
    }

 bgWorker_dowork()
 {
   timer1.Enabled = true;
      timer1.Start();

      while(progressBar1.Value < progressBar1.Maximum)
      {
        progressBar1.Value++;
      } 
  }

What am i doing wrong?

Edit: i cant make it show a progressbar while minimized on the taskbar, althought i did the changes suggested..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

心的位置 2025-01-04 08:03:27

那是因为您阻塞了 GUI 线程,

  while(true)
  {
    progressBar1.Value++;
  } 

这将导致它锁定并阻止它更新 UI。

永远不要在主线程上运行这样的循环,使用 BackGroundWorker 来处理类似的事情。

That's because you're blocking your GUI thread

  while(true)
  {
    progressBar1.Value++;
  } 

This will cause it to lock and prevent it from updating the UI.

Never run such loops on your main thread, use a BackGroundWorker for stuff like that.

凑诗 2025-01-04 08:03:27

确保进度条的值不应超过最大值。

ProgressBar.Maximum 属性

ProgressBar.Minimum属性

 while (progressBar1.Value < progressBar1.Maximum) 
 { 
       progressBar1.Value++; 
       Application.DoEvents();//This will update the UI paintings 
                              //which is not happening prior to this code.     
 }

Make sure the value of the Progress Bar should not exceed the Maximum Value.

ProgressBar.Maximum Property

ProgressBar.Minimum Property

 while (progressBar1.Value < progressBar1.Maximum) 
 { 
       progressBar1.Value++; 
       Application.DoEvents();//This will update the UI paintings 
                              //which is not happening prior to this code.     
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文