This commit is contained in:
Andrew Arnott 2018-02-19 02:22:04 -08:00
Родитель 61da424428
Коммит 03f83a48a4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: A9B9910CDCCDA441
2 изменённых файлов: 24 добавлений и 10 удалений

Просмотреть файл

@ -1,10 +1,19 @@
# VSTHRD010 Invoke single-threaded types on Main thread
Acquiring, casting, or invoking Visual Studio services should be done after ensuring
that your code is running on the UI thread.
Acquiring, casting, or invoking single-threaded objects should be done after ensuring
that your code is running on the main thread.
This analyzer can be configured to:
1. Recognize the objects that are single-threaded that are unique to your app or library.
2. Recognize synchronous methods that verify the caller is already on the main thread.
3. Recognize methods that switch to the main thread when the caller awaits them.
Calls to `JoinableTaskFactory.SwitchToMainThreadAsync` methods are pre-configured.
## Examples of patterns that are flagged by this analyzer
This example is based on the configuration available from the Visual Studio SDK
that defines `IVs*` interfaces as requiring the main thread.
```csharp
private void CallVS()
{
@ -15,9 +24,13 @@ private void CallVS()
## Solution
First ensure you are running on the UI thread before interacting with a Visual Studio service.
First ensure you are running on the main thread before interacting with single-threaded objects.
Either throw when you are not on the appropriate thread, or explicitly switch to the
UI thread.
main thread.
This solution example is based on the configuration available from the Visual Studio SDK
that defines `ThreadHelper.ThrowIfNotOnUIThread()` as one which throws if the caller
is not already on the main thread.
```csharp
private void CallVS()
@ -36,3 +49,7 @@ private async Task CallVSAsync()
```
Refer to [Asynchronous and multithreaded programming within VS using the JoinableTaskFactory](http://blogs.msdn.com/b/andrewarnottms/archive/2014/05/07/asynchronous-and-multithreaded-programming-within-vs-using-the-joinabletaskfactory/) for more info.
## Configuration
TODO

Просмотреть файл

@ -3,21 +3,18 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
/// <summary>
/// Report warnings when detect the usage on Visual Studio services (i.e. IVsSolution) without verified
/// that the current thread is main thread, or switched to main thread prior invocation explicitly.
/// Flag usage of objects that must only be invoked while on the main thread (e.g. STA COM objects)
/// without having first verified that the current thread is main thread either by throwing if on
/// the wrong thread or asynchronously switching to the main thread.
/// </summary>
/// <remarks>
/// [Background] Most of Visual Studio services especially the legacy services which are implemented in native code