Run two winform windows simultaneously

前端 未结 3 1133
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 09:32

I have two C# winform (.NET 4.0) forms that each run separate but similar automated tasks continuously. Separate in that they are distinct processes/workflows, but similar

3条回答
  •  清酒与你
    2020-11-28 09:54

    If you really need two windows/forms to run on two separate UI threads, you could do something like this:

    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            var thread = new Thread(ThreadStart);
            // allow UI with ApartmentState.STA though [STAThread] above should give that to you
            thread.TrySetApartmentState(ApartmentState.STA); 
            thread.Start(); 
    
            Application.Run(new frmOne());
        }
    
        private static void ThreadStart()
        {
            Application.Run(new frmTwo()); // <-- other form started on its own UI thread
        }
    }
    

提交回复
热议问题