This file contains hidden or 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
| public void DoSomething() | |
| { | |
| DoSomethingAsync().Wait(); // WARNING: this will deadlock. Don’t do this. | |
| } | |
| public async Task DoSomethingAsync() | |
| { | |
| await Task.Yield(); // could be any async work | |
| } |
This file contains hidden or 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
| private int m_filesOpened; | |
| private const int MaxOpenFilesAllowed = 2; | |
| private FileStream[] m_openFiles = new FileStream[MaxOpenFilesAllowed]; | |
| public int SomeMethod(string path) | |
| { | |
| ThreadHelper.ThrowIfNotOnUIThread(); | |
| if (m_filesOpened < MaxOpenFilesAllowed) | |
| { | |
| m_openFiles[m_filesOpened] = File.Open(path, FileMode.Open); | |
| m_filesOpened++; |
This file contains hidden or 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
| private async Task PerformDataAnalysisAsync() | |
| { | |
| // At this point, we’re on whatever thread the caller was on (UI thread or background). | |
| // The first thing we do is call a VS service, so ensure we’re on the UI thread. | |
| await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); | |
| this.solution.EnumProjects(…); | |
| await DoSomethingAsync(); | |
| // We’re still on the UI thread at this point. | |
| // Let’s get off to do some expensive work without disturbing the user. | |
| await TaskScheduler.Default; |
This file contains hidden or 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
| ThreadHelper.JoinableTaskFactory.Run(async delegate | |
| { | |
| await SomeOperationAsync(); | |
| }); |
This file contains hidden or 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
| int result = ThreadHelper.JoinableTaskFactory.Run(async delegate | |
| { | |
| int value = await SomeOperationAsync(); | |
| return value; | |
| }); | |
| return result; |
This file contains hidden or 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
| public IVsTask CreateProjectAsync(string projectName) | |
| { | |
| return ThreadHelper.JoinableTaskFactory.RunAsyncAsVsTask( | |
| VsTaskRunContext.UIThreadBackgroundPriority, | |
| async cancellationToken => | |
| { | |
| // Code here executes on the caller's thread (UI thread or otherwise) | |
| // before CreateProjectAsync returns an IVsTask. | |
| // [ your synchronous portion here ] |
This file contains hidden or 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
| public IVsTask CreateProjectAsync(string projectName) | |
| { | |
| return ThreadHelper.JoinableTaskFactory.RunAsyncAsVsTask( | |
| VsTaskRunContext.UIThreadBackgroundPriority, | |
| async cancellationToken => | |
| { | |
| IVsAsyncService someService; | |
| IVsTask someOtherAsyncOperation = someService.SomethingAsync(); | |
| await someOtherAsyncOperation; |
This file contains hidden or 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
| public IVsTask CreateProjectAsync(string projectName) | |
| { | |
| return ThreadHelper.JoinableTaskFactory.RunAsyncAsVsTask( | |
| VsTaskRunContext.UIThreadBackgroundPriority, | |
| async cancellationToken => | |
| { | |
| await Task.Yield(); | |
| return VSConstants.S_OK; | |
| }); |
This file contains hidden or 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
| await joinableTaskFactoryInstance.SwitchToMainThreadAsync(); |
This file contains hidden or 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
| JoinableTaskFactory.Run(async delegate { | |
| await SomeOperationAsync(...); | |
| }); |