I get a run-time exception trying to execute the example below.
Unhandled Exception: System.InvalidOperationException: The current SynchronizationContext may not be used as a TaskScheduler. at System.Threading.Tasks.SynchronizationContextTaskScheduler..ctor() at System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext() at TaskDemo.MyForm..ctor() in D:\myStudio\ASPNet\CSharp\CSharp4\MyApp\MyApp\Hello.cs:line 428 at TaskDemo.SynchronizationContextTaskScheduler() in D:\myStudio\ASPNet\CSharp\CSharp4\MyApp\MyApp\Hello.cs:line 396 at TaskDemo.Go() in D:\myStudio\ASPNet\CSharp\CSharp4\MyApp\CLRviaCSharp\Hello.cs:line 214 at ComputeOps.Main() in D:\myStudio\ASPNet\CSharp\CSharp4\MyApp\CLRviaCSharp\Hello.cs:line 23
Code example:
public class TaskSchedulerTest { public void Test() { SynchronizationContextTaskScheduler(); } private void SynchronizationContextTaskScheduler() { var f = new MyForm(); System.Windows.Forms.Application.Run(); } private sealed class MyForm : System.Windows.Forms.Form { public MyForm() { Text = "Synchronization Context Task Scheduler Demo"; Visible = true; Width = 400; Height = 100; } private readonly TaskScheduler m_syncContextTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); private CancellationTokenSource m_cts; protected override void OnMouseClick(System.Windows.Forms.MouseEventArgs e) { if (m_cts != null) {
Any idea?
source share