C#.net - 如何提醒程序线程已完成(事件驱动)? [英] C#.net - How to alert program that the thread is finished (event driven)?

查看:193
本文介绍了C#.net - 如何提醒程序线程已完成(事件驱动)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我课上的一个片段:

  public bool start()
{
Thread startThread = new Thread(this.ThreadDealer);
startThread.Start();
return _start;
}

在ThreadDealer()中我将布尔变量_start设置为false或真实。我现在所需要的但似乎无法想象的是一个事件,当ThreadDealer() - 线程完成时,start()执行返回语句。



我尝试过一些AutoResetEvent和.WaitOne(),但是由于我有一个GUI,只是阻止所有的东西,而它做我需要做的(等待线程完成)它是无用的,如果它阻止我的GUI。 p>

任何帮助将不胜感激。

解决方案

你想做什么 - 等待UI线程的后台线程,但仍然允许UI响应 - 不可能。您需要将代码分为两部分:一个在后台线程启动(或并行)之前执行,另一个在后台线程完成后运行。



最简单的方法是使用 BackgroundWorker类。它在工作完成后,在UI线程( RunWorkerCompleted )中引发事件。这里有一个例子:

  public void start()
{
var bw = new BackgroundWorker();

//定义事件处理程序
bw.DoWork + =(sender,args)=> {
//在这里做你漫长的东西 - 这将发生在一个单独的线程
...
};
bw.RunWorkerCompleted + =(sender,args)=> {
if(args.Error!= null)//如果在DoWork中发生异常,
MessageBox.Show(args.Error.ToString()); //在此处执行错误处理

//完成任务后,再做任何你想做的事情。
//这是在主UI线程中发生的。
...
};

bw.RunWorkerAsync(); //启动后台工作人员

//执行继续与后台工作并行执行
}


this is a snippet from my class:

public bool start()
{
   Thread startThread = new Thread(this.ThreadDealer);
   startThread.Start();
   return _start;
}

In ThreadDealer() I'm setting the boolean variable "_start" to false or true. What I need now but can't seem to figure out is an event to alert start() to execute its return statement when the ThreadDealer()-Thread has finished.

I tried something with an AutoResetEvent and .WaitOne() but since I have a GUI that just blocks everything and while it does what I need it to do (wait for the Thread to finish) it is useless if it blocks my GUI.

Any help would be much appreciated.

解决方案

What you want to do -- wait for the background thread in a method of the UI thread but still allow the UI to be responsive -- is not possible. You need to split your code into two parts: One executed before starting (or parallel to) the background thread and the other one running after the background thread has finished.

The easiest way is to use the BackgroundWorker class. It raises an event in the UI thread (RunWorkerCompleted) after its work is done. Here's an example:

public void start()
{
    var bw = new BackgroundWorker();

    // define the event handlers
    bw.DoWork += (sender, args) => {
        // do your lengthy stuff here -- this will happen in a separate thread
        ...
    };
    bw.RunWorkerCompleted += (sender, args) => {
        if (args.Error != null)  // if an exception occurred during DoWork,
            MessageBox.Show(args.Error.ToString());  // do your error handling here

        // Do whatever else you want to do after the work completed.
        // This happens in the main UI thread.
        ...
    };

    bw.RunWorkerAsync(); // starts the background worker

    // execution continues here in parallel to the background worker
}

这篇关于C#.net - 如何提醒程序线程已完成(事件驱动)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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