运行在一个单独的线程Windows窗体 [英] Running a windows form in a separate thread

查看:101
本文介绍了运行在一个单独的线程Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理的运行控制的形式,但形式本身是没有价值的我。我基本上希望表单运行任务,并返回一个值,但是对于我想使用类似的的AutoResetEvent从函数调用返回时,才完成,这显然会阻塞线程的形式,使之不可能为任务完成。

I am dealing with running a control in a form, however the form itself is of no value to me. I essentially want the form to run a task and return a value, however for that I'd like to use something like an AutoResetEvent to return from the function call only when it has completed, which obviously would block the forms thread and make it impossible for the task to complete.

推荐答案

我觉得最简单的解决方法就是从形式上触发事件,一旦任务完成。

I think the simplest solution is to just raise an event from the form once the task has completed.

void RunTask()
{
    Form form = new Form();
    form.TaskCompleted += new EventHandler(form_TaskCompleted);
    form.Show();
}

void form_TaskCompleted(object sender, EventArgs e)
{
    object result = ((Form)sender).GetResult();
}

编辑:当然,你要处理的形式和解开这种情况下,一旦它的完成等。

Of course you'd want to dispose the form and unhook that event once it's completed etc..

这篇关于运行在一个单独的线程Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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