public MultithreadingSample() { int r = 0; var tbl = new Table(); PackStart(tbl); tbl.Add(new Label("Application.Invoke:"), 0, r); tbl.Add(l1 = new Label("0"), 1, r); tbl.Add(btn1 = new ToggleButton("Run"), 2, r++); Task worker1; CancellationTokenSource worker1cancel = new CancellationTokenSource();; btn1.Toggled += delegate { if (btn1.Active) { worker1cancel = new CancellationTokenSource(); worker1 = Task.Factory.StartNew(delegate { int cnt = 0; while (!worker1cancel.Token.IsCancellationRequested) { Thread.Sleep(50); cnt++; string txt = cnt.ToString(); Application.Invoke(() => l1.Text = txt); } }, worker1cancel.Token, TaskCreationOptions.LongRunning); } else { worker1cancel.Cancel(); } }; tbl.Add(new Label("Application.UITaskScheduler:"), 0, r); tbl.Add(l2 = new Label("0"), 1, r); tbl.Add(btn2 = new ToggleButton("Run"), 2, r++); Task worker2; CancellationTokenSource worker2cancel = new CancellationTokenSource();; btn2.Toggled += delegate { if (btn2.Active) { worker2cancel = new CancellationTokenSource(); worker2 = Task.Factory.StartNew(delegate { int cnt = 0; while (!worker2cancel.Token.IsCancellationRequested) { Thread.Sleep(50); cnt++; var txt = cnt.ToString(); Task.Factory.StartNew(delegate { l2.Text = txt; }, worker2cancel.Token, TaskCreationOptions.None, Application.UITaskScheduler); } }, worker2cancel.Token, TaskCreationOptions.LongRunning); } else { worker2cancel.Cancel(); } }; tbl.Add(new Label("BackgroundWorker:\n(XwtSynchronizationContext.Post)"), 0, r); tbl.Add(l3 = new Label("0"), 1, r); tbl.Add(btn3 = new ToggleButton("Run"), 2, r++); var worker3 = new BackgroundWorker(); worker3.WorkerReportsProgress = true; worker3.WorkerSupportsCancellation = true; worker3.DoWork += BackgroundWorkerDoWork; worker3.ProgressChanged += (sender, e) => l3.Text = e.ProgressPercentage.ToString(); btn3.Toggled += delegate { if (btn3.Active) { worker3.RunWorkerAsync(); } else { worker3.CancelAsync(); } }; tbl.Add(new Label("XwtSynchronizationContext.Send:"), 0, r); tbl.Add(l4 = new Label("0"), 1, r); tbl.Add(btn4 = new ToggleButton("Run"), 2, r++); worker4 = new SampleBGWorker(); worker4.SynchronizationContext = SynchronizationContext.Current; worker4.InvokeSynchronized = true; worker4.Count += HandleCount4; btn4.Toggled += delegate { if (btn4.Active) { worker4.Start(); } else { worker4.Stop(); } }; tbl.Add(new Label("ISynchronizeInvoke.BeginInvoke:"), 0, r); tbl.Add(l5 = new Label("0"), 1, r); tbl.Add(btn5 = new ToggleButton("Run"), 2, r++); worker5 = new SampleBGWorker(); worker5.InvokeSynchronized = false; worker5.Count += HandleCount5; btn5.Toggled += delegate { if (btn5.Active) { worker5.Start(); } else { worker5.Stop(); } }; tbl.Add(new Label("ISynchronizeInvoke.Invoke:"), 0, r); tbl.Add(l6 = new Label("0"), 1, r); tbl.Add(btn6 = new ToggleButton("Run"), 2, r++); worker6 = new SampleBGWorker(); worker6.InvokeSynchronized = true; worker6.Count += HandleCount6; btn6.Toggled += delegate { if (btn6.Active) { worker6.Start(); } else { worker6.Stop(); } }; }
public MultithreadingSample () { int r = 0; var tbl = new Table (); PackStart (tbl); tbl.Add (new Label("Application.Invoke:"), 0, r); tbl.Add (l1 = new Label ("0"), 1, r); tbl.Add (btn1 = new ToggleButton("Run"), 2, r++); Task worker1; CancellationTokenSource worker1cancel = new CancellationTokenSource ();; btn1.Toggled += delegate { if (btn1.Active) { worker1cancel = new CancellationTokenSource (); worker1 = Task.Factory.StartNew (delegate { int cnt = 0; while (!worker1cancel.Token.IsCancellationRequested) { Thread.Sleep (50); cnt++; string txt = cnt.ToString (); Application.Invoke (() => l1.Text = txt); } }, worker1cancel.Token, TaskCreationOptions.LongRunning); } else worker1cancel.Cancel(); }; tbl.Add (new Label("Application.UITaskScheduler:"), 0, r); tbl.Add (l2 = new Label ("0"), 1, r); tbl.Add (btn2 = new ToggleButton("Run"), 2, r++); Task worker2; CancellationTokenSource worker2cancel = new CancellationTokenSource ();; btn2.Toggled += delegate { if (btn2.Active) { worker2cancel = new CancellationTokenSource (); worker2 = Task.Factory.StartNew (delegate { int cnt = 0; while (!worker2cancel.Token.IsCancellationRequested) { Thread.Sleep (50); cnt++; var txt = cnt.ToString (); Task.Factory.StartNew(delegate { l2.Text = txt; }, worker2cancel.Token, TaskCreationOptions.None, Application.UITaskScheduler); } }, worker2cancel.Token, TaskCreationOptions.LongRunning); } else worker2cancel.Cancel(); }; tbl.Add (new Label("BackgroundWorker:\n(XwtSynchronizationContext.Post)"), 0, r); tbl.Add (l3 = new Label ("0"), 1, r); tbl.Add (btn3 = new ToggleButton("Run"), 2, r++); var worker3 = new BackgroundWorker (); worker3.WorkerReportsProgress = true; worker3.WorkerSupportsCancellation = true; worker3.DoWork += BackgroundWorkerDoWork; worker3.ProgressChanged += (sender, e) => l3.Text = e.ProgressPercentage.ToString (); btn3.Toggled += delegate { if (btn3.Active) worker3.RunWorkerAsync (); else worker3.CancelAsync (); }; tbl.Add (new Label("XwtSynchronizationContext.Send:"), 0, r); tbl.Add (l4 = new Label ("0"), 1, r); tbl.Add (btn4 = new ToggleButton("Run"), 2, r++); worker4 = new SampleBGWorker (); worker4.SynchronizationContext = SynchronizationContext.Current; worker4.InvokeSynchronized = true; worker4.Count += HandleCount4; btn4.Toggled += delegate { if (btn4.Active) worker4.Start (); else worker4.Stop (); }; tbl.Add (new Label("ISynchronizeInvoke.BeginInvoke:"), 0, r); tbl.Add (l5 = new Label ("0"), 1, r); tbl.Add (btn5 = new ToggleButton("Run"), 2, r++); worker5 = new SampleBGWorker (); worker5.InvokeSynchronized = false; worker5.Count += HandleCount5; btn5.Toggled += delegate { if (btn5.Active) worker5.Start (); else worker5.Stop (); }; tbl.Add (new Label("ISynchronizeInvoke.Invoke:"), 0, r); tbl.Add (l6 = new Label ("0"), 1, r); tbl.Add (btn6 = new ToggleButton("Run"), 2, r++); worker6 = new SampleBGWorker (); worker6.InvokeSynchronized = true; worker6.Count += HandleCount6; btn6.Toggled += delegate { if (btn6.Active) worker6.Start (); else worker6.Stop (); }; }