Question : Redraw problem when application is busy - thread operations?

I have an application in VS2008 (VB.NET) that performs some lengthy operations. On my main form, I have a progress bar (in marquee mode) that I'd like to be visible. When the application is executing subs and functions, the form locks up and is poorly drawn (as pictured) for several minutes while code is executed - I believe the solution is to run the functions in another thread. How is this done SIMPLY?

When a radio button is selected, the functions are executed.
Example code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
...
  ElseIf Me.RadioButtoni7.Checked = True Then
    ExecuteOperation("Start")
    RestartClusteredServiceProduction("HF1")
    RestartClusteredServiceProduction("Sys2")
    ExecuteOperation("Stop")
  ElseIf Me.RadioButtoni8.Checked = True Then
    ExecuteOperation("Start")
    RestartPSProduction()
    RestartSFProduction()
    RestartQRProduction()
    ExecuteOperation("Stop")
  ElseIf Me.RadioButtoni9.Checked = True Then
    ExecuteOperation("Start")
    RestartTServerTest()
    RestartEPSTest()
    ExecuteOperation("Stop")
  ElseIf Me.RadioButtono1.Checked = True Then
...
Attachments:
 
Application frozen while executing
Application frozen while executing
 
 
How the application should look while executing (progress bar is visible) (some form elements pixelated for privacy/security)
How the application should look while executing (progress bar is visible) (some form elements pixelated for privacy/security)
 

Answer : Redraw problem when application is busy - thread operations?

Below is some pseudo code showing simplified use of the BackgroundWorker() control:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.RunWorkerAsync(ParameterOptionalHere)
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ' grab the parameter if used:
        Dim x = e.Argument ' cast to whatever you need
        Foo(x)
    End Sub

    Private Sub Foo()

        ' ...your existing sub...

        ' Use ReportProgress() if you need to update the GUI during the operation:
        BackgroundWorker1.ReportProgress(0, ANY_OBJECT_HERE)
        ' first parameter is typically 0 to 100  for a progressbar
        ' second parameter can be used to passy ANYTHING
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ' ...update the GUI from this event...
        e.ProgressPercentage ' first parameter passed to ReportProgress()
        e.UserState ' second parameter passed to ReportProgress() --> cast it with CType()
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ' ...this fires when the backgroundworker operation is complete...
    End Sub

End Class
Random Solutions  
 
programming4us programming4us