c# - Pass information to BackgroundWorker From UI during execution

孤人 提交于 2019-12-05 12:45:36

You have to pass the TrackBar object to the BackgroundWorker, not delay. delay doesn't change once you set it.

To simplify the needed Invoke(), you can use a helper method, such as this one:

Async.UI(delegate { textBox1.Text = "This is way easier!"; }, textBox1, true);

I will assume that you are already familiarized with cross-thread invocation to update the UI. So, the solution is very simple: in your worker thread, after each iteration, invoke the UI to get the slider thumb position.

Cheeso

To use a backgroundworker, you add a method to the DoWork property, like this:

this.backgroundWorker1.WorkerSupportsCancellation = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);

In the DoWork method, you need to check the variable where the updated delay is set.

This could be an integer field that is available on the containing Form or UI control, or it could be the TrackBar itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!