Hi Experts,
I am having a ultrawingrid which is used to display the filepaths in column1 and progressbar in column2 i want to import the file from the path (displayed in col1) to the database and while inserting the records i have to show the percentage in progressbar column.. For this purpose i have used Background worker with methodinvokers. It works correctly for 8 out of 10 times. But suddenly its showing an unhandled error and my ultrawingrid is marked with a big 'X' mark.. what may be the problem. i have used try catch too but i cant handle that error...
.
A MethodInvoker does not implicitly invoke execution on the UI thread. You cannot make changes to any controls or the datasource your grid is bound to from anywhere other than the UI thread.
See Control.Invoke on MSDN for more info: http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx
The Following is the place where i am getting this error
Try If (Me.InvokeRequired) Then 'Me.Invoke(New MethodInvoker(AddressOf chk1, i)) Me.Invoke(New MethodInvoker(Function() ' Define a handler for unhandled exceptions for threads behind forms. AddHandler Application.ThreadException, AddressOf MYThreadHandler Try Select Case Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(0).Value.ToString() Case "BOM Parts List" Me.grdFilesUploadProgress.DisplayLayout.Bands(0).Columns(2).MinValue = 0 'Code to Process BOM Parts List files Dim ObjBOM As New clsImportMachineBOM(intOrganisationId) ObjBOM.DeleteInputMachineBOMData(3) clcmaxval_rows = ObjBOM.FnBulkCopyFromCSVToDB(Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(1).Value.ToString()) 'Setting the maximum value for the Progressbar If clcmaxval_rows.Count > 0 Then Me.grdFilesUploadProgress.DisplayLayout.Bands(0).Columns(2).MaxValue = clcmaxval_rows(1) intRowsAffcted = clcmaxval_rows(2) End If Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(2).Value = intRowsAffcted Application.DoEvents() ' ObjBOM = Nothing Case "Production Plan", "Install Base" Me.grdFilesUploadProgress.DisplayLayout.Bands(0).Columns(2).MinValue = 0 Me.grdFilesUploadProgress.DisplayLayout.Bands(0).Columns(2).MaxValue = dtinfotbl.Rows.Count For Each dtrow In dtinfotbl.Rows intProdId = objCommon.GetProductId(dtrow(0), intOrganisationId, 1) 'Code to Process ProductionPlan type of files If Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(0).Value.ToString() = "Production Plan" Then Dim objProdPlanImport As New clsImportProductionPlan(intOrganisationId, CType(dtrow(1), Integer), CType(dtrow(2).ToString().Trim(), Integer), intProdId, CType(dtrow(3), Integer)) blnresult = objProdPlanImport.fnImportProductionPlan(2) If blnresult = True Then intRowsAffcted += 1 End If Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(2).Value = intRowsAffcted Application.DoEvents() 'objProdPlanImport = Nothing 'Code to Process Installbase type of files ElseIf Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(0).Value.ToString() = "Install Base" Then Dim ObjInstallBase As New ClsImportInstallBase(intOrganisationId, intProdId, CType(dtrow(1).ToString(), Integer), CType(dtrow(2).ToString.Trim(), Integer)) blnresult = ObjInstallBase.fnImportInstallBase(2) If blnresult = True Then intRowsAffcted += 1 End If Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(2).Value = intRowsAffcted Application.DoEvents() 'ObjInstallBase = Nothing End If If blnClosed = True Then End End If Next End Select 'Threading.Thread.Sleep(500) Me.grdFilesUploadProgress.DisplayLayout.Rows(UGrow.Index).Cells(2).Style = ColumnStyle.CheckBox Me.grdFilesUploadProgress.Rows(UGrow.Index).Cells(2).Value = True blnSuccess = True Return Nothing Catch ex As Exception blnSuccess = False fnImportByRow_Ex(UGrow.Index, intRowsAffcted.ToString() & " Row Completed.. Error Loading file. Refer to Log File") Return Nothing End Try End Function)) End If Return blnSuccess Catch ex As Exception Throw ex End Try
From what event is this code being called?
The exception you are getting here is almost certainly a threading issue.
As I mentioned (several times) in the other thread you posted, you cannot mix DataBinding and multiple threads. Your use of InvokeRequired and BeginInvoke here will not help, because there is all sorts of communication going on between the grid, the BindingManager, and the data source over which you have no control.
i have written this code in a seperate function and i have called this function within backgroundworker_dowork() event.....
Yes Mike. i understood what you r saying. So please give me any alternative solution and how to do it please..... and also please send me any samples
Okay, I'm really confused. If this method is called on a background worker and you are checking InvokeRequired, then this will always be true and you are marshalling this method back to the UI Thread.
So the thread is completely pointless. You are doing everything on the UI Thread, anyway. Unless there is some other code being run on the background thread.
As for how to do this the correct way, I would just ditch the threading altogether and update the progress bar cells synchronously. If you must do it asynchronously, then I would recommend using a Windows Forms Timer, which operates on the UI Thread.