Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Пример активити
- public sealed class CodeActivity1 : NativeActivity
- {
- [RequiredArgument]
- public InArgument<DateTime> left_date { get; set; }
- [RequiredArgument]
- public OutArgument<bool> cancelProg { get; set; }
- protected override void Execute(NativeActivityContext context)
- {
- var d1 = left_date.Get(context);
- if(check)
- {
- }
- else
- {
- ICheckArgumentsNotifier ext = context.GetExtension<ICheckArgumentsNotifier>();
- ext.SendNotification("checkaArgs", d1, d2, "Input incorrect");
- context.CreateBookmark("checkaArgs", new BookmarkCallback(this.OnResumeBookmark));
- }
- }
- protected override bool CanInduceIdle
- {
- get { return true; }
- }
- public void OnResumeBookmark(NativeActivityContext context,Bookmark bookmark,object obj)
- {
- if (obj != null)
- { var dict = obj as Dictionary<string, object>;
- left_date_out.Set(context, dict["Date1"]);
- right_date_out.Set(context, dict["Date2"]);
- }
- }
- }
- //Нотифаер
- using System.Threading;
- namespace lab5
- { public interface ICheckArgumentsNotifier
- {
- void SendNotification(string bookmarkName, DateTime d1, DateTime d2, string message);
- }
- public class CheckArgumentsNotifier : ICheckArgumentsNotifier
- {
- public EventHandler<CheckArgumentsNotifierEventArgs> notify;
- public void SendNotification(string bookmarkName, DateTime d1, DateTime d2, string message)
- {
- if (notify != null)
- ThreadPool.QueueUserWorkItem(delegate (Object state)
- { notify(this, new CheckArgumentsNotifierEventArgs(bookmarkName, d1, d2, message)); }); }
- }
- public class CheckArgumentsNotifierEventArgs : EventArgs
- {
- public string BookmarkName { get; private set; }
- public DateTime D1 { get; private set; }
- public DateTime D2 { get; private set; }
- public string Msg { get; private set; }
- public CheckArgumentsNotifierEventArgs(string bname, DateTime d1, DateTime d2, string msg)
- {
- BookmarkName = bname;
- D1 = d1;
- D2 = d2;
- Msg = msg;
- } } }
- // runwfapp
- private void RunWFApp(DateTime date1, DateTime date2)
- {
- AutoResetEvent syncEvent = new AutoResetEvent(false);
- AutoResetEvent idleEvent = new AutoResetEvent(false);
- var inputs = new Dictionary<string, object>() { { "Left_date", date1 }, {"Right_date", date2 } };
- var reason = string.Empty;
- WorkflowApplication wfApp = new WorkflowApplication(new WF(), inputs)
- {
- Completed = delegate (WorkflowApplicationCompletedEventArgs ea)
- {
- if (ea.CompletionState == ActivityInstanceState.Faulted)
- reason = ea.TerminationException.Message;
- syncEvent.Set();
- },
- Aborted = delegate (WorkflowApplicationAbortedEventArgs ea)
- {
- syncEvent.Set();
- },
- OnUnhandledException = delegate (WorkflowApplicationUnhandledExceptionEventArgs ea)
- {
- return UnhandledExceptionAction.Terminate;
- }
- };
- CheckArgumentsNotifier notifier = new CheckArgumentsNotifier();
- notifier.notify += delegate (Object sender, CheckArgumentsNotifierEventArgs e)
- {
- var outputs = new Dictionary<string, object>();
- DialogResult dResult = MessageBox.Show(e.Msg, "Произошла ошибка", MessageBoxButtons.OKCancel);
- if (dResult == DialogResult.OK)
- {
- outputs.Add("Canceled", false);
- CorrectDataForm form = new CorrectDataForm();
- form.dateTimePicker1.Value = date1;
- form.dateTimePicker2.Value = date2;
- form.ShowDialog();
- if (form.success)
- {
- outputs.Add("Date1", dateTimePicker1.Value.Date);
- outputs.Add("Date2", dateTimePicker1.Value.Date);
- }
- else outputs["Canceled"] = true;
- wfApp.ResumeBookmark(e.BookmarkName, outputs);
- }
- else
- {
- outputs.Add("Canceled", true);
- wfApp.ResumeBookmark(e.BookmarkName, outputs);
- }
- };
- wfApp.Extensions.Add(notifier);
- wfApp.Run();
- syncEvent.WaitOne();
- if (reason != string.Empty) MessageBox.Show(reason);
- }
- //кнопка
- private async void button1_Click(object sender, EventArgs e)
- {
- if (this.button1.InvokeRequired)
- {
- this.BeginInvoke(new MethodInvoker(delegate
- {
- this.button1.Enabled = false;
- }));
- }
- else this.button1.Enabled = false;
- await Task.Factory.StartNew(() =>
- { RunWFApp(date1, date2);
- });
- MessageBox.Show("WF Закончен");
- }
Advertisement
Add Comment
Please, Sign In to add comment