WinForms with one thread per window?

H

Hans Merkl

My app displays real time data in grids on several forms. Each grid usually
contains several thousand records. When we open a lot of forms the GUI
thread can't keep up with updating all the grids (data decoding is done on
a separate thread). Our clients have multi core CPUs but because the single
GUI thread is the bottleneck the additional cores don't get utilized.

Some years ago I created a similar app with MFC and there I ran every
window in its own thread. This worked very well and I am wondering if I can
do the same thing with .NET.

I think it would be relatively easy to implement but I wonder if we might
run into some WinForms problems.

Does anybody here have experience with multiple GUI threads in a WinForms
app?
 
L

Linda Liu [MSFT]

Hi Hans,

Based on my understanding, you have several forms in your WinForm
application, each of which contains a DataGridView. You display real time
data in these DataGridViews. The problem is that when you open a lot of
forms, the UI thread can't refresh all these DataGridViews timely. If I'm
off base, please feel free to let me know.

Yes, we can run a WinForm in its own thread in .NET application. To do
this, create a thead and call Application.Run to set up a message loop for
the form to show when the thread is started. The following is a sample.

public partial class Form1 : Form
{
List<Form> forms = new List<Form>();
private void Form1_Load(object sender, EventArgs e)
{
Thread t1= new Thread(new ParameterizedThreadStart(ShowForm));
Form frm2 = new Form2();
forms.Add(frm2);
t1.Start(frm2);

Thread t2 = new Thread(new ParameterizedThreadStart(ShowForm));
Form frm3 = new Form3();
forms.Add(frm3);
t2.Start(frm3);
}

private void ShowForm(object frm)
{
Application.Run(frm as Form);
}
}

We need to close the instances of Form2 and Form3 explicitly when the
instance of Form1 is closed, because the instances of Form2 and Form3 are
running in other threads. Note the cross-thread operation problem when
closing the Fom2 and Form3 instances from Form1. The following is a sample.

public class BaseForm : Form
{
public delegate void CloseFormDelegate();
public void CloseForm()
{
if (this.InvokeRequired)
{
this.Invoke(new CloseFormDelegate(CloseForm));
}
else
{
this.Close();
}
}
}

public partial class Form2 : BaseForm
{ ....}
public partial class Form3 : BaseForm
{ ....}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += new
FormClosingEventHandler(Form1_FormClosing);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < forms.Count; i++)
{
(forms as BaseForm).CloseForm();
}
}
....
}

What I shall point out is that it's not true that the more threads you
create in your applicaion, the more efficient the application will be.
Managing multiple threads will consume much CPU and memory. At last, I
suggest that you only update the necessary data in the DataGridView each
time, rather than refreshing all the data in the grid.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Hans Merkl

Linda,

Thank you very much for your response. This very helpful.

Thanks again!

Hans

Hi Hans,

Based on my understanding, you have several forms in your WinForm
application, each of which contains a DataGridView. You display real time
data in these DataGridViews. The problem is that when you open a lot of
forms, the UI thread can't refresh all these DataGridViews timely. If I'm
off base, please feel free to let me know.

Yes, we can run a WinForm in its own thread in .NET application. To do
this, create a thead and call Application.Run to set up a message loop for
the form to show when the thread is started. The following is a sample.

public partial class Form1 : Form
{
List<Form> forms = new List<Form>();
private void Form1_Load(object sender, EventArgs e)
{
Thread t1= new Thread(new ParameterizedThreadStart(ShowForm));
Form frm2 = new Form2();
forms.Add(frm2);
t1.Start(frm2);

Thread t2 = new Thread(new ParameterizedThreadStart(ShowForm));
Form frm3 = new Form3();
forms.Add(frm3);
t2.Start(frm3);
}

private void ShowForm(object frm)
{
Application.Run(frm as Form);
}
}

We need to close the instances of Form2 and Form3 explicitly when the
instance of Form1 is closed, because the instances of Form2 and Form3 are
running in other threads. Note the cross-thread operation problem when
closing the Fom2 and Form3 instances from Form1. The following is a sample.

public class BaseForm : Form
{
public delegate void CloseFormDelegate();
public void CloseForm()
{
if (this.InvokeRequired)
{
this.Invoke(new CloseFormDelegate(CloseForm));
}
else
{
this.Close();
}
}
}

public partial class Form2 : BaseForm
{ ....}
public partial class Form3 : BaseForm
{ ....}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.FormClosing += new
FormClosingEventHandler(Form1_FormClosing);
}
void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < forms.Count; i++)
{
(forms as BaseForm).CloseForm();
}
}
....
}

What I shall point out is that it's not true that the more threads you
create in your applicaion, the more efficient the application will be.
Managing multiple threads will consume much CPU and memory. At last, I
suggest that you only update the necessary data in the DataGridView each
time, rather than refreshing all the data in the grid.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top