Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

How to resolve 'The calling thread must be STA, because many UI components require this' in VSIX “TextAdornment”

$
0
0

I am creating a VSIX project having TextAdornment features. Here when My CreateGeom() method executes, and try to run new Image{};, It throws exception The calling thread must be STA, because many UI components require this. I tried setting ApartmentState manually but no luck. Following is my code:

[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());
        }
    }
}

There are other recursive methods also in SndTextCall(). All are working fine but when CreateGeom() method comes into execution, it throws an exception.

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);
    }
}



Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>