Right now I have C# code to spawn a new window in a different thread, this works, but as soon as the new spawned window opens, it closes and the thread ends. How would I make it so the new spawned window can be closed from the first thread?
Here is a "tree" of how the spawning currently works:
Main thread
--Uses a function in the main thread to start another function in a separate thread to open w window, causing the window to use that thread.
Basically I just want the two windows to each have their own thread. And be able to control the spawned secondary window from the first window thread.
I bet what you're doing is something like this:
new Thread(() => new TestForm().Show()).Start();
because this makes the window disappear immediately, like you describe.
Try this instead:
new Thread(() => new TestForm().ShowDialog()).Start();
ShowDialog spins its own message pump, and only returns when the window is closed.
This is just a quick example. It's a little more robust than the first one I wrote. It eliminates the existing race condition by using p/invoke.
class MainUIThreadForm : Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainUIThreadForm());
}
private IntPtr secondThreadFormHandle;
public MainUIThreadForm()
{
Text = "First UI";
Button button;
Controls.Add(button = new Button { Name = "Start", Text = "Start second UI thread", AutoSize = true, Location = new Point(10, 10) });
button.Click += (s, e) =>
{
if (secondThreadFormHandle == IntPtr.Zero)
{
Form form = new Form
{
Text = "Second UI",
Location = new Point(Right, Top),
StartPosition = FormStartPosition.Manual,
};
form.HandleCreated += SecondFormHandleCreated;
form.HandleDestroyed += SecondFormHandleDestroyed;
form.RunInNewThread(false);
}
};
Controls.Add(button = new Button { Name = "Stop", Text = "Stop second UI thread", AutoSize = true, Location = new Point(10, 40), Enabled = false });
button.Click += (s, e) =>
{
if (secondThreadFormHandle != IntPtr.Zero)
PostMessage(secondThreadFormHandle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
};
}
void EnableStopButton(bool enabled)
{
if (InvokeRequired)
BeginInvoke((Action)(() => EnableStopButton(enabled)));
else
{
Control stopButton = Controls["Stop"];
if (stopButton != null)
stopButton.Enabled = enabled;
}
}
void SecondFormHandleCreated(object sender, EventArgs e)
{
Control second = sender as Control;
secondThreadFormHandle = second.Handle;
second.HandleCreated -= SecondFormHandleCreated;
EnableStopButton(true);
}
void SecondFormHandleDestroyed(object sender, EventArgs e)
{
Control second = sender as Control;
secondThreadFormHandle = IntPtr.Zero;
second.HandleDestroyed -= SecondFormHandleDestroyed;
EnableStopButton(false);
}
const int WM_CLOSE = 0x0010;
[DllImport("User32.dll")]
extern static IntPtr PostMessage(IntPtr hWnd, int message, IntPtr wParam, IntPtr lParam);
}
internal static class FormExtensions
{
private static void ApplicationRunProc(object state)
{
Application.Run(state as Form);
}
public static void RunInNewThread(this Form form, bool isBackground)
{
if (form == null)
throw new ArgumentNullException("form");
if (form.IsHandleCreated)
throw new InvalidOperationException("Form is already running.");
Thread thread = new Thread(ApplicationRunProc);
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = isBackground;
thread.Start(form);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With