473,325 Members | 2,860 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,325 software developers and data experts.

Using MsgBox in a thread

RML
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
Nov 21 '05 #1
6 8472
"RML" <RM*@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #2
RML
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

"Herfried K. Wagner [MVP]" wrote:
"RML" <RM*@discussions.microsoft.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
"RML" <RM*@discussions.microsoft.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #4
RML
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

"Herfried K. Wagner [MVP]" wrote:
"RML" <RM*@discussions.microsoft.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5
RML
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

"Herfried K. Wagner [MVP]" wrote:
"RML" <RM*@discussions.microsoft.com> schrieb:
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>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
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)

Nov 21 '05 #7

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

Similar topics

2
by: js | last post by:
Hi all, I currently encounter a problem and it is urgent to me. After calling the MsgBox.Show(), the message box is shown with non-modal mode, what is the possible reason??? This only happen...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
3
by: Bob Day | last post by:
VS 2003, vb.net, sql msde... The help is pretty empatic that you cannot pass parameters to a thread. The sample below is from help, showing you to set up variables in the TasksClass, and assign...
0
by: Scott H | last post by:
Hi All, I've got a bit a problem I can't seem to work out. Using the code listed below, the program runs fine within the VB dev environment, but once built and running outside of VB, it doesn't...
2
by: Brent | last post by:
I have variables in a structure loaded into a list box. I thought I could use FieldInfo.SetValue to update the items value when the user clicks on it, but it is not working. .. .. .. Dim fi...
5
by: | last post by:
I have a thread which is displaying a msgbox. I need to programmatically cancel the message box if the user does click on OK. I tried to abort the thread T1.abort() , but am getting an exception....
2
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH...
0
by: vinidimple | last post by:
Hi i have a serious problem while i was working in Excel.I want to fetch columns from an excel worksheet and i need to compare it with an sql querry fields,so i tried to open an excle...
2
by: j1808 | last post by:
hi there, so yes, im am trying to make my label flash diffrent colours, so when i have enterd a value in some textbox which does not meet my parameter, (i.e runs the ELSE in an IF..) it runs this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.