使用后台工作者运行windows窗体

本文关键字:windows 窗体 运行 工作者 后台 | 更新日期: 2023-09-27 18:05:50

我在后台工作器下运行windows窗体时遇到问题。

private void workerDecrypter_DoWork(object sender, DoWorkEventArgs e)
{
    route =(string) e.Argument;
    try
    {
        Application.Run(new Mono(route));
    }
    catch (Exception era) { }
}

Mono是windows窗体类,它创建一个新窗口,我在其中解密一些文本,当它完成处理后,它自动关闭窗口。如果没有try catch,我得到这个异常

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception has been thrown by the target of an invocation.

使用后台工作者运行windows窗体

您应该在主线程上调用Application.Run,而不是在另一个线程中。这将导致TIA。

作为旁注:如果这不是主表单,则不需要以这种方式打开该表单。你可以很容易地这样做:

Mono m = new Mono(route);
m.Show();

如果它不是一个形式,它应该是ApplicationContext的一个实例,我不认为看到你的代码。