从另一个线程和更新类在C#GUI进度条

编程入门 行业动态 更新时间:2024-10-11 15:13:20
本文介绍了从另一个线程和更新类在C#GUI进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复:结果的从另一个线程

在我的计划,我想非GUI功能分离到另一个类,并留在主类相关的GUI的东西。不过,我有更新进度条,而在工人阶级中的工人的方法之一是做了工作的问题。我知道,我将与多线程在这里工作,但我不明白怎么。我可能只是缺少简单的事情,但是当我寻找关于它的信息,似乎大多数的教程谈细节,但不解释大局很好。我部分地理解调用和委托命令,但我真的不明白他们是如何进行互动。

下面是剥离下来的是我想要做的版本。我如何修改这个来更新进度条,但保留窗口响应和重新粉刷

主窗体类:

公共部分Form1类:表格 { time_waster的事情=新time_waster(); 公共Form1中() {的InitializeComponent(); progressBar1.Minimum = 0; progressBar1.Maximum = 100; } 私人无效的button1_Click(对象发件人,EventArgs五) { thing.something_that_takes_a_while(); } }

单独的工作类:类time_waster {公共time_waster() {}

公共无效something_that_takes_a_while( ) { INT延迟= 200; 的for(int i = 0; I< 100;我++) { Thread.sleep代码(延迟); //适当的代码来更新循环的每次迭代进度条。 } } }

解决方案

.NET包含一个名为 BackgroundWorker的,它提供了用于报告后台线程中的事件的进展的方法。该事件被自动调用它创造了BackgroundWorker的(通常是UI线程)的线程上。

订阅了ProgressChanged事件,并更新进度条事件处理程序。官方的MSDN文档提供了一些示例代码。

Possible Duplicate: Updating a Progress Bar from Another Thread

In my program, I wanted to separate non-GUI functions to another class, and leave things related to the GUI in the main class. However, I am having issues with updating a progress bar while one of the worker methods in the worker class is doing its job. I know that I will have to work with multithreading here, but I do not understand how. I may just be missing simple things, but when I look for information about it, it seems that most tutorials talk about the minutia, but do not explain the big picture very well. I partially understand what invoke and delegate commands are, but I don't really understand how they interact.

Below is stripped down version of what I want to do. How do I modify this to update the progress bar, but keep the window responsive and repainting?

Main form class:

public partial class Form1 : Form { time_waster thing = new time_waster(); public Form1() { InitializeComponent(); progressBar1.Minimum = 0; progressBar1.Maximum = 100; } private void button1_Click(object sender, EventArgs e) { thing.something_that_takes_a_while(); } }

Separate worker class: class time_waster { public time_waster() { }

public void something_that_takes_a_while() { int delay = 200; for (int i = 0; i < 100; i++) { Thread.Sleep(delay); //appropriate code to update the progress bar for each iteration of the for loop. } } }

解决方案

.NET Includes a class called the BackgroundWorker, which provides methods for reporting the progress of the background thread in an event. The event is automatically called on the thread which created the BackgroundWorker (typically, the UI thread).

Subscribe to that "ProgressChanged" event, and update the progress bar in that event handler. The official MSDN Documentation provides some sample code.

更多推荐

从另一个线程和更新类在C#GUI进度条

本文发布于:2023-11-12 11:09:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581326.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   进度条   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!