In C# XAML WPF app, getting Exception HResult -2146233079 Task

DonBaechtel-7903 141 Reputation points
2023-03-19T22:17:56.0933333+00:00

I have a Visual Studio C# WPF XAML application running on Windows 10 .Net 7.0.

I have an async Task that runs continuously (while (IsRuuning).

The job of the Task is to: every 100 ms, under a mutex, read the contents of a file located at statusfilePath, and use the contents of that file to update the contents of 5 Labels in the main window,

When the statement "Xpos.Contents = terms[0];" is executed, the Exception is triggered.

The Exception indicates "The calling thread cannot access this object because a different thread owns it."

How can I run a continuously running task that periodically modifies the Contents of WPF Label objects owned by the MainWindow thread?

Code for the Task:

        private async Task<bool> ReadStatus()
        {
            System.Threading.Mutex? _mutey = null;
            try
            {
                _mutey = System.Threading.Mutex.OpenExisting("Cinerique");
                //we got Mutey and can try to obtain a lock by waitone
            }
            catch
            {
                //the specified mutex doesn't exist, we should create it
                _mutey = new System.Threading.Mutex(false, "Cinerique");
            }

            char[] charSeparators = new char[] { ' ' };
            try
            {
                await Task.Delay(1000);
                while (IsRunning)
                {
                    await Task.Delay(100);
                    string resp = "";
                    string? status = null;
                    _mutey.WaitOne();
                    using (StreamReader sr = new StreamReader(statusfilePath))
                    {
                        status = sr.ReadLine().Trim();
                    }
                    _mutey.ReleaseMutex();
                    if (status != null)
                    {
                        string[] terms = status.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
                        if (terms.Count() == 5)
                        {
                            X_Pos.Content = terms[0];
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Exception n = e;
                return true;
            }
            finally
            {
                _mutey.ReleaseMutex();
                _mutey = null;
            }
            return true;
        }

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,560 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,342 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,666 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
{count} votes

Accepted answer
  1. Viorel 111.7K Reputation points
    2023-03-20T06:39:57.6266667+00:00

    Try assigning the value in this manner:

    await Dispatcher.BeginInvoke( ( ) => X_Pos.Content = terms[0] );
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful