Displaying Modal and Modeless Windows Forms:
To display a form as a modeless dialog box Call the Show method:
The following example shows how to display an About dialog box in modeless format.
// C#
//Display frmAbout as a modeless dialog
Form f= new Form();
f.Show();
To display a form as a modal dialog box Call the ShowDialog method.
The following example shows how to display a dialog box modally.
// C#
//Display frmAbout as a modal dialog
Form frmAbout = new Form();
frmAbout.ShowDialog();
See: Displaying Modal and Modeless Windows Forms
See the following console application code:
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
class Test
{
[STAThread]
static void Main()
{
var f = new Form();
f.Text = "modeless ";
f.Show();
var f2 = new Form() { Text = "modal " };
Application.Run(f2);
Console.WriteLine("Bye");
}
}
you may use another thread, but you must wait for that thread to join or abort it:
like this working sample code:
using System;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication2
{
static class Test
{
[STAThread]
static void Main()
{
var f = new Form { Text = "Modeless Windows Forms" };
var t = new Thread(() => Application.Run(f));
t.Start();
// do some job here then press enter
Console.WriteLine("Press Enter to Exit");
var line = Console.ReadLine();
Console.WriteLine(line);
//say Hi
if (t.IsAlive) f.Invoke((Action)(() => f.Text = "Hi"));
if (!t.IsAlive) return;
Console.WriteLine("Close The Window");
// t.Abort();
t.Join();
}
}
}