Run two winform windows simultaneously

前端 未结 3 1115
爱一瞬间的悲伤
爱一瞬间的悲伤 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
        }
    }
    
    0 讨论(0)
  • 2020-11-28 10:13

    Assumption

    You do not need the two different processes, you are only using the 2 processes because you want to have the two different forms and want to be able to keep the application running until both forms are exited.

    Another solution

    Rely on the Form.Closed event mechanism. You can add an eventhandler which allows you to specify what to do when a form closes. E.g. exit the application when both forms are closed.

    In terms of some code

        public Form1()
        {
            InitializeComponent();
    
            _form2 = new Form2();
            _form2.Show(this);
    
            this.Closed += Form1Closed;
            _form2.Closed += Form2Closed;
        }
    
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
            Form1Closed(this, new EventArgs());
            base.OnFormClosing(e);
        }
    
        private void Form1Closed(object sender, EventArgs eventArgs)
        {
            form1IsClosed = true;
    
            TryExitApplication();
        }
    
        private void Form2Closed(object sender, EventArgs eventArgs)
        {
            _form2IsClosed = true;
    
            TryExitApplication();
        }
    
        private void TryExitApplication()
        {
            if (form1IsClosed && _form2IsClosed)
            {
                Dispose();
                Application.Exit();
            }
        }
    

    Note that this should be refactored to make it a better solution.


    UPDATE

    The comments provided by Servy made my revise this "supposed to be simple solution", which pointed out that his solution is way better then this solution. Since I am supported to leave the answer I will use this answer I will also address the issues that start arising when going for this solution:

    • cancelling close events
    • rerouting from one event to another
    • force calling Dispose.
    • as Servy pointed out: maintenance unfriendly (state to check which form is closed)
    0 讨论(0)
  • 2020-11-28 10:17

    You can create a new ApplicationContext to represent multiple forms:

    public class MultiFormContext : ApplicationContext
    {
        private int openForms;
        public MultiFormContext(params Form[] forms)
        {
            openForms = forms.Length;
    
            foreach (var form in forms)
            {
                form.FormClosed += (s, args) =>
                {
                    //When we have closed the last of the "starting" forms, 
                    //end the program.
                    if (Interlocked.Decrement(ref openForms) == 0)
                        ExitThread();
                };
    
                form.Show();
            }
        }
    }
    

    Using that you can now write:

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MultiFormContext(new Form1(), new Form2()));
    
    0 讨论(0)
提交回复
热议问题