Run two winform windows simultaneously
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()));
Having two active windows in a Winform application
Add a form to your project (let's call it Form2
). Somewhere within your code (maybe in a button click event) use the following code:
Form2 f = new Form2();
f.Show();
The Show
method allows you to interact with the originating form, whereas ShowDialog
prevents interaction from the original form.
Display multiple forms when C# application starts
Start the other forms from the Form.Load
event of Form1
.
private void Form1_Load(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
How to run multiple instance of WinForms application
Assuming this is related to Task Scheduler and not your application specifically:
You can configure the behaviour of the Task Scheduler task when the target process is already running. In the UI, this is on the Settings
tab, "If the task is already running...". The default is "Do not start a new instance"; what you want is probably "Run a new instance in parallel", though a better option would probably be to have a different executable for handling the background tasks, and queuing the consecutive runs instead.
Running two tasks simultaneously
Elaborating on @BojanB's answer, you can use the traditional WinForms InvokeRequired
/Invoke
pattern. The InvokeRequired
property returns true if it's evaluated on the non-UI thread. If an Invoke
call is required, then you just Invoke
the same call (marshaling the call onto the UI thread) and it will complete. If you do it this way, then the same function can be called from either the UI thread or any other thread.
First I created these three helper functions:
private void SetTextBoxFromThread(TextBox textBox, string contents)
{
if (InvokeRequired)
{
Invoke(new Action<TextBox, string>(SetTextBoxFromThread), new object[] { textBox, contents });
return;
}
textBox.Text = contents;
}
private string ReadTextBoxFromThread(TextBox textBox)
{
if (InvokeRequired)
{
return (string)Invoke(new Func<TextBox, string>(ReadTextBoxFromThread), new[] { textBox });
}
return textBox.Text;
}
private void SetLabelFromThread(Label label, string contents)
{
if (InvokeRequired)
{
Invoke(new Action<Label, string>(SetLabelFromThread), new object[] { label, contents });
return;
}
label.Text = contents;
}
With those in hand, I tested them by using this stupid little thread function (that matches an async
version of the ThreadStart
delegate):
private int isPaused = 0; //used like a bool, int for clear atomicity
private async void ThreadFunc()
{
while (isPaused == 0)
{
var now = DateTime.Now;
var str1 = now.ToString("T");
var str2 = now.ToString("t");
var str3 = now.ToString("s");
SetTextBoxFromThread(textBox1, str1);
SetTextBoxFromThread(textBox2, str2);
SetTextBoxFromThread(textBox3, str3);
await Task.Delay(1500);
SetLabelFromThread(label1, ReadTextBoxFromThread(textBox1));
SetLabelFromThread(label2, ReadTextBoxFromThread(textBox2));
SetLabelFromThread(label3, ReadTextBoxFromThread(textBox3));
}
}
Open two forms at the same time
You could try something like this:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
Form3 frm3 = null;
frm2.Shown += (s, args) =>
{
frm3 = new Form3();
frm3.Show();
};
frm2.FormClosing += (s, args) =>
{
frm3.Close();
};
frm2.ShowDialog(this);
}
Form2
is shown using ShowDialog
which will prevent Form1
from getting selected. When Form2
is shown, it shows Form3
in its Shown
event. If Form2
is closed, it will also close Form3
.
Run multiple UI Threads
I don't think that what you ask is really what you want but creating a message pump per thread is easy, you just have to call Application.Run once per thread.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Thread t1 = new Thread(Main_);
Thread t2 = new Thread(Main_);
t1.Start();
t2.Start();
t1.Join();
t2.Join();
}
static void Main_()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Multiple Forms in separate threads
Here's what I think you need to do:
Thread ATM2 = new Thread(new ThreadStart(ThreadProc));
ATM2.Start();
It calls this method:
private void ThreadProc()
{
var frm = new ATM();
frm.ShowDialog();
}
Related Topics
Linq to Entities Doesn't Recognize a Method
Minimal and Correct Way to Map One-To-Many with Nhibernate
Filter SQL Based on C# List Instead of a Filter Table
Two Dimensional Array Slice in C#
.Net and Bitmap Not Automatically Disposed by Gc When There Is No Memory Left
.Net Regex Matching $ with the End of the String and Not of Line, Even with Multiline Enabled
Center Multiple Rows of Controls in a Flowlayoutpanel
Unrolled Loop Works, for Loop Does Not Work
Get Text/Value from Textbox After Value/Text Changed Server Side
Newtonsoft JSON.Net Deserialization Error Where Fields in JSON Change Order
Gmail Smtp via C# .Net Errors on All Ports
Popup's in Selenium Webdrivers
Using Mediaelement to Play Video from Stream
System.Net.Http: Missing from Namespace? (Using .Net 4.5)