[2005] exception in Backgroundwork


我用backgroundworker运行一个线程,现程中执行的是打开一系列文件,写入东西,关闭。第一次运行,一切良好。第二次运行开始出现exception
I use backgroundworker for a thread. Inside of the thread I opens some files write in and close tham. It works fine,but an exception will be popup in the 2nd time.

A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in System.Windows.Forms.dll

VB Code: Private Sub ProcessMode1(ByVal arr(,) As String, ByVal sFilePath As String, ByVal e As System.ComponentModel.DoWorkEventArgs) Dim i As Integer, a As System.IO.StreamWriter, iMax As Integer '--if not existed we creat a new one If My.Computer.FileSystem.FileExists(sFilePath) = False Then System.IO.File.Open(sFilePath, IO.FileMode.CreateNew).Close() End If a = My.Computer.FileSystem.OpenTextFileWriter(sFilePath, True) '--New processing start a.WriteLine() a.WriteLine("Process Start") a.WriteLine() iMax = UBound(arr, 2) '--需要打开文件的路径数组 For i = iCurrentIndex To iMax a.WriteLine("File Name: " & arr(1, i)) a.WriteLine("Modify Time: " & Format(Now, "dd/MM/yyyy HH:mm:ss")) a.WriteLine("----------------------------------------------------") sCurrentFileName = arr(1, i) ' ReflashForm() bgWorker.ReportProgress(i) 'calling bgWorker_ProgressChanged If ThreadInterrupt(i, e) = True Then 'check whether pause or interrupt.( exception is here) iCurrentIndex = i + 1 'Get the interrupt point If bPause = True Then a.WriteLine("Suspend") a.Close() acadapp.Quit() acadapp = Nothing Exit Sub End If Next i iCurrentIndex = 0 a.WriteLine("Finished") a.Close() End Sub 'I have 2 buttons one for Pause/Resume, the other one for Stop. Pause: in the thread, finish current file ,keep the file number ,exit sub. Resume: start thread from the first beginning, loop from the last exit number.

'inside of the button click
bgWorker.CancelAsync()

'Function for checking pause stop

VB Code: Private Function ThreadInterrupt(ByVal iTempIndex As Integer, ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean If bgWorker.CancellationPending = True Then If bCancel = True Then If MessageBox.Show("Do you want to stop this process", "Automated Process", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then iCurrentIndex = 0 '--Refresh from bgWorker.ReportProgress(-1) End If End If If bPause = True Then iCurrentIndex = iTempIndex End If e.Cancel = True ThreadInterrupt = True End If End Function

stacktrace.

System.Reflection.TargetInvocationException was unhandled
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Posted On: Saturday 3rd of November 2012 12:48:42 PM Total Views:  229
View Complete with Replies