从单独的线程更新我的UI上的进度栏 [英] Update a Progress Bar on my UI from a separate thread

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

问题描述

我编写了一个正在运行线程的程序,以防止执行任务时锁定Form1.我已经在form1上创建了一个进度条,但是在弄清楚如何从线程中更新进度条时遇到了很多麻烦,该线程在一个单独的类中运行.

有人知道一个很好的参考吗?

I have written a program that is running a thread to prevent lockup of my form1 while performing the tasks. I have created a progress bar on my form1 but I am having lots of trouble figuring out how to update that progress bar from my thread, which runs in a separate class.

Does anyone know a good reference?

推荐答案

为该类创建一个事件,并使用线程引发该事件.

该事件将发回progressvalue.因此,在处理程序内部,您需要重置进度值.

确保从UI线程调用进度值.

多线程进度条控件 [
Create an Event for the class and use Thread to raise the event.

The event will send back the progressvalue. So inside the handler you need to reset the progress value.

Make sure you invoke the progress value from UI Thread.

Multiple Thread Progress Bar Control[^]


Abhishek的答案正确.

您也可以使用BackgroundWorker.它的DoWork事件在单独的线程上引发,并且ReportProgress方法自动在UI线程上引发ProgressChanged事件.

示例
此处 [
Abhishek''s answer is correct.

You could also use a BackgroundWorker. It''s DoWork event is raised on a seperate thread and the ReportProgress method automatically raises the ProgressChanged event on the UI thread.

Example here[^].


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

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