从另一个项目打开一个表单

时间:2014-04-08 21:25:45

标签: c# visual-studio-2010 c#-4.0

我有一个名为spreadsheetClient的项目,其中包含client.cs。我想从spreadsheetGUI中打开一个电子表格GUI。

SS.Program.Main(); // open up a spreadsheet GUI
Console.WriteLine("Spreadsheet is open.");

代码永远不会进入Console.Writeline。在client.cs中,当另一个项目的GUI同时运行时,如何处理其他内容?

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

            // Start an application context and run one form inside it
            DemoApplicationContext appContext = DemoApplicationContext.getAppContext();
            appContext.RunForm(new Form1());
            Application.Run(appContext);
        }
    }

1 个答案:

答案 0 :(得分:1)

Application.Run()不是异步的。根据MSDN文档,此方法&#34;开始在当前线程上运行标准应用程序消息循环。&#34;

因此,在应用程序运行完毕并终止之前,它不会返回。为了允许Console.WriteLine("Spreadsheet is open.")运行,您需要从单独的线程中调用Application.Run()

编辑:要创建并运行单独的线程,您需要执行以下操作:

像这样创建一个新的System.Threading.Thread

var thread = new System.Threading.Thread(delegate(){ 
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // Start an application context and run one form inside it
    DemoApplicationContext appContext = DemoApplicationContext.getAppContext();
    appContext.RunForm(new Form1());
    Application.Run(appContext);
});

委托是一个匿名函数,直接传递给线程。这告诉线程启动时要做什么。然后,您需要调用thread.Start();才能运行该线程。

相关问题