473,396 Members | 1,871 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,396 software developers and data experts.

A doubt regarding widgets and threads.

I have a doubt: is it dangerous to create widgets out of the main thread, or
it is dangerous to call methods of a widgets out of the thread which created
it? or both?

TIA
Perry
Nov 16 '05 #1
6 1284
If by widget you mean UI controls fro a windows forms application then a control cannot be created on a different thread than its parent (you can have more than one top level window - each on its own thread). You *must not* call a form or control from a thread other than the one which created it. To update the UI from another thread you have to marshal the update on to the UI thread using Control.BeginInvoke.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Ov**************@TK2MSFTNGP10.phx.gbl>

I have a doubt: is it dangerous to create widgets out of the main thread, or
it is dangerous to call methods of a widgets out of the thread which created
it? or both?

TIA
Perry

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #2
If by widget you mean UI controls for a windows forms application then a control cannot be created on a different thread than its parent (you can
have more thanone top level window - each on its own thread).
I think you meant that a dialog (the parent) and a button displayed inside
the window must be created by the same thread, ok. But if I have a top level
window, that spawn a thread that in its Run() method calls:

myDialog = new MyDialog ();
myDialog.ShowDialog (this);

where "MyDialog" and "this" are both a Windows.System.Forms.Form (and
MyDialog is a modal dialog), there could be problems? I am experiencing that
the dialog sometimes appear, and sometimes does not, it's very rare, but it
happens.

You *must not* call a form or control from a thread other than the one which created it. To update the UI fromanother thread you have to marshal the update on to the UI thread using

Control.BeginInvoke.

Yes, ok.
Thanks in advance,
Perry
Nov 16 '05 #3

Hello,
This isn't going to work as no-one is pumping messages on the spawned thread.

nice to know. Indeed it worked very badly.

Each UI thread needs to call Application.Run to start the message pump and this isn't going to work with ShowDialog. You can pass the form as the param
toApplication.Run but the main thread will not wait for it to exit. If you are calling ShowDialog why are you using a background thread?
The problem is that the thread is spawned by the main window's constructor.
This thread check periodically for a condition, and when the condition is
detected, the ShowDialog is run. I could use a timer instead of a thread,
but since the check requires some seconds (5 seconds) it would hang the
message cycle pump (it would hang the application for a time span) for 5
seconds... and i used a thread to avoid just that stuckness.

Any hint?

Luca

The main thread will not continue until the child form has exited so you will not have any parallel processing.
Secondary UI threads are useful for situations where you want to have a number of independent top level windows in one process - like Internet
Explorer, each browser window runs in its own thread all owned by the
IExplore.exe process.

Nov 16 '05 #4
Why a hidden form that you pass to Application.Run on the secondary thread that checks for the condition then calls ShowDialog on the form you want to display.

What puzzles me is why you want to use ShowDialog rather than Show when you seem to want non-modal behavior. What is the logic behind using ShowDialog?

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<em**************@TK2MSFTNGP12.phx.gbl>
Hello,
This isn't going to work as no-one is pumping messages on the spawned thread.

nice to know. Indeed it worked very badly.

Each UI thread needs to call Application.Run to start the message pump and this isn't going to work with ShowDialog. You can pass the form as the param
toApplication.Run but the main thread will not wait for it to exit. If you are calling ShowDialog why are you using a background thread?
The problem is that the thread is spawned by the main window's constructor.
This thread check periodically for a condition, and when the condition is
detected, the ShowDialog is run. I could use a timer instead of a thread,
but since the check requires some seconds (5 seconds) it would hang the
message cycle pump (it would hang the application for a time span) for 5
seconds... and i used a thread to avoid just that stuckness.

Any hint?

Luca

The main thread will not continue until the child form has exited so you will not have any parallel processing.
Secondary UI threads are useful for situations where you want to have a number of independent top level windows in one process - like Internet
Explorer, each browser window runs in its own thread all owned by the
IExplore.exe process.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #5

"Richard Blewett [DevelopMentor]" <ri******@develop.com> ha scritto nel
messaggio news:em**************@TK2MSFTNGP10.phx.gbl...
Why a hidden form that you pass to Application.Run on the secondary thread that checks for the condition then calls ShowDialog on the form you want todisplay.
So, if I understood correctly, you are suggesting to me to use something
like this:

class MyThread
{
public void Run ()
{
while(true)
if (conditionisDetected)
Application.Run (new MyDialog ());
}
}//class

Am i right?
What puzzles me is why you want to use ShowDialog rather than Show when you seem to want non-modal behavior. What is the logic behind usingShowDialog?
I want a modal behaviour. When this form of class MyDialog is created, i
dont want that the user could interact with the main window. And I'm
creating (as i said using a 'new MyDialog ().ShowDialog(this)', where 'this'
is the parent window) the dialog within a secondary thread (instead from the
main thread) because the "conditionDetected" takes many seconds to returns
'true' or 'false', and I dont want that the application gets stopped for
those seconds. I think I could put that Run() code inside a method called
periodically by a timer, and then calling Application.DoEvents() while
checking the condition I could avoid that the applciation gets stopped. What
you think?

Thanks for the help,
Luca

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<em**************@TK2MSFTNGP12.phx.gbl>

Hello,
> This isn't going to work as no-one is pumping messages on the spawned thread.

nice to know. Indeed it worked very badly.

>Each UI thread needs to call Application.Run to start the message pump

and this isn't going to work with ShowDialog. You can pass the form as the param to
>Application.Run but the main thread will not wait for it to exit.
>If you are calling ShowDialog why are you using a background thread?


The problem is that the thread is spawned by the main window's

constructor. This thread check periodically for a condition, and when the condition is
detected, the ShowDialog is run. I could use a timer instead of a thread,
but since the check requires some seconds (5 seconds) it would hang the
message cycle pump (it would hang the application for a time span) for 5
seconds... and i used a thread to avoid just that stuckness.

Any hint?

Luca

>The main thread will not continue until the child form has exited so you

will not have any parallel processing.
>
> Secondary UI threads are useful for situations where you want to have a

number of independent top level windows in one process - like Internet
Explorer, each browser window runs in its own thread all owned by the
IExplore.exe process.
>


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #6
If that is your requirement you need to make the the main form call ShowDialog. You need a setup something like this (ognoring the detail of stopping the thread for clarity)

delegate void ConditionMetDel();

class MyThread
{
Form mainForm
public MyThread(Form mainForm)
{
this.mainForm = mainForm;
}
public void Run()
{
while(true)
{
if( conditionDetected )
{
mainForm.BeginInvoke(new ConditionMetDel(mainForm.TellMeWhenConditionMet), new object[]{});
}
Thread.Sleep(1000); // or whatever interval works for you - just don't spin the thread in a tight loop
}
}
}

Then in the Form's TellMeWhenConditionMet method call ShowDialog in your child form.If you want the thread to put make the dialog appear every time the condition is met but not while the dialog is being shown you probably want to use mainForm.Invoke(); If its a one off notification then you want to end the thread after the BeginInvoke call.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<ex**************@TK2MSFTNGP12.phx.gbl>
"Richard Blewett [DevelopMentor]" <ri******@develop.com> ha scritto nel
messaggio news:em**************@TK2MSFTNGP10.phx.gbl...
Why a hidden form that you pass to Application.Run on the secondary thread that checks for the condition then calls ShowDialog on the form you want todisplay.
So, if I understood correctly, you are suggesting to me to use something
like this:

class MyThread
{
public void Run ()
{
while(true)
if (conditionisDetected)
Application.Run (new MyDialog ());
}
}//class

Am i right?
What puzzles me is why you want to use ShowDialog rather than Show when you seem to want non-modal behavior. What is the logic behind usingShowDialog?
I want a modal behaviour. When this form of class MyDialog is created, i
dont want that the user could interact with the main window. And I'm
creating (as i said using a 'new MyDialog ().ShowDialog(this)', where 'this'
is the parent window) the dialog within a secondary thread (instead from the
main thread) because the "conditionDetected" takes many seconds to returns
'true' or 'false', and I dont want that the application gets stopped for
those seconds. I think I could put that Run() code inside a method called
periodically by a timer, and then calling Application.DoEvents() while
checking the condition I could avoid that the applciation gets stopped. What
you think?

Thanks for the help,
Luca

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<em**************@TK2MSFTNGP12.phx.gbl>

Hello,
This isn't going to work as no-one is pumping messages on the spawned thread.

nice to know. Indeed it worked very badly.

Each UI thread needs to call Application.Run to start the message pump

and this isn't going to work with ShowDialog. You can pass the form as the param to
Application.Run but the main thread will not wait for it to exit.
If you are calling ShowDialog why are you using a background thread?


The problem is that the thread is spawned by the main window's

constructor. This thread check periodically for a condition, and when the condition is
detected, the ShowDialog is run. I could use a timer instead of a thread,
but since the check requires some seconds (5 seconds) it would hang the
message cycle pump (it would hang the application for a time span) for 5
seconds... and i used a thread to avoid just that stuckness.

Any hint?

Luca

The main thread will not continue until the child form has exited so you

will not have any parallel processing.

Secondary UI threads are useful for situations where you want to have a

number of independent top level windows in one process - like Internet
Explorer, each browser window runs in its own thread all owned by the
IExplore.exe process.


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004

[microsoft.public.dotnet.languages.csharp]


---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 24/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #7

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

Similar topics

1
by: sujata | last post by:
Hi, I have a query regarding threads. Is it possible to create thread pools in python.i.e. create threads and then retrieve the memory allocated to it so that the same threads can be used over and...
2
by: William Gill | last post by:
I need to display a couple of labels and a checkbox from each entry in my database. Simple enough, but there are several hundred records, and I only want to display 5 or 10 at a time. Can this be...
6
by: reji_thomas | last post by:
Hi, I have a doubt in the following code: struct xyz { int x; long l; float f; };
7
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.