שתף באמצעות


sometimes messagebox.show can not appear on screen.

Question

Friday, April 15, 2011 3:08 AM

I am using visual basic .net 2005 win application.

My code,

Dim

coyCollect As New Collections.Specialized.StringCollection

Dim

WithEvents TestWorker As New System.ComponentModel.BackgroundWorker

Dim

 

frm31 As WF301

in btnOk_Click event

TestWorker.WorkerSupportsCancellation =

True

TestWorker.RunWorkerAsync()

frm31 = New WF301

frm31.SetDesktopLocation(40, 650)

frm31.ShowDialog() (this is just a "in progress" message screen)

in TestWorker_DoWork event

Dim Res As Integer = Function1(coyCollect, strEr)

(so i am sending list of company codes to the function. and in function i loop through to do something. if success i will return 1 if some error occures i will return 2)

 

If Res = 1

Then

MessageBox.Show(

"Success.", "My App"

, MessageBoxButtons.OK, MessageBoxIcon.Information)

 

ElseIf Res = 2

Then

MessageBox.Show(

"Failed," & Chr(10) & strEr, "My App"

, MessageBoxButtons.OK, MessageBoxIcon.Information)

 

End

If

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

in TestWorker_RunWorkerCompleted event

frm31.close()



My probelm, sometimes messagebox.show method don't show messagebox on screen. It will appear in task bar but we have to click it to show it on screen (looks like it is hanged but messagebox is already there in taskbar so click it to show message "success" then click ok to close the messagebox). then only we can click "ok" to close it. without clicking it my "in progress" window won't close. sometimes messagebox will appear with no problem. Any idea?

h4007

All replies (8)

Friday, April 15, 2011 7:33 AM ✅Answered

This occurs when the user clicks twice on whatever control opens the message box.  The first click opens the message box, but the second click appears on the form, and brings it to the front.  This makes it appear that the message box has gone behind the form and can only be accessed from the taskbar.  It also occurs if the user is typing on the form when the messagebox appears - for the same reason.

If you replace your messagebox with your own dialog form, you can use BringToFront to force it to appear on top of any active forms.


Friday, April 15, 2011 11:53 AM ✅Answered

TestWorker.RunWorkerAsync()
frm31 = New WF301
frm31.SetDesktopLocation(40, 650)
frm31.ShowDialog() (this is just a "in progress" message screen)

I got a feeling the code above may cause sort of thread race between frm31 and messagebox.

The thread running slower will be shown on top.

If TestWorker is slower, then messagebox will be displayed after frm31 poped out. Otherwise, you will see frm31 overlapped on the messagebox. You might put a small delay inside backgroundworker to make sure it is running a bit longer, see whether the issue still occurred or not

regards


Friday, April 15, 2011 5:36 AM

If you have anything which you don't like from a messagebox, then make your own, it is very easy.

Using a windows forms all features are available.

YOu have then to show that created form with

using myDialog as new TheCreatedForm
    if myDialog.ShowDialog = DialogResult.OK Then
       'do what should happen
    End if
end using

Be aware that if you don't use the using, it is in this case better to use the dispose method if the dialog is showed, because the showdialog uses in fact the internal dialog.

 

Success
Cor


Wednesday, April 20, 2011 7:03 AM

Hi h4007,

Any update to this problem?

Please let us know if you have further questions about it.

 

Best Regards,

Kee Poppy [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Monday, April 25, 2011 7:52 AM

Hi,

I think MiniMum_max guess was correct. i wrote following code to slow the thread.

My code inside the background worker,

Threading.thread.sleep(100) - so it is working fine now. But is it ok to use thread.sleep method? I heard it is not a good practice

Any views on this will be good to read.

Thanks.

h4007


Monday, April 25, 2011 9:08 AM

You can use one of the overloads that takes an IWin32Window parameter to set the owning window of the messagebox:

MessageBox.Show(Me, "Hello World")

There are similar overloads available for classes that have ShowDialog methods.

It won't work in this situation though because the messagebox is in another thread - I wouldn't like to set the parent cross-thread.

I would just alter the code so that the messagebox is not shown in another thread. Keep all UI stuff on the UI thread. The background worker can return values in e.Result, to return more than one value use an object array as the value. You can look at the return value in the RunWorkerCompleted event, which runs in the UI thread.

 

Private Sub testWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles testWorker.DoWork
  Dim strEr As String
  Dim err As Integer = Function1(coyCollect, strEr)
  e.Result = New Object() {err, strEr}
End Sub

Private Sub testWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles testWorker.RunWorkerCompleted
  Dim returnValue() As Object = CType(e.Result, Object())
  Dim res As Integer = returnValue(0)
  Dim strEr As String = returnValue(1)
  If res = 1 Then
    MessageBox.Show(Me, "Success.", "My App", MessageBoxButtons.OK, MessageBoxIcon.Information)
  ElseIf res = 2 Then
    MessageBox.Show(Me, "Failed," & Chr(10) & strEr, "My App", MessageBoxButtons.OK, MessageBoxIcon.Information)
  End If
End Sub


Friday, April 29, 2011 2:33 AM

Hi,

But then how to show 'In progress' messagebox? (What i am doing isn't it a good way?)

I want user to know that, don't do anything. Wait till system tells that it has finished the job. If we don't show anything it will look like system hang. (for example i have to import accounts for say 30 or more companies (each will have difinitely not less than 700 accounts)

 

h4007


Friday, April 29, 2011 7:04 AM

Hi,

How about just use backgroundworker to report the progress, then show the progress bar on the main form.

It is normally what backgroundworker does.

Regards