UI进度的更新过程中卡住 [英] UI stuck during update of ProgressBar

查看:768
本文介绍了UI进度的更新过程中卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的DLL Pcapdot.net为了发送数据包缓冲到我的网络适配器。我检查了我的Wireshark的文件,并收集到的所有数据包,持续时间等。
我的主类发送缓冲区,有几个属性(数据包数量,持续时间..)的信息,例如号码在主线程我检查这个与BackgroundWorker.ProgressChanged类:

  bgWoSingle =新的BackgroundWorker(); 
bgWoSingle.WorkerReportsProgress = TRUE;
bgWoSingle.ProgressChanged + =新ProgressChangedEventHandler(bgW_ProgressChanged);



Wireshark的文件中的每个数据包都有一个timstamp,当然我可以送其他速率此缓冲区增加或减少的数据包之间的时间戳记。
现在我的问题是:



一个我检查的属性是我有多少包已经发出。因为我知道我的文件多少数据包中包含的,我可以显示通过进度条。如果我删除的数据包打所有延迟改变发送速率最大,速度是如此之快,进度栏和所有的UI卡住,直到它完成发送的所有数据包。我该如何改变呢? ?也许通过更新另一个线程我的进度



这是我的函数谁检查我的类:

 无效bgW_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
//唧唧歪歪(检查所有的类属性)

PCAP = e.UserState为PCAP ;
progressBar1.Value = e.ProgressPercentage; //这里是我的进度更新


//唧唧歪歪(检查所有的类属性)
}


解决方案

我不知道Pcapdot.net,但我知道这样的WinForms我可以给你一对夫妇的提示。



看来你的UI是的停留的在这个意义上,窗口没有响应(不能点击活动的按钮或编辑文本框,如果你尝试窗口变为灰色)。在这种情况下,你有一个线程的问题。我不知道这是否的BackgroundWorker 是一个库类或一个你做,但你的必须,确保所有的I / O操作在另一个线程中完成的。虽然顾名思义它发生,我不知道你是如何开始对什么是由API假设的过程。例如,某些库允许你叫启动派生另一个线程,而有些则需要你打电话方法的执行从你已经分叉线程的方法。这使得差别。



为了从您必须始终使用非UI线程更新UI [Control.Invoke] [1] 方法(如果你需要暂停线程,直到UI被更新)或 Control.BeginInvoke (如果你想尽快进行)。



我的 LAMBDA福是不是每天的训练,所以请纠正我,如果我做了错误在我的语法

  progressBar1.Invoke(委托(){progressBar1.Value = e.ProgressPercentage;}); 



<击>在第二种情况中,你的UI是的停留的中进度条就不会进步意义,首先确保该事件被触发。第二个也是最重要的,您不能更新从一个非UI线程UI。势在必行!



如果您运行 progressChanged 一个try-catch我看到的烦恼,因为当你更新里面的方法进度百分比你可能被掩盖一个例外。我也希望 ProcessPercentage 0和100,而不是一个双击 0和1,东西之间,导致之间的整数酒吧没有明显进步。


I am using Pcapdot.net DLLs in order to send buffer of packets to my network adapter. I checked my Wireshark file and collected all the information e.g number of packets, duration etc.. My main class sends the buffer and has several properties (number of packets, duration..) In the main thread I am checking this class with BackgroundWorker.ProgressChanged:

            bgWoSingle = new BackgroundWorker();
            bgWoSingle.WorkerReportsProgress = true;
bgWoSingle.ProgressChanged += new ProgressChangedEventHandler(bgW_ProgressChanged);

Every packet inside Wireshark file has a timstamp and of course I can send this buffer in other speed rate by increasing or decreasing the timestamp between the packets. now my problem is:

One of the properties I am checking is how many packets I have already sent. Because I know how many packets my file contains, I can show the progress via progress bar. If I am changing the send rate to maximum by deleting all the delay between the packets play, the speed is so fast and the progress bar and all the UI stuck until it's finished sending all the packets. How can I change it? Maybe update my ProgressBar via another thread?

this is my function who check my class:

  void bgW_ProgressChanged(object sender, ProgressChangedEventArgs e)
  {
    //bla bla bla (check all Class properties)

        pcap = e.UserState as Pcap;
        progressBar1.Value = e.ProgressPercentage; //here is my progressBar update


    //bla bla bla (check all Class properties)
  }

解决方案

I don't know Pcapdot.net, but I know Winforms so I can give you a couple of hints.

It appears that your UI is stuck in the sense that the window is unresponsive (you cannot click active buttons or edit textboxes, and if you try the window becomes gray). In that case you do have a threading problem. I don't know if that BackgroundWorker is a library class or one that you made, but you must be sure that any I/O operation is done in another thread. While the name suggests that it happens, I'm not sure about how you start the process against what is supposed by the API. For example, certain libraries allow you to call a start method that forks another thread, while others require you to call an execute method from a thread you already forked. This makes the difference.

In order to update your UI from a non-UI thread you must always use [Control.Invoke][1] method (if you need to pause your thread until UI gets updated) or Control.BeginInvoke (if you want to proceed ASAP).

My Lambda-fu is not trained daily, so please correct me if I make an error in my syntax

progressBar1.Invoke(delegate() {progressBar1.Value = e.ProgressPercentage;});

In the second case in which your UI is stuck in the sense that the progress bar won't progress, first make sure that the event is fired. Second and most important, you cannot update UI from a non-UI thread. Imperative!

If you run that progressChanged method inside a try-catch I see troubles, because when you update the progressBar percentage you get an exception that could be masked. I also hope that ProcessPercentage is an integer between 0 and 100 and not a double between 0 and 1, something that causes the bar not to apparently progress.

这篇关于UI进度的更新过程中卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆