traceonelast

Untitled

Apr 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.68 KB | None | 0 0
  1. /* Пример активити
  2. public sealed class CodeActivity1 : NativeActivity
  3.     {
  4.         [RequiredArgument]
  5.         public InArgument<DateTime> left_date { get; set; }
  6.         [RequiredArgument]
  7.         public OutArgument<bool> cancelProg { get; set; }  
  8.         protected override void Execute(NativeActivityContext context)
  9.         {              
  10.             var d1 = left_date.Get(context);
  11.             if(check)
  12.             {          
  13.             }
  14.             else
  15.             {
  16.                 ICheckArgumentsNotifier ext = context.GetExtension<ICheckArgumentsNotifier>();
  17.                 ext.SendNotification("checkaArgs", d1, d2, "Input incorrect");
  18.                 context.CreateBookmark("checkaArgs", new BookmarkCallback(this.OnResumeBookmark));
  19.             }
  20.         }
  21.         protected override bool CanInduceIdle
  22.         {
  23.             get { return true; }
  24.         }
  25.         public void OnResumeBookmark(NativeActivityContext context,Bookmark bookmark,object obj)
  26.         {
  27.             if (obj != null)
  28.             {   var dict = obj as Dictionary<string, object>;                          
  29.                 left_date_out.Set(context, dict["Date1"]);
  30.                 right_date_out.Set(context, dict["Date2"]);                              
  31.             }
  32.         }
  33.     }
  34. //Нотифаер
  35. using System.Threading;
  36.     namespace lab5
  37.     {   public interface ICheckArgumentsNotifier
  38.         {
  39.             void SendNotification(string bookmarkName, DateTime d1, DateTime d2, string message);
  40.         }
  41.         public class CheckArgumentsNotifier : ICheckArgumentsNotifier
  42.         {
  43.             public EventHandler<CheckArgumentsNotifierEventArgs> notify;
  44.  
  45.             public void SendNotification(string bookmarkName, DateTime d1, DateTime d2, string message)
  46.             {
  47.                 if (notify != null)
  48.                     ThreadPool.QueueUserWorkItem(delegate (Object state)
  49.                     { notify(this, new CheckArgumentsNotifierEventArgs(bookmarkName, d1, d2, message)); }); }
  50.         }
  51.         public class CheckArgumentsNotifierEventArgs : EventArgs
  52.         {
  53.             public string BookmarkName { get; private set; }
  54.             public DateTime D1 { get; private set; }
  55.             public DateTime D2 { get; private set; }
  56.             public string Msg { get; private set; }
  57.             public CheckArgumentsNotifierEventArgs(string bname, DateTime d1, DateTime d2, string msg)
  58.             {
  59.                 BookmarkName = bname;
  60.                 D1 = d1;
  61.                 D2 = d2;
  62.                 Msg = msg;
  63.             }        }    }
  64.  
  65.  
  66. // runwfapp
  67. private void RunWFApp(DateTime date1, DateTime date2)
  68.         {
  69.             AutoResetEvent syncEvent = new AutoResetEvent(false);
  70.             AutoResetEvent idleEvent = new AutoResetEvent(false);
  71.             var inputs = new Dictionary<string, object>() { { "Left_date", date1 }, {"Right_date", date2 } };
  72.             var reason = string.Empty;
  73.             WorkflowApplication wfApp = new WorkflowApplication(new WF(), inputs)
  74.             {
  75.                 Completed = delegate (WorkflowApplicationCompletedEventArgs ea)
  76.                 {
  77.                     if (ea.CompletionState == ActivityInstanceState.Faulted)
  78.                         reason = ea.TerminationException.Message;
  79.                     syncEvent.Set();
  80.                 },
  81.                 Aborted = delegate (WorkflowApplicationAbortedEventArgs ea)
  82.                 {
  83.                     syncEvent.Set();
  84.                 },
  85.                 OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs ea)
  86.                 {
  87.                     return UnhandledExceptionAction.Terminate;
  88.                 }
  89.             };
  90.             CheckArgumentsNotifier notifier = new CheckArgumentsNotifier();
  91.             notifier.notify += delegate (Object sender, CheckArgumentsNotifierEventArgs e)
  92.             {
  93.                 var outputs = new Dictionary<string, object>();
  94.                 DialogResult dResult = MessageBox.Show(e.Msg, "Произошла ошибка", MessageBoxButtons.OKCancel);
  95.                 if (dResult == DialogResult.OK)
  96.                 {
  97.                     outputs.Add("Canceled", false);
  98.                     CorrectDataForm form = new CorrectDataForm();
  99.                     form.dateTimePicker1.Value = date1;
  100.                     form.dateTimePicker2.Value = date2;  
  101.                     form.ShowDialog();
  102.                     if (form.success)
  103.                     {
  104.                         outputs.Add("Date1", dateTimePicker1.Value.Date);
  105.                         outputs.Add("Date2", dateTimePicker1.Value.Date);                      
  106.                     }
  107.                     else outputs["Canceled"] = true;
  108.                     wfApp.ResumeBookmark(e.BookmarkName, outputs);
  109.                 }
  110.                 else
  111.                 {
  112.                     outputs.Add("Canceled", true);
  113.                     wfApp.ResumeBookmark(e.BookmarkName, outputs);
  114.                 }
  115.             };
  116.             wfApp.Extensions.Add(notifier);
  117.             wfApp.Run();
  118.             syncEvent.WaitOne();
  119.             if (reason != string.Empty) MessageBox.Show(reason);
  120.         }
  121.  
  122. //кнопка
  123. private async void button1_Click(object sender, EventArgs e)
  124.         {
  125.             if (this.button1.InvokeRequired)
  126.             {
  127.                 this.BeginInvoke(new MethodInvoker(delegate
  128.                 {
  129.                     this.button1.Enabled = false;
  130.                 }));
  131.             }
  132.             else this.button1.Enabled = false;
  133.             await Task.Factory.StartNew(() =>
  134.             { RunWFApp(date1, date2);
  135.             });
  136.             MessageBox.Show("WF Закончен");
  137.         }
Advertisement
Add Comment
Please, Sign In to add comment