Results 1 to 2 of 2

Thread: Running a new form as a backgroundworker.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2008
    Location
    Norway
    Posts
    24

    Running a new form as a backgroundworker.

    Hi. (VS 2008)

    I have created a application that creates a windows schedule task that connects to a FTP server and uploads/synchronize selected files and folders from your computer. After the user has selected all needed information and click continue, a form with a progressbar (Marquee) should appear. The application will then perform different tasks needed for the schedule to be completed. When all needed tasks are finished i want the progressForm to close and the main form will show you a summary of the newly created task.

    I run the tasks in the main thread and assign a backgroundworker for the porgressFrom. i have have also tried to run the taks in a backgroundworker.

    Button Continue Click Event. Code:
    1. Private Sub btn_Continue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Continue.Click
    2.  
    3.         Try
    4.             If ShowProcessFormBackgroundWorker.IsBusy Then
    5.                 ShowProcessFormBackgroundWorker.CancelAsync()
    6.             End If
    7.         Catch ex As Exception
    8.  
    9.         End Try
    10.  
    11.         ShowProcessFormBackgroundWorker = New BackgroundWorker
    12.         ShowProcessFormBackgroundWorker.WorkerReportsProgress = True
    13.         ShowProcessFormBackgroundWorker.WorkerSupportsCancellation = True
    14.         ShowProcessFormBackgroundWorker.RunWorkerAsync()
    15.  
    16.  
    17.         ' Checks that the name field, a schedule event and days (if needed) are set.
    18.         If txb_Name.Text = Nothing Then
    19.             MessageBox.Show("Please give your schedule a valid name", _
    20.                             "Name field empty", MessageBoxButtons.OK, MessageBoxIcon.Information)
    21.             Exit Sub
    22.         End If
    23.  
    24.         Select Case cmb_Schedule_Type.Text
    25.             Case ""
    26.                 MessageBox.Show("Please select a schedule type from the schedule menu", _
    27.                                 "Schedule type not selected", MessageBoxButtons.OK, MessageBoxIcon.Information)
    28.                 Exit Sub
    29.             Case "Run once", "Daily", "Run every 14 days"
    30.                 ' Do nothing
    31.             Case Else
    32.  
    33.                 ' Checks that at least one day is selected (if needed).
    34.                 Dim CheckState As New ArrayList
    35.                 For Each item As CheckBox In Panel_Days.Controls
    36.                     CheckState.Add(item.Checked.ToString)
    37.                 Next
    38.  
    39.                 If Not CheckState.Contains("True") Then
    40.                     MessageBox.Show("Please select at least one day from the schedule panel", _
    41.                                     "No days selected", MessageBoxButtons.OK, MessageBoxIcon.Information)
    42.                     Exit Sub
    43.                 End If
    44.  
    45.         End Select
    46.  
    47.         ' Adds all selected files and folders to the Collection Arrays.
    48.         Dim Temptree As TreeView = New TreeView
    49.         Temptree = ExplorerTree
    50.         Dim Nodelist As List(Of TreeNode) = FlattenDepth(Temptree)
    51.  
    52.         MainForm.arrFileCollection.Clear()
    53.         MainForm.arrFolderCollection.Clear()
    54.  
    55.         For Each node As TreeNode In Nodelist
    56.             If node.Checked = True AndAlso node.Text IsNot Nothing AndAlso node.Name IsNot Nothing AndAlso _
    57.                node.Text IsNot "[EMPTY]" AndAlso node.Name IsNot "[EMPTY]" Then
    58.                 If node.Tag = "File" Then
    59.                     If node.Parent.Checked = False Then
    60.                         MainForm.arrFileCollection.Add(node.Name)
    61.                     End If
    62.                 ElseIf node.Tag = "Folder" Then
    63.                     MainForm.arrFolderCollection.Add(node.Name)
    64.                 End If
    65.             End If
    66.         Next
    67.  
    68.         ' Checks that at least one file or folder are selected.
    69.         If MainForm.arrFileCollection.Count = 0 AndAlso MainForm.arrFolderCollection.Count = 0 Then
    70.             MessageBox.Show("Please select at least one file or folder from the filebrowser", _
    71.                             "No file or folder selected", MessageBoxButtons.OK, MessageBoxIcon.Information)
    72.             Exit Sub
    73.         End If
    74.  
    75.         ' Verifies that the FTP Connection is OK, if not show the settings panel and exit the sub.
    76.         If Not FTP_Connection_Test(MainForm.FTP_Hostname, DeCrypt(ReadSingleNode("username", MainForm.strFilePathConfig)) _
    77.                                    , DeCrypt(ReadSingleNode("password", MainForm.strFilePathConfig)), MainForm.FTP_port) = "True" Then
    78.  
    79.             MessageBox.Show("Please enter a valid username and password and run the 'test connection' again", _
    80.                             "Connection failed", MessageBoxButtons.OK, MessageBoxIcon.Stop)
    81.             AddSingleNode("ftp_check_ok", "False", MainForm.strFilePathConfig)
    82.             SettingsForm.ShowDialog()
    83.             Exit Sub
    84.         End If
    85.  
    86.         ' Sets counters variables.
    87.         Dim lngTotalSize As Long = 0
    88.         Dim intFileCount As Integer = 0
    89.         Dim intFolderCount As Integer = 0
    90.  
    91.         ' loops thru all folders in the foldercollection and gets the total size and file/folder count.
    92.         For Each item As String In MainForm.arrFolderCollection
    93.             lngTotalSize = lngTotalSize + GetTotalSize(item.ToString, True)
    94.             intFileCount = intFileCount + CountFiles(item.ToString)
    95.             intFolderCount = intFolderCount + CountFolders(item.ToString)
    96.         Next
    97.  
    98.         ' Format total size to B, KB, MB or GB.
    99.         Dim strTotalSize As String = Nothing
    100.  
    101.         If lngTotalSize < 1000 Then
    102.             strTotalSize = lngTotalSize.ToString & " Bytes"
    103.         ElseIf lngTotalSize > 1000 And lngTotalSize < 1000000 Then
    104.             strTotalSize = CType(lngTotalSize.ToString / 1024, Integer) & " KB"
    105.         ElseIf lngTotalSize > 1000000 And lngTotalSize < 1000000000 Then
    106.             strTotalSize = CType(lngTotalSize.ToString / 1048576, Integer) & " MB"
    107.         Else
    108.             strTotalSize = CType(lngTotalSize.ToString / 1073741824, Integer) & " GB"
    109.         End If
    110.  
    111.         ' Sets the finish panel lables.
    112.         lbl_Job_name.Text = lbl_Job_name.Text & " " & txb_Name.Text
    113.         lbl_Total_size.Text = lbl_Total_size.Text & " " & strTotalSize & " " & "(" & CType((lngTotalSize / 1024), Integer).ToString & " KB)"
    114.         lbl_Number_of_folders.Text = lbl_Number_of_folders.Text & " " & intFolderCount.ToString
    115.         lbl_Number_of_files.Text = lbl_Number_of_files.Text & " " & intFileCount.ToString
    116.  
    117.         ' Runs the SaveScheduleData sub.
    118.         SaveScheduleData()
    119.  
    120.         Try
    121.             If ShowProcessFormBackgroundWorker.IsBusy Then
    122.                 ShowProcessFormBackgroundWorker.CancelAsync()
    123.             End If
    124.         Catch ex As Exception
    125.         End Try
    126.  
    127.         ' Show the Finish Panel
    128.         Panel_Start.Enabled = False
    129.         Panel_Start.Visible = False
    130.         Panel_Finish.Enabled = True
    131.         Panel_Finish.Visible = True
    132.  
    133.     End Sub

    I know that i should most likely run the schedule taks in a backgroundworker (long running process) and the progressForm in the main thread. But i still can't get the progress from to close when the schedule tasks are completed. The .CancelAsync will not kill the backgroundworker until some time has passed.

    Background worker DoWork Event Code:
    1. ' ShowProcessFormBackgroundWorker DoWork Event.
    2.     Private Sub ShowProcessFormBackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles ShowProcessFormBackgroundWorker.DoWork
    3.         If ShowProcessFormBackgroundWorker.CancellationPending = True Then
    4.             ProgressForm.Close()
    5.             e.Cancel = True
    6.         End If
    7.         ProgressForm.ShowDialog()
    8.     End Sub

    Some pictures of the UI.
    Attached Images Attached Images   
    Last edited by StianR; Aug 6th, 2009 at 06:20 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,949

    Re: Running a new form as a backgroundworker.

    You don't close the form in the DoWork event handler. The point with the BackgroundWorker is that the DoWork event handler is ONLY for the background task and ALL interaction with the UI is done in the ProgressChanged event handler and the RunWorkerCompleted event handler. You want to close the form when the background task completes so you do so in the RunWorkerCompleted event handler.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width