Question

I have created a login form (frmLogin) in vb.net. While logging in I'm showing processing dialog (frmProcessing.ShowDialog). When the user clicks on the login button it checks the database whether the user is available or not at this background work I'm showing processing dialog. After checking the database I need to close this processing dialog then I want to show "welcome msgbox". All are working well and the problem is, while displaying mesbox the processing dialog also running. I cannot close it Please Help Me..... Thanks in Advance

My Code.....

Login()

 frmProcessing.ShowDialog()

 BackWorker.RunWorkerAsync()

End Login

DoWork()

 IF CheckInDataBase(Username,Pass) then     'checking user available or not
        BackWorker.ReportProgress(0)        'Here i need to show welcome msgbox
 Else
        BackWorker.ReportProgress(1)        'Here i need to show invalid pswd msgbox
 End IF

End DoWork

ProgressChanged()

   frmProcessing.close()         'This code does not work

   If e.ProgressPercentage=0 then

       msgbox("Welcom")

   elseif e.ProgressPercentage=1 then

         msgbox("Invalid Pswd")
   End IF

End ProcessChanged

When i'm showing "welcome/invalid pswd msgbox" the processing dialog (frmProcessing) also running in background I need to close it first then want to show msgbox.....

Was it helpful?

Solution

The part which causes problem seems to be in the following part:

   frmProcessing.close()         'This code does not work

   If e.ProgressPercentage=0 then

       msgbox("Welcom")

   elseif e.ProgressPercentage=1 then

         msgbox("Invalid Pswd")
   End IF

This piece of code shouldn't be the part of Progress_Changed event. BackgroundWorker supports another delegate function RunWorkerCompleted, any change to the UI after background process completion should be done in this delegate.

So you code should look like:

Dim isValidUser as Boolean   ' Global variable

Private Sub backgroundWorker1_DoWork( _
ByVal sender As Object, _
ByVal e As DoWorkEventArgs) _
Handles backgroundWorker1.DoWork

isValidUser = CheckInDataBase(Username,Pass)

End Sub  

Private Sub backgroundWorker1_RunWorkerCompleted( _
ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) _
Handles backgroundWorker1.RunWorkerCompleted

       frmProcessing.Close()          

       If isValidUser then
             msgbox("Welcom")
       else 
             msgbox("Invalid Pswd")
       End IF
End Sub

NOTE: MSGBOX is a VB6 style. Use MessageBox.Show instead.

OTHER TIPS

you can do as below

if (frmProcessing.ShowDialog() == DialogResult.OK)
{
     BackWorker.RunWorkerAsync()
}

no need to close the frmProcessing in ProgressChanged method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top