none
[VB 2005] 背景作業在各表單間傳遞的問題 RRS feed

  • 問題

  • 各位老師好

    首先
    建立一個模組 Public_x.vb
    在Public_x 裡

    #Region "BGW_Ready"
        Public WithEvents BGW_Ready As New System.ComponentModel.BackgroundWorker
        Public Run_Ready As Boolean
        Public Sub BGW_Ready_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGW_Ready.DoWork

        End Sub
        Public Sub BGW_Ready_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGW_Ready.RunWorkerCompleted
            If Run_Ready = True Then
                       .
                       .
                       .
                      (有控制到主表單控制項)
                    Application.DoEvents()
                       .
                       .
                       .
                BGW_Ready.RunWorkerAsync()
            End If
        End Sub
    #End Region

    然後我的主表單一Load的時候就啟動 BGW_Ready


    然後我再建立另一表單 Frm2

    在Frm2中我發現 BGW_Ready 該做的工作失效了(沒做)

    所以我再Frm2一Load 時又啟動了一次 BGW_Ready

    終於BGW_Ready 又恢復他該做的工作

    但是在我Frm2關閉的時候

     出現了不明的例外狀況如下:

    System.Reflection.TargetInvocationException 未處理
      Message="引動過程的目標傳回例外狀況。"
      Source="mscorlib"
      StackTrace:
           於 System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
           於 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
           於 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
           於 System.Delegate.DynamicInvokeImpl(Object[] args)
           於 System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
           於 System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
           於 System.Threading.ExecutionContext.runTryCode(Object userData)
           於 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           於 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           於 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           於 System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
           於 System.Windows.Forms.Control.InvokeMarshaledCallbacks()
           於 System.Windows.Forms.Control.WndProc(Message& m)
           於 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           於 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           於 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           於 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           於 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           於 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           於 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           於 System.Windows.Forms.Application.Run(ApplicationContext context)
           於 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           於 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           於 Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           於 AF1.My.MyApplication.Main(String[] Args) 於 17d14f5c-a337-4978-8281-53493378c1071.vb: 行 81
           於 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           於 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           於 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           於 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           於 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           於 System.Threading.ThreadHelper.ThreadStart()
    我的問題:

    1. 為什麼我已經在主表單 啟動了 BGW_Ready , 它在 Frm2中卻失效 ?
    2.該怎麼避免以上的例外狀況

    謝謝各位老師


    易耳易兒
    2009年8月20日 上午 04:21

解答

所有回覆