C# 3.0 如何在GUI线程c中从后台线程获取返回值#
这里我们有一个称为t的线程,它是背景线程。其中我调用了Start方法,该方法返回的值如上面的代码所示 我必须在下面代码中的GUI线程中使用这个结果C# 3.0 如何在GUI线程c中从后台线程获取返回值#,c#-3.0,c#-2.0,C# 3.0,C# 2.0,这里我们有一个称为t的线程,它是背景线程。其中我调用了Start方法,该方法返回的值如上面的代码所示 我必须在下面代码中的GUI线程中使用这个结果 try { Thread t = new Thread(() => { try { result = myclass.Start(mode, Check, Tasks,exeMode); } catch (GeneralException g
try
{
Thread t = new Thread(() =>
{
try
{
result = myclass.Start(mode, Check, Tasks,exeMode);
}
catch (GeneralException ge)
{
}
finally
{
if (Master)
{
}
}
});
t.IsBackground = true;
t.Start();
bResult = true;
}
catch(GeneralException ge)
{
}
mode = Mode.Continue;
if (ErrorCode != 0)
{
this.Invoke(new System.EventHandler(_showErrorMsg), execErrorCode, EventArgs.Empty);
}
else if (result == Result.RegainRequest)
{
this.Invoke(new System.EventHandler(_showMsg), Modes.RegainRequest, new EventArgs());
}
else if (result == Result.RegainRequestNoClear)
{
this.Invoke(new System.EventHandler(_showMsg), Modes.RegainRequestNoClear, new EventArgs());
}
else if (result == Result.PathRemain)
{
this.Invoke(new System.EventHandler(_showMsg), Modes.PathRemain, new EventArgs());
}
else
{
_regainmode = RegainMode.Continue;
}
请告诉我如何执行此操作,以便在GUI线程中使用此值。不要使用手动线程。相反,请使用BackgroundWorker()控件,该控件将允许您在DoWork()处理程序中设置
e.Result
。结果可以在RunWorkerCompleted()事件中获得,在该事件中更新GUI是安全的,因此也无需手动使用Invoke()。我有两种情况:1)在第一种情况下,我不必等待start方法的完成,它应该只运行2)在第二种情况下,我必须等待start方法的完成,并在GUI线程中使用它的返回值。这就是为什么我在后台线程上运行start方法。任何帮助都将不胜感激。我正在为智能设备使用Compact framework 3.0。在这种情况下,请将您的线程包装在一个引发自定义事件的自定义类中。问题现在已解决。我已经在方法作用域上声明了线程,并将结果变量声明为静态变量。只要检查结果并将后台线程连接到UI线程,就可以了。
else if (result == Result.RegainRequest)
{
this.Invoke(new System.EventHandler(_showMsg), Modes.RegainRequest, new EventArgs());
}