public static void MainThreadEntrypoint(Func <Task> mainThreadEntrypoint)
        {
            var syncContext    = new UIDispatcherMock();
            var oldSyncContext = SynchronizationContext.Current;

            SynchronizationContext.SetSynchronizationContext(syncContext);
            Exception unhandledException = null;

            syncContext.Post(
                async state =>
            {
                try
                {
                    await mainThreadEntrypoint();
                }
                catch (Exception ex)
                {
                    unhandledException = ex;
                }
                finally
                {
                    syncContext.Continue = false;
                }
            },
                null);
            syncContext.Loop();
            SynchronizationContext.SetSynchronizationContext(oldSyncContext);
            if (unhandledException != null)
            {
                ExceptionDispatchInfo.Capture(unhandledException).Throw();
            }
        }
Esempio n. 2
0
 public static void MainThreadEntrypoint(Func<Task> mainThreadEntrypoint)
 {
     var syncContext = new UIDispatcherMock();
     var oldSyncContext = SynchronizationContext.Current;
     SynchronizationContext.SetSynchronizationContext(syncContext);
     Exception unhandledException = null;
     syncContext.Post(
         async state =>
         {
             try
             {
                 await mainThreadEntrypoint();
             }
             catch (Exception ex)
             {
                 unhandledException = ex;
             }
             finally
             {
                 syncContext.Continue = false;
             }
         },
         null);
     syncContext.Loop();
     SynchronizationContext.SetSynchronizationContext(oldSyncContext);
     if (unhandledException != null)
     {
         ExceptionDispatchInfo.Capture(unhandledException).Throw();
     }
 }
        public void OpenFileForRead_NoHangForSync()
        {
            // Simulate the UI thread of an app.
            UIDispatcherMock.MainThreadEntrypoint(delegate
            {
                //  Arrange
                IFolder folder  = TestFileSystem.LocalStorage;
                string fileName = "fileToOpenForRead.txt";
                IFile file      = folder.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists).Result;

                //  Act
                using (Stream stream = file.OpenAsync(FileAccess.Read).Result)
                {
                    //  Assert
                    Assert.IsFalse(stream.CanWrite);
                    Assert.IsTrue(stream.CanRead);
                    Assert.IsTrue(stream.CanSeek);
                }

                //  Cleanup
                file.DeleteAsync().Wait();
            });
        }