Click to See Complete Forum and Search --> : How to creat a modeless dialog in the thread?


jessie9a
January 24th, 2007, 09:25 PM
There is a while loop in the thread.

public void ThreadFunction()
{
int count=0;
while (true)
{
count++;
if(count==10||count==20)
{
newDialog dlg=new newDialog();
dlg.show();
}
if(count==100)
{
break;

}

Thread.Sleep(1000);


}
}

newDialog is the modeless dialog which is to be shown.But when the "dlg.show();" runs, the application stops and couldn't do anything?Is there any methods to show modeless dialog in the thread?

Blacklazy
January 24th, 2007, 10:08 PM
LoL....ohy... :rolleyes:

I'm not even going to comment on the solution. I'm not sure what that would be usefull for...

But anyway, on to your question. I assure you the code is working "correctly" for what ever its intent is to do. The applicaiton isn't stopping. Your just not waiting long enough. When you are making the thread hibernate after each loop it just takes FOREVER to complete. Based on that code you will need to wait about 5 minuets for it to stop processing and return control back to the user.

You can also verify with the debugger that the code works "correctly"... :)

I am interested in knowing what type of a solution that is supposed to solve.

jessie9a
January 25th, 2007, 01:39 AM
Should I use Control.Invoke?

zips
January 25th, 2007, 10:57 AM
I don't think you can do any GUI type things from a secondary thread. All GUI things are done in the main thread, so trying a dialog from the secondary thread causes a threading crash.

darwen
January 25th, 2007, 11:05 AM
Threads in C# do not have a windows message loop (unlike CWinThread in MFC) which means that you can't show any modeless forms using them.

You can show a form modal though because this has its own message loop.

However it's very difficult to tell from your code what you're trying to do... why aren't you using a 'for' loop for instance ?

Darwen.

Blacklazy
January 25th, 2007, 11:50 AM
I don't think you can do any GUI type things from a secondary thread. All GUI things are done in the main thread, so trying a dialog from the secondary thread causes a threading crash.


First of all the thread isn't stopping, second that code itsn't starting a second thread, its using the current (main) thread, to open the new form, and then putting the current thread to sleep. ;)

The difference here is that Show() is being used intead of ShowDialog(). I assume that this is what she? (I assume from the name) is referring to. When you use the ShowDialog() method the form stops application execution until its closed, when you use Show() it does not, I assume this is what she means from "modeless". It just appears that the form is being modeled because it takes a long time for the program to finish executing that loop. It doesnt stop the thread as she stated, but it locks up the repainting of the UI until the loop is completed. Which takes about 5 minuets as I stated before.

You can create a modeless form in the main application thread in .Net by using the Show() method in place of the ShowDialog().

If you could tell us more about what you were trying to accomplish with this solution someone may be able to provide you will a better solution that wont up the UI.