[Category("NotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran public void DownloadMultiple3() { WebClient wc = new WebClient(); int thread_id = Thread.CurrentThread.ManagedThreadId; bool data_completed = false; bool string_completed = false; bool error = false; wc.DownloadDataCompleted += delegate { if (data_completed || (Thread.CurrentThread.ManagedThreadId != thread_id)) { error = true; } data_completed = true; }; wc.DownloadStringCompleted += delegate { if (string_completed || (Thread.CurrentThread.ManagedThreadId != thread_id)) { error = true; } string_completed = true; }; MessagePumpSyncContext.Run(async() => { await wc.DownloadStringTaskAsync("http://www.example.org/"); await wc.DownloadDataTaskAsync("http://www.example.com/"); }, () => data_completed && string_completed, 15000); Assert.IsTrue(data_completed, "#1"); Assert.IsTrue(string_completed, "#2"); Assert.IsFalse(error, "#3"); }
public static void Run(Func <Task> action, Func <bool> completed, int timeout) { var old_ctx = SynchronizationContext.Current; var ctx = new MessagePumpSyncContext(completed, timeout); try { SynchronizationContext.SetSynchronizationContext(ctx); var thread_id = Thread.CurrentThread.ManagedThreadId; var task = action(); task.ContinueWith((t) => { ctx.running = false; }, TaskScheduler.FromCurrentSynchronizationContext()); ctx.RunMessagePump(); if (task.IsFaulted) { throw task.Exception; } } finally { SynchronizationContext.SetSynchronizationContext(old_ctx); } }
[Category("NotWorking")] // Fails when ran as part of the entire BCL test suite. Works when only this fixture is ran public void DownloadMultiple2() { WebClient wc = new WebClient(); MessagePumpSyncContext.Run(async() => { await wc.DownloadStringTaskAsync("http://www.example.org/"); await wc.DownloadDataTaskAsync("http://www.example.com/"); }, null, 15000); }
public void DownloadData() { WebClient wc; bool progress_changed = false; bool completed = false; bool progress_changed_error = false; bool completed_error = false; int thread_id = Thread.CurrentThread.ManagedThreadId; wc = new WebClient(); wc.DownloadProgressChanged += delegate { progress_changed = true; if (Thread.CurrentThread.ManagedThreadId != thread_id) { progress_changed_error = true; } }; wc.DownloadDataCompleted += delegate { completed = true; if (Thread.CurrentThread.ManagedThreadId != thread_id) { completed_error = true; } }; MessagePumpSyncContext.Run(async() => { var url = Assembly.GetExecutingAssembly().CodeBase; await wc.DownloadDataTaskAsync(url); Assert.AreEqual(Thread.CurrentThread.ManagedThreadId, thread_id); }, () => progress_changed && completed, 10000); Assert.IsTrue(progress_changed, "#1"); Assert.IsFalse(progress_changed_error, "#2"); Assert.IsTrue(completed, "#3"); Assert.IsFalse(completed_error, "#4"); }
public static void Run (Func<Task> action, Func<bool> completed, int timeout) { var old_ctx = SynchronizationContext.Current; var ctx = new MessagePumpSyncContext (completed, timeout); try { SynchronizationContext.SetSynchronizationContext (ctx); var thread_id = Thread.CurrentThread.ManagedThreadId; var task = action (); task.ContinueWith ((t) => { ctx.running = false; }, TaskScheduler.FromCurrentSynchronizationContext ()); ctx.RunMessagePump (); if (task.IsFaulted) throw task.Exception; } finally { SynchronizationContext.SetSynchronizationContext (old_ctx); } }