public MainUIThreadForm() { Text = "First UI"; Button button; Controls.Add(button = new Button { Text = "Start second UI thread", AutoSize = true, Location = new Point(10, 10) }); button.Click += (s, e) => { if (secondThreadForm == null || !secondThreadForm.IsHandleCreated) { secondThreadForm = SecondUIThreadForm.Create(); } }; Controls.Add(button = new Button { Text = "Stop second UI thread", AutoSize = true, Location = new Point(10, 40) }); button.Click += (s, e) => { if (secondThreadForm != null && secondThreadForm.IsHandleCreated) { secondThreadForm.Invoke((Action)(() => secondThreadForm.Close())); } }; }
public MainUIThreadForm() { Text = "First UI"; Button button; Controls.Add(button = new Button { Text = "Start second UI thread", AutoSize = true, Location = new Point(10, 10) }); button.Click += (s, e) => { if (secondThreadFormHandle == IntPtr.Zero) { Form second = SecondUIThreadForm.Create(); second.HandleCreated += SecondFormHandleCreated; second.HandleDestroyed += SecondFormHandleDestroyed; } }; Controls.Add(button = new Button { Text = "Stop second UI thread", AutoSize = true, Location = new Point(10, 40) }); button.Click += (s, e) => { if (secondThreadFormHandle != IntPtr.Zero) { PostMessage(secondThreadFormHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } }; }
public static SecondUIThreadForm Create() { SecondUIThreadForm form = new SecondUIThreadForm(); thread = new Thread(Main2); thread.SetApartmentState(ApartmentState.STA); thread.Start(form); return(form); }