Click here to Skip to main content
15,886,639 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created one child dialog in a thread from my parent dialog and Use the showdialog() method to pop up the dialog. Now the child dialog is going back when we click on parent dialog.

But I need stay the dialog on top of the parent dialog as normal when we call showdialog()


C#
m_threadStartTest = new Thread(StartTest);
                m_threadStartTest.Start();




C#
private void StartTest()
       {
           MessageBoxForm obj = new MessageBoxForm();
           obj.ShowDialog();
}
Posted
Updated 21-Jan-13 23:34pm
v2
Comments
adriancs 24-Jan-13 0:08am    
I know this problem is solved.
just to share a bit idea.
does this solve your problem too?
at the load event of MessageBoxForm, add this:
this.Activate() or
this.BringToFront() or
this.TopMost = true;

You cannot do such things. Moreover, in your case, it makes no sense at all. You need to show dialog in the UI thread. This method you used as a thread start does not contain blocking calls. You had to call it directly.

[EDIT #1]

However, I realize that you can use the thread for some other reasons. And you may trigger showing this dialog from this already existing thread. In this case you can do this:
C#
class MyForm { 

// ...
        void ShowDialogFromAnyThread() {
            System.Action show = new Action(() => {
                new MessageBoxForm().ShowDialog();
            });
            if (InvokeRequired)
                Invoke(show);
            else
                show();
        } //ShowDialogFromAnyThread

} //class MyForm


Here, I also demonstrated how to reuse the show calls with local delegate instance.
You need to use Invoke, not BegingInvoke. I can explain.

You should clearly understand that the dialog will be shown in UI thread anyway.

[EDIT #2]

I also want to explain the role of InvokeRequired. Many excessively use it even it's not needed, and you might also use it. The thing is very simple: this predicate is always false if you call a function like ShowDialogFromAnyThread and always true in case of the call from any other thread. The thread is defined simply: in this context, the "UI thread" is simply the thread when this control (in this case, Form) was created. If calling thread is some other than that, invoke is always required.

This way, you can also skip the check via InvokeRequired. If you always call this function from some other thread, you don't need to check, know that invocation is always required.

On the invocation mechanism, please see my short article Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Please also see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA
 
Share this answer
 
v5
Comments
Arun Kumar K S 23-Jan-13 3:48am    
In my original code StartTest() doing several processes and showing dialogs during that processes. I don't need to write full code to explain my situation, but with a simple example my question is clear...
Sergey Alexandrovich Kryukov 23-Jan-13 3:53am    
I don't say your question is unclear. I insist that you should show you dialog in UI thread and nothing else. After all, just try it.
—SA
Arun Kumar K S 23-Jan-13 3:59am    
My child dialogs are showing based on the code in my thread. That I not explained here. I just want to popup my child dialog from thread that will looks like normal model dialog.
Sergey Alexandrovich Kryukov 23-Jan-13 11:29am    
Wait a second. I'm started to think that I misunderstood you.

Yes, that's possible that you already use a thread for some different reasons and need to show some status using a dialog. This is possible. But this dialog will be shown in UI thread. Yes, in this case you will need invoke...

OK, let me take it into account, but this will be just the improvement of the answer you already have.

—SA
Sergey Alexandrovich Kryukov 23-Jan-13 11:44am    
OK, please see my updated answer, after [EDIT].

I want to say: it was my fault; sorry for misunderstanding and possible inconvenience.

Even though your explanation was unclear to me, I could take into account this possibility but missed it, so that was my fault.

Thank you,
—SA
You can pass the parent form in your child ShowDialog method as shown below
C#
private void StartTest()
{
  if (InvokeRequired)
  {
    BeginInvoke(new MethodInvoker(StartTest));
    return;
  }
  MessageBoxForm obj = new MessageBoxForm ();
  obj.ShowDialog(parentForm); //Pass your parent form
}
Though this will work for you, it makes no sense as SA mentioned in his solution.
 
Share this answer
 
Comments
Arun Kumar K S 23-Jan-13 3:40am    
Thanks...., that worked
Sergey Alexandrovich Kryukov 23-Jan-13 3:44am    
Two people are telling you: this makes no sense. No sense at all.
Look at the last clause!
In practice, you should simply call it from UI thread, as I told you. Remove this extra thread or use for something good. You probably think that showing dialog blocks thread until the dialog is closed. No! It returns immediately and only then the dialog is actually shown. Doh!
—SA
Arun Kumar K S 23-Jan-13 3:52am    
In my original code StartTest() doing several processes and showing dialogs during that processes. I don't need to write full code to explain my situation, but with a simple example my question is clear...
Sergey Alexandrovich Kryukov 23-Jan-13 3:41am    
Yes, formally, it will work correcty, but the fact that it makes no sense is more important. You are not re-using the method StartTest. And also, it's good to pass a parameter to Invoke. Right now you pass it using the Closure (I hope you know closures? Nothing wrong with them, but it's better not to use them without the good reason). What else? It's better to use Action and call StartTest from anonymous method. And, finally, BeginInvoke is redundant; Invoke should better be used...
(I voted 4.)
—SA
Arun Kumar K S 23-Jan-13 4:17am    
OK, I will do..,Thanks to your valuable advice and support

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900