1

frmMain has a button that kicks off a background worker

Private Sub btnProcessITN_Click(sender As Object, e As EventArgs) Handles btnProcessITN.Click
    Me.frmMainProgressBar.Visible = True
    Me.btnProcessITN.Text = "working..." & Me.frmMainProgressBar.Value & "%"
    Me.btnProcessITN.Enabled = False


    backgroundWorker2.WorkerReportsProgress = True
    BackgroundWorker2.RunWorkerAsync()
End Sub

Private Sub backgroundWorker2_DoWork(ByVal sender As Object, ByVal e As    System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
    ProcessITN()
End Sub

ProcessITN lives in modITN. This runs, does its thing, then PROBLEM

Public Sub ProcessITN()
...doing work here....
    frmMain.updateProgress(current, Max)       
End Sub

and back in form main I have a function that tries to update the progress of the background worker, and update a progress bar on the form

Public Function updateProgress(current As Integer, Max As Integer) As Boolean

    BackgroundWorker2.ReportProgress((current / Max) * 100)
    Return True
End Function


Private Sub backgroundWorker2_ProgressChanged( _
    ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) _        Handles BackgroundWorker2.ProgressChanged
    Me.frmMainProgressBar.Value = e.ProgressPercentage
    Me.btnProcessITN.Text = "working..." & Me.frmMainProgressBar.Value & "%"
    'Me.txtProgress.Text = Me.txtProgress.Text & vbNewLine & strMessage
    Me.txtProgress.AppendText(vbNewLine & strMessage)
End Sub

Now, the problem is, that although the code is running, and the values are being passed across correctly from modITN to frmMain, the form itself is not refreshing. the progress bar does not change. txtProgress does not change.

N.B this code was working perfectly when the stuff within modITN was within frmMain. I moved it to try and tidy my code.

Mr.Adam
  • 294
  • 5
  • 23

2 Answers2

0

I've sorted the problem!

I moved the backgroundworker blocks to modITN, out frmMain. The whole thing runs in modITN now, and updates the progress bar from there. frmMain simply calls the sub in modITN to start the whole thing off.

Simple!

Mr.Adam
  • 294
  • 5
  • 23
0

You may still have a look here, I had the same question - and there are some more issues to it.

Multithreading for a progressbar and code locations (vb.net)?

VB.net's 'Default Instance' of Forms is a hideous trap in my opinion.

Community
  • 1
  • 1
KekuSemau
  • 6,830
  • 4
  • 24
  • 34