The thread cannot access an object because a different thread owns it when using wpf

Ibanez1408

I get the error:

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

when I use this code:

private async void onGetFilesAsync(object sender, RoutedEventArgs e)
{
    if (txtIpAddress.Text.Contains("IP Address"))
    {
        MessageBox.Show("Ip Address is invalid");
        return;
    } else if (string.IsNullOrEmpty(dpDate.Text))
    {
        MessageBox.Show("Date is invalid");
        return;
    }

    var date = dpDate.Text;
    var splitDate = date.Split('/');
    int month = Convert.ToInt32(splitDate[1]);
    int day = Convert.ToInt32(splitDate[0]);
    var year = splitDate[2];
    var filePath = $@"\\{txtIpAddress.Text}\i\Hardware Interfacing\{year}\{month}\{day}\PeripheralLogsDq.txt";

    using (new ImpersonateUser("usernam", "", "password"))
    {
        FlowDocument doc = new FlowDocument();

        IsWaveActive = true;

        await Task.Run(() =>
        {
            LoadLogs(rbQueue, File.ReadAllText(filePath));
        });

        IsWaveActive = false;
    } 

private void LoadLogs(RichTextBox rtb, string msg)
{
    FlowDocument flowDocument = new FlowDocument();
    Paragraph paragraph = new Paragraph();
    paragraph.Inlines.Add(new Run(msg));
    flowDocument.Blocks.Add(paragraph);


    Dispatcher.Invoke(new Action(delegate ()
    {
        rtb.Document = flowDocument;
    }));
}

I am doing this because I am trying to implement a spinner when the data is loading from a text file.

Bohring

The other thread comes form using Task.Run

    await Task.Run(() =>
    {
        LoadLogs(rbQueue, File.ReadAllText(filePath));
    });

replace it with something like:

using (var reader = File.OpenText(filePath))
{
    LoadLogs(rbQueue, await reader.ReadToEndAsync());
}

and then LoadLogs() does not need to Invoke anymore.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

the calling thread cannot access this object because a different thread owns it wpf

From Dev

The Calling thread cannot access this object because a different thread owns it,WPF

From Dev

WPF: The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

From Dev

"The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

From

The calling thread cannot access this object because a different thread owns it

From Dev

The calling thread cannot access this object because a different thread owns it (with Dispatcher)

From Dev

ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

From Dev

Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

From Dev

The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

From Dev

Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

From Dev

c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

From Dev

Different thread owns it in WPF

From Dev

Famous the calling thread cannot access this object because a different issue

From Dev

"calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

From Dev

"The subprocess making the call can not access this object because the owner is another thread" Exception ASYNC/AWAIT WPF C#

From Dev

WPF Access window created on different UI thread

From Dev

Calling thread cannot access object when it's created from the same thread

From Dev

Calling thread cannot access object due to separate thread ownership

From Dev

WPF error 'calling thread cannot access this object' trying to play sounds in c# class

From Dev

Redshift - user "xyz" cannot be dropped because the user owns some object

From Dev

Updating image via Dispatcher still gives "different thread owns it" error

From Dev

sequential access to object with multiple thread using synchronized

From Dev

Pass object to thread (access object outside thread)

From Java

Determine which thread owns a monitor

From Dev

How to kill the thread in specific object when application exit in WPF?

From Dev

WPF host object in new thread

From Dev

Async method continuation in different thread - means cooperative two-thread access to object variable?

From Dev

Using Thread with Parameter as object

Related Related

  1. 1

    the calling thread cannot access this object because a different thread owns it wpf

  2. 2

    The Calling thread cannot access this object because a different thread owns it,WPF

  3. 3

    WPF: The calling thread cannot access this object because a different thread owns it

  4. 4

    The calling thread cannot access this object because a different thread owns it, even when using the dispatcher

  5. 5

    "The calling thread cannot access this object because a different thread owns it" error when updating UI control from different thread in WPF

  6. 6

    The calling thread cannot access this object because a different thread owns it

  7. 7

    The calling thread cannot access this object because a different thread owns it (with Dispatcher)

  8. 8

    ReactiveUI WPF - The calling thread cannot access this object because a different thread owns it

  9. 9

    Getting error The calling thread cannot access this object because a different thread owns it wpf, How to use Dispatch.invovef

  10. 10

    The calling thread cannot access this object because a different thread owns it - BackgroundWorker error

  11. 11

    Error:The calling thread cannot access this object because a different thread owns it. Storyboard simulation

  12. 12

    c# - Changes in cefsharp 79.1.35 (from 75.1.143) creates: 'The calling thread cannot access this object because a different thread owns it.'

  13. 13

    Different thread owns it in WPF

  14. 14

    Famous the calling thread cannot access this object because a different issue

  15. 15

    "calling thread cannot access this object" exception when working with live visual tree in multithreaded UI WPF app

  16. 16

    "The subprocess making the call can not access this object because the owner is another thread" Exception ASYNC/AWAIT WPF C#

  17. 17

    WPF Access window created on different UI thread

  18. 18

    Calling thread cannot access object when it's created from the same thread

  19. 19

    Calling thread cannot access object due to separate thread ownership

  20. 20

    WPF error 'calling thread cannot access this object' trying to play sounds in c# class

  21. 21

    Redshift - user "xyz" cannot be dropped because the user owns some object

  22. 22

    Updating image via Dispatcher still gives "different thread owns it" error

  23. 23

    sequential access to object with multiple thread using synchronized

  24. 24

    Pass object to thread (access object outside thread)

  25. 25

    Determine which thread owns a monitor

  26. 26

    How to kill the thread in specific object when application exit in WPF?

  27. 27

    WPF host object in new thread

  28. 28

    Async method continuation in different thread - means cooperative two-thread access to object variable?

  29. 29

    Using Thread with Parameter as object

HotTag

Archive