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 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]
---
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]