C# 如何在文本装饰项目VSIX上实现多线程

C# 如何在文本装饰项目VSIX上实现多线程,c#,.net,multithreading,thread-safety,vsix,C#,.net,Multithreading,Thread Safety,Vsix,我正在创建一个具有文本装饰功能的VSIX项目。在这里,当我的CreateGeom()方法执行,并尝试运行newimage{},它抛出异常调用线程必须是STA,因为许多UI组件都需要这个。我尝试手动设置ApartmentState,但没有成功。以下是我的代码: [STAThread] internal async void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e) { string lang = get

我正在创建一个具有文本装饰功能的VSIX项目。在这里,当我的
CreateGeom()
方法执行,并尝试运行
newimage{}
,它抛出异常
调用线程必须是STA,因为许多UI组件都需要这个
。我尝试手动设置
ApartmentState
,但没有成功。以下是我的代码:

[STAThread]
internal async void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
    string lang = getCurruntCodeLanguage();
    if (lang.ToString() != "java" && lang.ToString() != "ts" && lang.ToString() != "js")
    {

    }
    else
    {
        try
        {
            currentSnapshotText = this.view.TextBuffer.CurrentSnapshot.GetText();
            this.currentSnapshotLinesList = this.view.TextBuffer.CurrentSnapshot.GetText().Split('\n');
            foreach (string str in this.currentSnapshotLinesList.ToList<string>())
            {
                TextAdornment1.allLinesList.Add(str.Trim('\r'));
            }

            if (numberOfLinesBeforeEnter != this.currentSnapshotLinesList.Length)
            {
                boundstart = 0;
                boundend = 0;
                this.checkCountIfMarked = 1;
                this.markthis.Clear();

                if (this.image != null)
                {
                    RemoveMarkedArea();
                }

                if (threadSendText != null)
                {
                    if (threadSendText.IsAlive)
                    {
                        threadSendText.Abort();
                    }
                }
                var v = System.Threading.Thread.CurrentThread.GetApartmentState(); //returns 'STA'

                threadSendText = new System.Threading.Thread(new ThreadStart(SndTextCall)); // Apartment State is 'Unknown'
                threadSendText.SetApartmentState(ApartmentState.STA); // Apartment State is 'STA'
                threadSendText.IsBackground = true;
                threadSendText.Priority = ThreadPriority.Highest;
                threadSendText.Start();
            }

            numberOfLinesBeforeEnter = this.currentSnapshotLinesList.Length;
        }
        catch (Exception exc)
        {
            //MessageBox.Show(exc.ToString());
        }
    }
}

可以使用或中的说明显式切换到UI线程

private void CreateGeom(SnapshotSpan span)
{
    IWpfTextViewLineCollection textViewLines = this.view.TextViewLines;

    this.geometry = textViewLines.GetMarkerGeometry(span);
    if (this.geometry != null)
    {
        this.drawing = new GeometryDrawing(this.brush, this.pen, this.geometry);
        this.drawing.Freeze();

        var drawingImage = new DrawingImage(this.drawing);
        drawingImage.Freeze();

        image = new Image
        {
            Source = drawingImage,
        }; // Here the exception comes

        Canvas.SetLeft(image, this.geometry.Bounds.Left);
        Canvas.SetTop(image, this.geometry.Bounds.Top);

        this.drawingImageList.Add(image);
    }
}
private void CreateGeom(SnapshotSpan span)
{
  ThreadHelper.JoinableTaskFactory.Run(async delegate
  {
    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
    // You're now on the UI thread.
    // ... Create your image here

  });
}