Created
June 22, 2011 15:51
-
-
Save jrwren/1040405 to your computer and use it in GitHub Desktop.
trying to get dispatcher to run in nunit test :[
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestFixture] | |
public class DispatcherObjectIsAJerk | |
{ | |
[SetUp] | |
public void Setup() | |
{ | |
var task = System.Threading.Tasks.Task.Factory.StartNew(() => | |
{ | |
System.Windows.Threading.Dispatcher.Run(); | |
}); | |
} | |
[Test] | |
public void AndDoesntWorkWithSyncContext() | |
{ | |
var @do = new MyDO();@do.Go(); | |
@do.Dispatcher.InvokeShutdown(); | |
Assert.AreEqual(true, @do.done); | |
} | |
} | |
public class MyDO: System.Windows.Threading.DispatcherObject | |
{ | |
public bool done; | |
public MyDO() | |
{ | |
} | |
public void Go() | |
{ | |
done = false; | |
writeSC("main"); | |
var task = System.Threading.Tasks.Task.Factory.StartNew(() => { writeSC("task"); }); | |
task.ContinueWith(t => { writeSC("cont"); }); | |
task.Wait(); | |
task = System.Threading.Tasks.Task.Factory.StartNew(() => { writeSC("task"); }); | |
task.ContinueWith(t => { writeSC("cont"); done = true; }, GetScheduler(this.Dispatcher).Result); | |
task.Wait(); | |
} | |
public static System.Threading.Tasks.Task<System.Threading.Tasks.TaskScheduler> GetScheduler(System.Windows.Threading.Dispatcher d) | |
{ | |
var schedulerResult = new System.Threading.Tasks.TaskCompletionSource<System.Threading.Tasks.TaskScheduler>(); | |
var op = d.BeginInvoke(new Action(() => | |
schedulerResult.SetResult( | |
System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext()))); | |
return schedulerResult.Task; | |
} | |
private void writeSC(string note) | |
{ | |
var oneSC = System.Threading.SynchronizationContext.Current; | |
writemessage(note, oneSC); | |
var d=base.Dispatcher; | |
writemessage(note, d); | |
var ts = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext(); | |
writemessage(note, ts); | |
ts = System.Threading.Tasks.TaskScheduler.Current; | |
writemessage(note, ts); | |
ts = System.Threading.Tasks.TaskScheduler.Default; | |
writemessage(note, ts); | |
} | |
private static void writemessage(string note, object o) | |
{ | |
Console.Write(note); | |
Console.Write(" "); | |
Console.Write(o.GetHashCode()); | |
Console.Write(" "); | |
Console.WriteLine(o); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment