Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 RunWorkerCompleted Not Firing Consistently

$
0
0
I am trying to figure out why my background worker's RunWorkerCompleted event is not always firing. Executing this same exact code, sometimes the event fires and sometimes it does not. Any help would be greatly appreciated.

I wired up a button to call RefreshStudents.

vb.net Code:
  1. ''' <summary>
  2.     ''' Refreshes the student list based on the value of the student refresh flag.
  3.     ''' </summary>
  4.     Private Sub RefreshStudents()
  5.  
  6.         Try
  7.  
  8.             'if not already busy
  9.             If Not brwStudents.IsBusy Then
  10.  
  11.                 oStudentLookup.Clear()
  12.  
  13.                 'clear any existing rows
  14.                 grdStudents.PrimaryGrid.Rows.Clear()
  15.  
  16.                 'update the status
  17.                 sStatusStudentMGT = "Retrieving student records..." : UpdateStudentManagementFooter()
  18.  
  19.                 'set the now rows text
  20.                 grdStudents.PrimaryGrid.NoRowsText = "Retrieving student records..."
  21.  
  22.                 'set the wait cursor
  23.                 grdStudents.UseWaitCursor = True
  24.  
  25.                 'show progress
  26.                 pgbMain.Visible = True
  27.  
  28.                 'retrieve the list students
  29.                 brwStudents.RunWorkerAsync(Me)
  30.  
  31.             Else 'already busy
  32.  
  33.                 'cancel the current operation
  34.                 brwStudents.CancelAsync()
  35.  
  36.             End If 'if not already busy
  37.  
  38.         Catch ex As Exception
  39.  
  40.             'handle the exception
  41.             MsgBox(ex.Message)
  42.  
  43.         End Try
  44.  
  45.     End Sub
  46.  
  47. Private Sub brwStudents_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles brwStudents.DoWork
  48.  
  49.         Try
  50.  
  51.             Dim fMain As frmLoad = TryCast(e.Argument, frmLoad)
  52.  
  53.             'if we have a main form (UI thread) reference
  54.             If (fMain IsNot Nothing) Then
  55.  
  56.                 Dim oGrid As SuperGridControl = fMain.grdStudents
  57.                 Dim bResult As Boolean = True
  58.                 Dim oWorker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)
  59.  
  60.                 'loop thru all the students
  61.                 For iIndex As Integer = 1 To 10000 Step 1
  62.  
  63.                     'exit the loop if cancellation is pending
  64.                     If oWorker.CancellationPending Then e.Cancel = True : Exit For
  65.  
  66.                     'insert the student row
  67.                     If Not UpdateDataRowStudent(oGrid, "ID:" & CStr(iIndex), "ID:" & CStr(iIndex), "First:" & CStr(iIndex), "Last:" & CStr(iIndex), True, True) Then bResult = False : Exit For
  68.  
  69.                 Next iIndex
  70.  
  71.                 'set the result
  72.                 e.Result = bResult
  73.  
  74.             End If 'if we have a main form (UI thread) reference
  75.  
  76.         Catch ex As Exception
  77.  
  78.             'set the result to false on error (exception will be stored in the Error property)
  79.             e.Result = False
  80.  
  81.         End Try
  82.  
  83.     End Sub
  84.  
  85.  Private Sub brwStudents_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles brwStudents.RunWorkerCompleted
  86.  
  87.         Dim sErrorMessage As String = String.Empty
  88.  
  89.         Try
  90.  
  91.             'if no errors occurred during the request
  92.             If (e.Error Is Nothing) Then
  93.  
  94.                 'if we have a result
  95.                 If (e.Result IsNot Nothing) Then
  96.  
  97.                     'if the result was an exception
  98.                     If (TypeOf e.Result Is Exception) Then
  99.  
  100.                         'set the error message
  101.                         sErrorMessage = DirectCast(e.Result, Exception).Message
  102.  
  103.                     ElseIf Not CBool(e.Result) Then 'request failed for another reason
  104.  
  105.                         'set the error message
  106.                         sErrorMessage = "List students failed."
  107.  
  108.                     ElseIf CBool(e.Result) Then 'request was successful
  109.  
  110.                         Dim oGridPanel As GridPanel = grdStudents.PrimaryGrid
  111.  
  112.                         'if we have rows
  113.                         If (oGridPanel.Rows.Count > 0) Then
  114.  
  115.                             Dim oRow As GridRow = If(oGridPanel.ActiveRow Is Nothing, CType(oGridPanel.Rows(0), GridRow), CType(oGridPanel.ActiveRow, GridRow))
  116.  
  117.                             'select the 1st row
  118.                             oGridPanel.ClearSelectedRows() : oGridPanel.SetSelected(oRow, True) : oGridPanel.SetActiveRow(oRow)
  119.  
  120.                         End If 'if we have rows
  121.  
  122.                     End If 'if the result was an exception
  123.  
  124.                 Else 'unexpected result
  125.  
  126.                     'set the error message
  127.                     sErrorMessage = "List students failed."
  128.  
  129.                 End If 'if we have a result
  130.  
  131.             Else 'exception occurred
  132.  
  133.                 'set the error message
  134.                 sErrorMessage = e.Error.Message
  135.  
  136.             End If 'if no errors occurred during the request
  137.  
  138.         Catch ex As Exception
  139.  
  140.             'set the error message
  141.             sErrorMessage = ex.Message
  142.  
  143.         Finally
  144.  
  145.             'update the status
  146.             sStatusStudentMGT = String.Empty : UpdateStudentManagementFooter()
  147.  
  148.             'if no errors occurred during the request
  149.             If String.IsNullOrEmpty(sErrorMessage) Then
  150.  
  151.                 'reset the now rows text
  152.                 grdStudents.PrimaryGrid.NoRowsText = "No students"
  153.  
  154.             Else 'errors occurred during the request
  155.  
  156.                 'set the now rows text to the error message
  157.                 grdStudents.PrimaryGrid.NoRowsText = sErrorMessage
  158.  
  159.             End If 'if no errors occurred during the request
  160.  
  161.             'reset the wait cursor
  162.             grdStudents.UseWaitCursor = False
  163.  
  164.             'hide progress
  165.             pgbMain.Visible = False
  166.  
  167.         End Try
  168.  
  169.     End Sub
  170.  
  171.   Private Function UpdateDataRowStudent(ByRef oGrid As SuperGridControl, ByVal sGUID As String, ByVal sID As String, ByVal sFirstName As String, ByVal sLastName As String, ByVal bIsActive As Boolean, ByVal bIsNewRow As Boolean) As Boolean
  172.  
  173.         Try
  174.  
  175.             Dim bResult As Boolean
  176.  
  177.             'if we have a target grid
  178.             If (oGrid IsNot Nothing) Then
  179.  
  180.                 Dim oRow As GridRow
  181.                 Dim oPrimaryPanel As GridPanel = oGrid.PrimaryGrid
  182.  
  183.                 'if a new row is to be inserted into the grid
  184.                 If bIsNewRow Then
  185.  
  186.                     Dim sRowData(GetType(StudentDataColumns).GetEnumNames.Count - 1) As Object
  187.  
  188.                     'instantiate an empty student row
  189.                     oRow = New GridRow(sRowData)
  190.  
  191.                     'insert a new row (activate the row if an invoke is not required - new student)
  192.                     InsertRowData(oPrimaryPanel, oRow)
  193.  
  194.                 Else 'modify/update row
  195.  
  196.                     'reference the active student row
  197.                     oRow = CType(oPrimaryPanel.ActiveRow, GridRow)
  198.  
  199.                 End If 'if a new row is to be inserted into the grid
  200.  
  201.                 'set the reference to the student's GUID in the row tag
  202.                 oRow.Tag = sGUID
  203.  
  204.                 'store the student GUID
  205.                 UpdateCellData(oRow.Item(StudentDataColumns.GUID), sGUID)
  206.  
  207.                 'store the student ID
  208.                 UpdateCellData(oRow.Item(StudentDataColumns.PublicID), sID)
  209.  
  210.                 'store the student first name
  211.                 UpdateCellData(oRow.Item(StudentDataColumns.FirstName), sFirstName)
  212.  
  213.                 'store the student last name
  214.                 UpdateCellData(oRow.Item(StudentDataColumns.LastName), sLastName)
  215.  
  216.                 'store the active flag
  217.                 UpdateCellData(oRow.Item(StudentDataColumns.Active), bIsActive)
  218.  
  219.                 'set the result
  220.                 bResult = True
  221.  
  222.                 'if a new row is to be inserted into the grid
  223.                 If bIsNewRow Then
  224.  
  225.                     'add the student to the lookup table
  226.                     If Not oStudentLookup.ContainsKey(sGUID) Then oStudentLookup.Add(sGUID, (sFirstName & " " & sLastName).Trim)
  227.  
  228.                 Else 'modify/update row
  229.  
  230.                     'update the student to the lookup table
  231.                     If oStudentLookup.ContainsKey(sGUID) Then oStudentLookup(sGUID) = (sFirstName & " " & sLastName).Trim
  232.  
  233.                 End If 'if a new row is to be inserted into the grid
  234.  
  235.                 'if in worker thread
  236.                 If Not oGrid.InvokeRequired Then
  237.  
  238.                     'perform any layout logic (required in order to update the selection)
  239.                     oGrid.ArrangeGrid()
  240.  
  241.                     'select the updated row
  242.                     oPrimaryPanel.ClearSelectedRows() : oPrimaryPanel.SetSelected(oRow, True) : oPrimaryPanel.SetActiveRow(oRow)
  243.  
  244.                 End If 'if in worker thread
  245.  
  246.             End If 'if we have a target grid
  247.  
  248.             'return the result
  249.             Return bResult
  250.  
  251.         Catch ex As Exception
  252.  
  253.             'throw the exception
  254.             Throw
  255.  
  256.         End Try
  257.  
  258.     End Function
  259.  
  260.  ''' <summary>
  261.     ''' Thread-safe delegate used to handle the updating of a grid cell's data.
  262.     ''' </summary>
  263.     ''' <param name="oCell">A reference to the target cell.</param>
  264.     ''' <param name="oData">The cell data.</param>
  265.     Private Sub UpdateCellData(ByVal oCell As GridCell, ByVal oData As Object)
  266.  
  267.         Try
  268.  
  269.             'if we have a cell
  270.             If (oCell IsNot Nothing) Then
  271.  
  272.                 'if must invoke the method
  273.                 If (oCell.SuperGrid IsNot Nothing) AndAlso oCell.SuperGrid.InvokeRequired Then
  274.  
  275.                     'invoke the method on the UI thread
  276.                     oCell.SuperGrid.BeginInvoke(New UpdateCellDataDelegate(AddressOf UpdateCellData), oCell, oData)
  277.  
  278.                 Else 'invoke not required
  279.  
  280.                     'set the value
  281.                     oCell.Value = If((oData Is Nothing), String.Empty, oData)
  282.  
  283.                 End If 'if must invoke the method
  284.  
  285.             End If 'if we have a cell
  286.  
  287.         Catch ex As Exception
  288.  
  289.             'handle the exception
  290.             MsgBox(ex.Message)
  291.  
  292.         End Try
  293.  
  294.     End Sub
  295.  
  296.  
  297.     ''' <summary>
  298.     ''' Delegate used to handle the updating of a grid cell's data.
  299.     ''' </summary>
  300.     ''' <param name="oCell">A reference to the target cell.</param>
  301.     ''' <param name="eColor">The text color of the cell.</param>
  302.     Private Sub UpdateCellColor(ByVal oCell As GridCell, ByVal eColor As Drawing.Color)
  303.  
  304.         Try
  305.  
  306.             'if we have a cell, set the color
  307.             If (oCell IsNot Nothing) Then oCell.CellStyles.ReadOnly.TextColor = eColor : oCell.CellStyles.Default.TextColor = eColor
  308.  
  309.         Catch ex As Exception
  310.  
  311.             'handle the exception
  312.             Throw
  313.  
  314.         End Try
  315.  
  316.     End Sub
  317.  
  318.     ''' <summary>
  319.     ''' Thread-safe delegate used to handle the insertion of rows into a grid panel.
  320.     ''' </summary>
  321.     ''' <param name="oGridPanel">A reference to the target grid panel.</param>
  322.     ''' <param name="oData">A reference to the data row.</param>
  323.     Private Sub InsertRowData(ByVal oGridPanel As GridPanel, ByVal oData As GridRow)
  324.  
  325.         Try
  326.  
  327.             'if we have a grid panel and data
  328.             If (oGridPanel IsNot Nothing) AndAlso (oData IsNot Nothing) Then
  329.  
  330.                 'if an invoke is required
  331.                 If oGridPanel.SuperGrid.InvokeRequired Then
  332.  
  333.                     'invoke the method on the UI thread
  334.                     oGridPanel.SuperGrid.BeginInvoke(New InsertRowDataDelegate(AddressOf InsertRowData), oGridPanel, oData)
  335.  
  336.                 Else 'on UI thread
  337.  
  338.                     'add the data to the collection
  339.                     oGridPanel.Rows.Add(oData)
  340.  
  341.                 End If 'if an invoke is required
  342.  
  343.             End If 'if we have a grid panel and data
  344.  
  345.         Catch ex As Exception
  346.  
  347.             'handle the exception
  348.             Throw
  349.  
  350.         End Try
  351.  
  352.     End Sub

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>