none
WF4.0异常通知客户端 RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Activities;
    using WorkflowLibrary;
    using System.Activities.DurableInstancing;
    using System.Runtime.DurableInstancing;
    using System.Threading;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
            WorkflowApplication instance = null;
            List<User> us = new List<User>();
            StringBuilder bu = new StringBuilder();
            SqlWorkflowInstanceStore instanceStore;
            Guid GUID;
            InstanceView view;
            private void btnStrart_Click(object sender, RoutedEventArgs e)
            {
                User uu = new User();
                uu.argsName = "11111";
                uu.UserID = "wss";
                us.Add(uu);
                User uus = new User();
                uus.argsName = "22222";
                uus.UserID = "zq";
                us.Add(uus);
                User uuss = new User();
                uuss.argsName = "Accounting";
                uuss.UserID = "caiwu";
                us.Add(uuss);
                User uusss = new User();
                uusss.argsName = "1";
                uusss.UserID = "1";
                us.Add(uusss);
    
    
                System.Collections.Generic.Dictionary<string, object> dictionary = new Dictionary<string, object>();
                //dictionary.Add("BookMarkList", nameList);
                dictionary.Add("Accounting", "Accounting");
                dictionary.Add("InputValue", 1200);
                dictionary.Add("argument1", "11111");
                dictionary.Add("argument2", "22222");
                //dictionary.Add("UserName", us);
                instance = new WorkflowApplication(new WorkflowLibrary.Workflow1(), dictionary);
                if (instanceStore == null)
                {
                    string connectionString = "Data Source=192.168.1.222,46122;Initial Catalog=WorkflowInstanceStore;Integrated Security=True";
                    instanceStore = new SqlWorkflowInstanceStore(connectionString);
                    view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));
                    instanceStore.DefaultInstanceOwner = view.InstanceOwner;
                }
                instance.InstanceStore = instanceStore;
                instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
                instance.OnUnhandledException = unhandledExceptionl;
                instance.Aborted = aborted;
                instance.Idle = workflowIdel;
                instance.PersistableIdle = persistableIdle;
                instance.Unloaded = unload;
                GUID = instance.Id;
                instance.Completed = workflowCompleted;
                instance.Run();
                bu.AppendLine("流程已经启动");
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
    
            }
    
    
            void unload(WorkflowApplicationEventArgs e)
            {
                bu.AppendLine("unload:" + e.InstanceId);
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
            }
    
            void workflowCompleted(WorkflowApplicationCompletedEventArgs e)
            {
                instance = null;
                bu.AppendLine("workflowCompleted:" + e.CompletionState.ToString());
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
            }
    
            void aborted(WorkflowApplicationAbortedEventArgs e)
            {
                instance = null;
                bu.AppendLine("aborted ,Reason:" + e.Reason.Message);
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
            }
    
            UnhandledExceptionAction unhandledExceptionl(WorkflowApplicationUnhandledExceptionEventArgs e)
            {
                bu.AppendLine("unhandledException:" + e.UnhandledException.Message);
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                return UnhandledExceptionAction.Cancel;
            }
            void workflowIdel(WorkflowApplicationIdleEventArgs e)
            {
                bu.AppendLine("workflowIdel:" + e.InstanceId);
    
                foreach (var v in e.Bookmarks)
                {
                    txtMinu.Dispatcher.Invoke(new Action(delegate { workerValue.Content = v.BookmarkName; }), null);
                }
                txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
            }
            PersistableIdleAction persistableIdle(WorkflowApplicationIdleEventArgs e)
            {
    
                System.Console.WriteLine("persistableIdle:{0}", e.InstanceId);
    
    
                return PersistableIdleAction.Unload;
            }
    
            private void btnApp_Click(object sender, RoutedEventArgs e)
            {
    
                instance = new WorkflowApplication(new WorkflowLibrary.Workflow1());
                instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
                instance.OnUnhandledException = unhandledExceptionl;
                instance.InstanceStore = instanceStore;
                instance.Aborted = aborted;
                instance.Idle = workflowIdel;
                instance.PersistableIdle = persistableIdle;
                instance.Unloaded = unload;
                instance.Completed = workflowCompleted;
                instance.Load(GUID);
    
                string bookName = workerValue.Content.ToString();
                string inputValue = txtValue.Text;
    
                if (instance != null)
                {
                    if (instance.GetBookmarks().Count > 0 && instance.GetBookmarks()[0].BookmarkName == bookName)
                    {
                        BookmarkResumptionResult bk = instance.ResumeBookmark(bookName, inputValue);
                        txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                        bu.AppendLine(bookName + "已经审批通过");
                        txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                    }
                    else
                    {
                        bu.AppendLine(bookName + "没有审批通过");
                        txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                        if (instance.GetBookmarks().Count > 0)
                        {
                            bu.AppendLine("由以下人员审批");
                        }
                        foreach (var v in instance.GetBookmarks())
                        {
                            bu.AppendLine(v.BookmarkName);
                        }
                        txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                    }
                }
                else
                {
                    bu.AppendLine(bookName + "没有创建实例");
                    txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                }
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                instance = new WorkflowApplication(new WorkflowLibrary.Workflow1());
                instance.Completed = new Action<WorkflowApplicationCompletedEventArgs>(workflowCompleted);
                instance.OnUnhandledException = unhandledExceptionl;
                instance.InstanceStore = instanceStore;
                instance.Aborted = aborted;
                instance.Idle = workflowIdel;
                instance.PersistableIdle = persistableIdle;
                instance.Unloaded = unload;
                instance.Completed = workflowCompleted;
                instance.Load(GUID);
                foreach (var v in instance.GetBookmarks())
                {
                    bu.AppendLine("1111111111111111:" + v.BookmarkName);
                    txtMinu.Dispatcher.Invoke(new Action(delegate { txtMinu.Content = bu.ToString(); }), null);
                }
            }
    
        }
    }
    
    

     目前出现异常,是直接显示出来了

     异步线程提示错误。在主线程上捕获异常却什么也抓不到,请问下有什么好的解决方案吗,能让用户知道系统出错了。

    2011年12月14日 9:32

答案