How can I invoke into the message loop of the tread/form after calling Application.Run()
without a dialog? The reason is that I want to prepare (and later show) a dialog that is clickable even if a modal dialog is shown in the main application.
static Form1 dialog;
private static void CreateDialog(object obj)
{
dialog = new Form1();
Application.Run();
}
static void Main(string[] args)
{
var thread = new Thread(CreateDialog);
thread.Start();
Thread.Sleep(2000); //only for demonstration
dialog.Invoke((Action)dialog.Show); //InvalidOperationException: need window handle first
}
I found the solution here: https://stackoverflow.com/a/4411493/1520078
I just have to access the Handle
property of the Form to be able to invoke it from now on.
private static void CreateDialog(object obj)
{
dialog = new Form1();
_ = dialog.Handle;
Application.Run();
}
Now this does not throw any more:
dialog.Invoke((Action)dialog.Show);