1

다음은 UI 스레드에 의해 성공적으로 시작된 새로 만든 스레드 코드입니다 (UI 코드는 여기에 없음).워크 플로 예외가 잡히지 않는 이유는 무엇입니까?

디버깅을 할 때 나는이 코드를 사용합니다.

실행중인 워크 플로에 문제가없는 경우 코드가 완료 될 때까지 실행됩니다. 그러나 워크 플로에 결함이있는 경우 (잘못된 워크 플로를 사용하여 코드를 테스트하고 있음)이 코드는 아래의 wf.Run() 문에서 발생하는 WorkflowException을 catch하지 않습니다.

아래 워크 플로우 실행 예외 코드가 있다고 생각합니까 ?? 내가 뭘 잘못하고 있는지 말해 줄 수 있니? 감사.

public void ThreadRun() 
    { 
      AutoResetEvent syncEvent = new AutoResetEvent(false); 
      var wf = ActivityXamlServices.Load(fileInfo.FileName); 
      // Create the WorkflowApplication using the desired 
      // workflow definition. 
      WorkflowApplication wfApp = new WorkflowApplication(wf); 

      // Handle the desired lifecycle events. 
      wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e) 
      { 
       syncEvent.Set(); 
      }; 

      try 
      { 
       // Start the workflow. 
       wfApp.Run(); 
       // Wait for Completed to arrive and signal that 
       // the workflow is complete. 
       syncEvent.WaitOne(); 
      } 
      catch (Exception exception) 
      { 

       wfApp.Completed = delegate(WorkflowApplicationCompletedEventArgs e) 
       { 
        if (e.CompletionState == ActivityInstanceState.Faulted) 
        { 
         Console.WriteLine("Workflow {0} Terminated.", e.InstanceId); 
         Console.WriteLine("Exception: {0}\n{1}", 
          e.TerminationException.GetType().FullName, 
          e.TerminationException.Message); 
        } 
        else if (e.CompletionState == ActivityInstanceState.Canceled) 
        { 
         Console.WriteLine("Workflow {0} Canceled.", e.InstanceId); 
        } 
        else 
        { 
         Console.WriteLine("Workflow {0} Completed.", e.InstanceId); 

         // Outputs can be retrieved from the Outputs dictionary, 
         // keyed by argument name. 
         // Console.WriteLine("The winner is {0}.", e.Outputs["Winner"]); 
        } 
       }; 

       wfApp.Aborted = delegate(WorkflowApplicationAbortedEventArgs e) 
       { 
        // Display the exception that caused the workflow 
        // to abort. 
        Console.WriteLine("Workflow {0} Aborted.", e.InstanceId); 
        Console.WriteLine("Exception: {0}\n{1}", 
         e.Reason.GetType().FullName, 
         e.Reason.Message); 
       }; 

       wfApp.Idle = delegate(WorkflowApplicationIdleEventArgs e) 
       { 
        // Perform any processing that should occur 
        // when a workflow goes idle. If the workflow can persist, 
        // both Idle and PersistableIdle are called in that order. 
        Console.WriteLine("Workflow {0} Idle.", e.InstanceId); 
       }; 

       wfApp.PersistableIdle = delegate(WorkflowApplicationIdleEventArgs e) 
       { 
        // Instruct the runtime to persist and unload the workflow. 
        // Choices are None, Persist, and Unload. 
        return PersistableIdleAction.Unload; 
       }; 

       wfApp.Unloaded = delegate(WorkflowApplicationEventArgs e) 
       { 
        Console.WriteLine("Workflow {0} Unloaded.", e.InstanceId); 
       }; 

       wfApp.OnUnhandledException = delegate(WorkflowApplicationUnhandledExceptionEventArgs e) 
       { 
        // Display the unhandled exception. 
        Console.WriteLine("OnUnhandledException in Workflow {0}\n{1}", 
         e.InstanceId, e.UnhandledException.Message); 

        Console.WriteLine("ExceptionSource: {0} - {1}", 
         e.ExceptionSource.DisplayName, e.ExceptionSourceInstanceId); 

        // Instruct the runtime to terminate the workflow. 
        // Other choices are Abort and Cancel. Terminate 
        // is the default if no OnUnhandledException handler 
        // is present. 
        return UnhandledExceptionAction.Terminate; 
       }; 
      } 
     } 
+0

그런데 예외를 잡은 후에 대리인을 설정하는 이유는 무엇입니까? ... – Qnan

+0

try {wfApp.Run()) – user1298925

+0

전에 대리자를 설정하려고 시도했으며 어떤 결과가 나타 났습니까? – Qnan

답변

3

다른 스레드에서 예외가 발생했습니다. 다음을 확인하십시오 : catch exception that is thrown in different thread

다른 스레드에서 발생하는 예외는 기본적으로 호출자 스레드에 전달되지 않습니다.

편집 : 워크 플로가 시작되기 전에 대리인이 설정되기를 바랍니다. 그 점이 맞습니까? 그렇다면 wfApp.OnUnhandledException에 할당을 수행하십시오. 전에 wfApp.Run()을 수행하십시오.

+0

감사합니다. 다음 코드가 UI 스레드에 속한다는 말입니까? : wfApp.Completed = 대리인 (WorkflowApplicationCompletedEventArgs e) 기타 ... – user1298925

+0

예. 그것은 콜백을 설정하기 때문에 델리게이트 내부의 코드는 UI에서 실행되지 않습니다. 그것은 기본적으로 wfApp에 "UnhandledException'이있는 경우에이를 말하고 있습니다." – Qnan

+0

1) wfApp 전에 델리게이트를 설정해야했습니다.Run() – user1298925

0

캐치 전에 OnUnhandledException 이벤트를 처리해야한다고 생각합니다 (이 이벤트는 처리되지만 catch 블록에서 볼 수 있습니다).

wfApp.OnUnhandledException 오류를 catch 할 수 있습니다.

관련 문제