0

works here ----- crashes here ----- So i am making this server client chat system based on message queue and in client side, i have a thread that receives messages. The name of my listbox is 'displaymsg'

here's my thread pool method

public void getmsg(object ob)
    {
        string msg = "";

        while (true)
        {
            msg = mRecieve.GetMessages();
            displaymsg.Items.Add(msg);
        }
    }

But the program is crashing when it reaches to displaymsg.Items.Add(msg) part. To check if it is even reaching there and if it is working fine, i replaced it with MessageBox.Show(msg), and it is working fine, i receive every message in a pop up box that i send from other end. I'm new to wpf so kinda lost now... please help!!

Clemens
  • 123,504
  • 12
  • 155
  • 268
Shrey
  • 93
  • 3
  • 5
  • 12
  • 1
    What message do you receive in the non-working case? I suspect this is due to trying to access the control from the wrong thread but without the error message I can't be sure of that. – SoronelHaetir Dec 10 '17 at 07:04
  • It doesn't show any error messages. Just crashes saying "client has stopped working, windows is reporting this problem" – Shrey Dec 10 '17 at 07:08
  • @Shrey Is your while loop in your program actually written like that? while(true) goes on infinitely. If you're creating infinite amounts of items to be added its going to crash because you're consuming all the computer resources. – Jamin Dec 10 '17 at 07:08
  • Actually for sake of making it work first, i dont have loop at all. i just do string msg = ""; displaymsg.Items.Add("something here"); it crashes when i use displaymsg, but works when i use messagebox. – Shrey Dec 10 '17 at 07:12
  • Another thing is, i have made this method public void getmsg(), but when i do static void getmsg(), i don't get access to displaymsg at all. While having public method, i can atleast access it, though it crashes, but when i make this method static, i can't even access it. – Shrey Dec 10 '17 at 07:14
  • @Shrey Please post some more code so we know whats going on. From the example we cant deduce anything too specific to help you figure out the problem. – Jamin Dec 10 '17 at 07:16
  • Are you running your program in the debugger? Debugged apps don‘t just crash without a message. – Sefe Dec 10 '17 at 07:20
  • Okay i just ran it in debugger and it says, System.InvalidOperationException: 'The calling thread cannot access this object because a different thread owns it.' – Shrey Dec 10 '17 at 07:24
  • i have just uploaded two pictures of my code... please check above – Shrey Dec 10 '17 at 07:25
  • You run it in the debugger yet get the" reporting this error" behavior? And there is no useful message at the bottom of the debug output window (found at the view menu -> output in VS). – SoronelHaetir Dec 10 '17 at 07:30
  • I just read this other post regarding how you have to use this.Dispatcher.Invoke when accessing xaml in custom made methods rather than event and it solved the issue. :) Really appreciate help of you all though. And @SoroneHaetir, my bad actually, i was running it without debugging and hence it was crashing rather than showing the error message. I am new to this stuff so, but hey that is a really good way to track your problem down by running it in debugger. :) Thanks! – Shrey Dec 10 '17 at 07:34
  • @Shrey can you supply a link to what found you the answer for record keeping. Also genuinely curious. Thanks – Jamin Dec 10 '17 at 07:36
  • 1
    Yes for sure @Jamin. Here's the link https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it – Shrey Dec 10 '17 at 07:40
  • I have also updated changes in my question description above that i made in my method to make it work. :) – Shrey Dec 10 '17 at 07:41
  • You don‘t update your question in these cases. You post an answer. – Sefe Dec 10 '17 at 07:59
  • How bizarre - I marked this question as a duplicate with the correct solution 5 hours ago, and someone removed that comment. Weird. – mjwills Dec 10 '17 at 12:35

1 Answers1

1

You can not access a control from a non-UI thread. You have to use a dispatcher to marshal it back to the UI thread:

displaymsg.Dispatcher.Invoke(() => displaymsg.Items.Add(msg));

You can also fire and forget if you don‘t want to wait for the result:

displaymsg.Dispatcher.BeginInvoke(() => displaymsg.Items.Add(msg));
Sefe
  • 13,731
  • 5
  • 42
  • 55