Using MsgBox in a thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all, I have a VB.NET app which contains 1 form. The form starts a thread
which does some processing based on what the user is doing on the form.

My problem is, the thread can display a MsgBox which is not Modal to the
form. If I click on the form while the MsgBox is up, the MsgBox hides behind
the form.

Is there anyway to tie the MsgBox or thread to the form so it acts as Modal
to the form?

RML
 
RML said:
My problem is, the thread can display a MsgBox which is not Modal to the
form. If I click on the form while the MsgBox is up, the MsgBox hides
behind
the form.

Is there anyway to tie the MsgBox or thread to the form so it acts as
Modal
to the form?

Add a method to a form that is running in the main thread that will show the
messagebox. Then use 'Control.Invoke'/'Control.BeginInvoke' to call the
form's method in order to show the messagebox. Notice that instance members
of Windows Forms controls are not safe for multithreading. Consequently,
you cannot call the form's method directly.
 
Thanks Herfried, I have never used Control.Invoke. How do I use this in my
thread to call the new form method. Do I need to create a Delegate, and use
the Delegate to Invoke the new form mothod?

RML
 
RML said:
I have never used Control.Invoke. How do I use this in my
thread to call the new form method. Do I need to create a Delegate, and
use
the Delegate to Invoke the new form mothod?

I have to learn for an exam today, so I am not able to write an example.
However, you will find information on multithreading and Windows Forms here:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
 
Hi Herfried, hope your exam went well.

I attempted to impliment a Method Invoke to display MsgBox'sw from my
thread. Here is what I did...

I 1st declared a Delegate in a Module called Common.vb...
Public Delegate Function MsgBoxInvoke(ByVal szMsg As String, ByVal buttons
As Microsoft.VisualBasic.MsgBoxStyle, ByVal szTitle As String) As
Microsoft.VisualBasic.MsgBoxResult

Public MsgBoxScriptor As MsgBoxInvoke

In my main Form I added the Mothod:
Private Function MsgBoxInvoke(ByVal szMsg As String, ByVal buttons As
Microsoft.VisualBasic.MsgBoxStyle, ByVal szTitle As String) As
Microsoft.VisualBasic.MsgBoxResult
MsgBoxInvoke = MsgBox(szMsg, buttons, szTitle)
End Function

In Form.Load() I added...
Common.MsgBoxScriptor = New MsgBoxInvoke(AddressOf Me.MsgBoxInvoke)

In my thread I call...
Common.MsgBoxScriptor.Invoke("Test Msg", MsgBoxStyle.Exclamation, "Title")

When run, the MsgBox comes up, but if I click on the Form it still gets
hidden.

Am I doing this wrong?

RML
 
Hello RML,
I can give you an example about delegates but in VC++ .NET. I guess that you
may understand the idea and modify it to VB.NET.

First define a delegate in the main class as below:

private: __delegate System::Void TaskDelegate( );

then define a method in your class which will open a Message box.

private: System::Void fnc_ShowMsgBx( )
{
...
MessageBox::Show(S"This is my text", S"Title");
}

Now, it is time to show fnc_ShowMsgBx to be called within the Threaded
method. Let's say that your thread is a background-working thread and it runs
the "fnc_MethodOfThread( )"

private: System::Void fnc_MethodOfThread( )
{
...
if ( ..... ) // this is your control statement.
{
TaskDelegate* myTaskDelegate = new TaskDelegate(this, fnc_ShowMsgBx);
this->BeginInvoke(myTaskDelegate);
}
...
}

That is all.
If you check the Thread IDs in the methods, you will see a Thread ID in
"fnc_ShowMsgBx" that is same as with the Main thread -your application's
thread Id-. But you will see a different thread ID in "fnc_MethodOfThread"

Wish you good work
Alper AKCAYOZ (Bil Muh)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top