0

在我的 c# windows 应用程序中,我想在线程检查的条件下显示另一个表单。并且那个(第二个)线程已经被另一个(第一个)线程调用了。这是我用来更好地解释的代码:

主要方法:

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

Form1 方法:

private void Form1_Load(object sender, EventArgs e)
{
        // Call First thread to start background jobs.
        var thread = new Thread(ThreadFirst);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

        // Continue my load event stuff here...

}
    private void ThreadFirst()
    {
        // Do some background operations..
        // Call second thread to switch to another background process.
        var thread = new Thread(ThreadSecond);
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
}

private void ThreadSecond()
{
        If (condition)
                // navigate to another form and close running one..
                ShowAnotherForm();
        else
        {
                // Continue working on current form.
        }
}

[STAThread]
private void ShowAnotherForm()
{
     try
     {
           // Object for new form.
           globalForm = new myForm();
           globalForm.Show();
           // Close the current form running..
           this.Close();
           this.ShowInTaskbar = false;
           Application.Run();           
     }
     catch (Exception ex)
     {
           messagebox.Show(ex.message);
     }
}

当我从我的解决方案中运行它时,它工作得很好。但是,当我为此创建一个 msi 包时,两种形式都被隐藏了。我是否缺少要添加的内容,以便它也可以从设置中正常工作?

谢谢。

4

1 回答 1

4

您不应该在“非 GUI”线程上调用“GUI Stuff”。您所有的 GUI 调用都应该在 GUI 线程上。使用InvokeRequiredandInvoke确保您的所有 GUI 代码都在正确的线程上完成。谷歌会给你很多例子。

于 2013-08-24T11:37:21.070 回答