473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Windows Form in seperate thread

Hello all,
Wonder what approach is used for this problem.

I have a MDIApplication, the MDIClinets are to be in a seperate thread.
So I've done something like this,

// Create a new Show method that starts a new thread
public new void Show()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
public void ThreadProc()
{
base.Show();
}

Now my problem is that I basically don't have a run loop, so as soon as the
Form shows the thread exits and I'm pretty much left in the soup.

I'm sure other people have done this in .NET?
Any ideas suggestions etc?

Thanks for you time
Brian Keating.
Nov 16 '05 #1
12 5818
On 2004-11-25, Brian Keating EI9FXB <> wrote:
Hello all,
Wonder what approach is used for this problem.

I have a MDIApplication, the MDIClinets are to be in a seperate thread.
So I've done something like this,

// Create a new Show method that starts a new thread
public new void Show()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
public void ThreadProc()
{
base.Show();
}

Now my problem is that I basically don't have a run loop, so as soon as the
Form shows the thread exits and I'm pretty much left in the soup.

I'm sure other people have done this in .NET?
Any ideas suggestions etc?

Thanks for you time
Brian Keating.


Call Application.Run in the new thread...

private void ThreadProc ()
{
Application.Run (new Form ());
}

--
Tom Shelton [MVP]
Nov 16 '05 #2
Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...

Nigel Armstrong

"Tom Shelton" wrote:
On 2004-11-25, Brian Keating EI9FXB <> wrote:
Hello all,
Wonder what approach is used for this problem.

I have a MDIApplication, the MDIClinets are to be in a seperate thread.
So I've done something like this,

// Create a new Show method that starts a new thread
public new void Show()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
public void ThreadProc()
{
base.Show();
}

Now my problem is that I basically don't have a run loop, so as soon as the
Form shows the thread exits and I'm pretty much left in the soup.

I'm sure other people have done this in .NET?
Any ideas suggestions etc?

Thanks for you time
Brian Keating.


Call Application.Run in the new thread...

private void ThreadProc ()
{
Application.Run (new Form ());
}

--
Tom Shelton [MVP]

Nov 16 '05 #3
The precise rule states

"Thou shalt not access a UI element from a thread other than the thread that created it"

Internet Explorer has multiple UI threads for example - each IE windows runs in its own UI thread even though there is noly one process.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...
Nov 16 '05 #4
Yes i'm pretty interested in same,
Also wondering how cleanup occurs? Application.ExitThread(); ?

I fear the biggest problem is going to be setting the MDIParent, I don't
know if this is going to work in a seperate thread.

I've been down this road before with a multithreaded C++/WTL application and
it was a heartache to implement, I think c# is going to be alot harder to do
this but the next few days will tell.

thanks for your help.
"Nigel Armstrong" wrote:
Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...

Nigel Armstrong

"Tom Shelton" wrote:
On 2004-11-25, Brian Keating EI9FXB <> wrote:
Hello all,
Wonder what approach is used for this problem.

I have a MDIApplication, the MDIClinets are to be in a seperate thread.
So I've done something like this,

// Create a new Show method that starts a new thread
public new void Show()
{
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
}
public void ThreadProc()
{
base.Show();
}

Now my problem is that I basically don't have a run loop, so as soon as the
Form shows the thread exits and I'm pretty much left in the soup.

I'm sure other people have done this in .NET?
Any ideas suggestions etc?

Thanks for you time
Brian Keating.


Call Application.Run in the new thread...

private void ThreadProc ()
{
Application.Run (new Form ());
}

--
Tom Shelton [MVP]

Nov 16 '05 #5
Hi Richard

Thanks for this - very helpful to me! In the specific case of an MDI
application, what happens with Menu merging? We have the MDI parent form on
one thread, and the child forms created on distinct threads...

Nigel Armstrong

"Richard Blewett [DevelopMentor]" wrote:
The precise rule states

"Thou shalt not access a UI element from a thread other than the thread that created it"

Internet Explorer has multiple UI threads for example - each IE windows runs in its own UI thread even though there is noly one process.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...

Nov 16 '05 #6
Woooahh!

you can create top level windows in separate threads not MDI children. The parent and children would be attempting to talk to eachother from separate threads which breaks the golden rule

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<D0**********************************@microsoft.co m>

Hi Richard

Thanks for this - very helpful to me! In the specific case of an MDI
application, what happens with Menu merging? We have the MDI parent form on
one thread, and the child forms created on distinct threads...

Nigel Armstrong

"Richard Blewett [DevelopMentor]" wrote:
The precise rule states

"Thou shalt not access a UI element from a thread other than the thread that created it"

Internet Explorer has multiple UI threads for example - each IE windows runs in its own UI thread even though there is noly one process.


Nov 16 '05 #7
On 2004-11-25, Nigel Armstrong <Ni************@discussions.microsoft.com> wrote:
Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...

Nigel Armstrong


Ooops! I missed the MDI part. No can do. This would cause definate
issues. I think you'll need to change your design...

As for top level windows, yes, you get a message pump per thread.

--
Tom Shelton [MVP]
Nov 16 '05 #8
Ok guys, thanks for your help,
I wonder if anyone has a suggestion on another approach.

Let me explain my problem, I've services running on my system, my
application receives diagnostic messages from these services, what i want to
do is create an MDIChild window for each service that sends a message.

Ok so far so good, but I get problems if one service is constantly sending
messages to it's window because, this window is very busy updating it's view,
hence because it's in the same thread at all other MDIChild windows and the
MDIParent window the whole application is un responsive becasue this window
is too busy.
I did manage to get the Multithread MDI working and stable in a C++ ATL/WTL
application but it proved a heartache to say the least. I'd rather not go
down that road again.

Suggestions welcome.
Thanks in advance
Brian.

"Tom Shelton" wrote:
On 2004-11-25, Nigel Armstrong <Ni************@discussions.microsoft.com> wrote:
Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per
application" rule - do we get a separate message pump for each MDI Child
window with this approach?

Just interested really...

Nigel Armstrong


Ooops! I missed the MDI part. No can do. This would cause definate
issues. I think you'll need to change your design...

As for top level windows, yes, you get a message pump per thread.

--
Tom Shelton [MVP]

Nov 16 '05 #9
If you use MDI you have limit of one thread, which is UI distributing
messages between child windows. I would go away from this paradigm.

You might be better off if you create forms on threads and use
Application.Run for these forms. This seems to create separate message pumps
and keeps windows pretty responsive if painting code is Ok. The only
drawback I've seen so far is that you have to manage separate threads /
forms as one applications - it adds some unnecessary complexity,

I am not sure why you want to have MDIChild for each service in one
application. Service is separate process, so related standalone form seems
to be natural companion.

HTH
Alex

"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message
news:BF**********************************@microsof t.com...
Ok guys, thanks for your help,
I wonder if anyone has a suggestion on another approach.

Let me explain my problem, I've services running on my system, my
application receives diagnostic messages from these services, what i want to do is create an MDIChild window for each service that sends a message.

Ok so far so good, but I get problems if one service is constantly sending messages to it's window because, this window is very busy updating it's view, hence because it's in the same thread at all other MDIChild windows and the MDIParent window the whole application is un responsive becasue this window is too busy.
I did manage to get the Multithread MDI working and stable in a C++ ATL/WTL application but it proved a heartache to say the least. I'd rather not go
down that road again.

Suggestions welcome.
Thanks in advance
Brian.

"Tom Shelton" wrote:
On 2004-11-25, Nigel Armstrong <Ni************@discussions.microsoft.com> wrote:
Hi Tom

How does this work with the "Thou Shalt have one User Interface thread per application" rule - do we get a separate message pump for each MDI Child window with this approach?

Just interested really...

Nigel Armstrong


Ooops! I missed the MDI part. No can do. This would cause definate
issues. I think you'll need to change your design...

As for top level windows, yes, you get a message pump per thread.

--
Tom Shelton [MVP]

Nov 16 '05 #10

"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message
news:BF**********************************@microsof t.com...
Ok guys, thanks for your help,
I wonder if anyone has a suggestion on another approach.

Let me explain my problem, I've services running on my system, my
application receives diagnostic messages from these services, what i want
to
do is create an MDIChild window for each service that sends a message.

Ok so far so good, but I get problems if one service is constantly
sending
messages to it's window because, this window is very busy updating it's
view,
hence because it's in the same thread at all other MDIChild windows and
the
MDIParent window the whole application is un responsive becasue this
window
is too busy.
I did manage to get the Multithread MDI working and stable in a C++
ATL/WTL
application but it proved a heartache to say the least. I'd rather not go
down that road again.


You should be able to handle this situation gracefully if you decouple the
window's UI operations (i.e., repaint) completely from the processing of
diagnostics messages. In other words, have the diagnostics message receiver
thread place the information into your own queue (or list, doesn't matter),
and then call Invalidate for the window (via Control.Invoke or BeginInvoke
to make it thread-safe).

If you really get lots of diagnostics messages and calling Invalidate is
still not good enough, then consider not calling Invalidate every time a
diagnostics message arrives. Simply place the diagnostics information into
your own queue and have the UI thread check the state of the queue e.g.,
once per second on a timer. This way you will get a refresh at a comfortably
slow rate no matter how fast the diagnostics messages arrive.

I'd go creating forms in multiple threads only as the very last resort.

Regards,
Sami
Nov 16 '05 #11
Hi Alex,
I've a test app created that used the Application.Run as you suggest,
this is pretty close to what I want, however i do get some events twice etc,

but I hope i'm getting close.
thanks for your help.
regards
Brian.

"AlexS" wrote:
If you use MDI you have limit of one thread, which is UI distributing
messages between child windows. I would go away from this paradigm.

You might be better off if you create forms on threads and use
Application.Run for these forms. This seems to create separate message pumps
and keeps windows pretty responsive if painting code is Ok. The only
drawback I've seen so far is that you have to manage separate threads /
forms as one applications - it adds some unnecessary complexity,

I am not sure why you want to have MDIChild for each service in one
application. Service is separate process, so related standalone form seems
to be natural companion.

HTH
Alex

"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message
news:BF**********************************@microsof t.com...
Ok guys, thanks for your help,
I wonder if anyone has a suggestion on another approach.

Let me explain my problem, I've services running on my system, my
application receives diagnostic messages from these services, what i want

to
do is create an MDIChild window for each service that sends a message.

Ok so far so good, but I get problems if one service is constantly

sending
messages to it's window because, this window is very busy updating it's

view,
hence because it's in the same thread at all other MDIChild windows and

the
MDIParent window the whole application is un responsive becasue this

window
is too busy.
I did manage to get the Multithread MDI working and stable in a C++

ATL/WTL
application but it proved a heartache to say the least. I'd rather not go
down that road again.

Suggestions welcome.
Thanks in advance
Brian.

"Tom Shelton" wrote:
On 2004-11-25, Nigel Armstrong <Ni************@discussions.microsoft.com> wrote: > Hi Tom
>
> How does this work with the "Thou Shalt have one User Interface thread per > application" rule - do we get a separate message pump for each MDI Child > window with this approach?
>
> Just interested really...
>
> Nigel Armstrong

Ooops! I missed the MDI part. No can do. This would cause definate
issues. I think you'll need to change your design...

As for top level windows, yes, you get a message pump per thread.

--
Tom Shelton [MVP]


Nov 16 '05 #12
Hi Sami,
Tnx for your help, I'm still doing up a few tests so when I finally get a
"workable" solution I'll post an article on it.

thanks for your time.

Brian

"Sami Vaaraniemi" wrote:

"Brian Keating EI9FXB" <csharp at briankeating.net> wrote in message
news:BF**********************************@microsof t.com...
Ok guys, thanks for your help,
I wonder if anyone has a suggestion on another approach.

Let me explain my problem, I've services running on my system, my
application receives diagnostic messages from these services, what i want
to
do is create an MDIChild window for each service that sends a message.

Ok so far so good, but I get problems if one service is constantly
sending
messages to it's window because, this window is very busy updating it's
view,
hence because it's in the same thread at all other MDIChild windows and
the
MDIParent window the whole application is un responsive becasue this
window
is too busy.
I did manage to get the Multithread MDI working and stable in a C++
ATL/WTL
application but it proved a heartache to say the least. I'd rather not go
down that road again.


You should be able to handle this situation gracefully if you decouple the
window's UI operations (i.e., repaint) completely from the processing of
diagnostics messages. In other words, have the diagnostics message receiver
thread place the information into your own queue (or list, doesn't matter),
and then call Invalidate for the window (via Control.Invoke or BeginInvoke
to make it thread-safe).

If you really get lots of diagnostics messages and calling Invalidate is
still not good enough, then consider not calling Invalidate every time a
diagnostics message arrives. Simply place the diagnostics information into
your own queue and have the UI thread check the state of the queue e.g.,
once per second on a timer. This way you will get a refresh at a comfortably
slow rate no matter how fast the diagnostics messages arrive.

I'd go creating forms in multiple threads only as the very last resort.

Regards,
Sami

Nov 16 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Fabio Papa | last post by:
I am trying to write a windows service that sends emails to clients at specific times based on information in a sql db. Since this is done for multiple cities, I start a thread for each city and...
14
by: Brian Keating EI9FXB | last post by:
I wonder can anyone reccomment a solution to this problem. Let me explain, I've services running on my system, my application receives diagnostic messages from these services, what i want to do...
5
by: vinoth | last post by:
Hi, I have created WindowsService Project.In that Project OnStart Method i have written the following Code. In this code the Server is waiting for the connection from client. When the Client...
6
by: | last post by:
Just a general question... When working with a form containing a treeview or similar control... if you need to show different form fields depending on what is selected in the treeview then what...
8
by: PAPutzback | last post by:
How do I keep the form up.
3
by: cbrown | last post by:
I have a vb app that watches the COM port for incoming data. When it sees that data it, it displays a form with a single label control on it. When I call the form.Show() method, the form shows...
1
by: Rykie | last post by:
I am primarily a web developer, but have to develop this windows app. It works a treat, but it has some places where it lags and some long running processes, which I want to provide user feedback...
3
by: sekarm | last post by:
Dear guys, In C# windows application i am using four threads for seperate process. Each thread done seperate job.i want to monitor the thread process in main windows form.so each...
6
by: Steve | last post by:
Hi All I have an on-screen keyboard within a POS program I have written in VB.net 2005, for touch screen computers I have it set to 'always on top' so the user can move the cursor to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.