0

I am using Matrix xmppClient to set up a chat client app on WP7.. On giving wrong credentials (username/password), Invalid Cross thread access exception is been thrown...I am using Dipatcher.BeginInvoke as shown below but still that exception is coming.. can anybody tel me what need to be done to fix this?? Thanks

void xmppClient_OnAuthError(object sender, SaslEventArgs e)
    {           
        Dispatcher.BeginInvoke(() =>
        {
            DisplayEvent("OnAuthError", "authentication failed");
        });
    }

        void DisplayEvent(string evt, string arg1 = null, string arg2 = null)
    {
        var par = new Paragraph();
        par.Inlines.Add(new Run
        {
            Text = evt,
            FontSize = 14,
        });

        if (arg1 != null)
            par.Inlines.Add(new Run
            {
                Text = "\t=>" + arg1,
                FontSize = 14,
            });

        if (arg2 != null)
            par.Inlines.Add(new Run
            {
                Text = "\t=> " + arg2,
                FontSize = 14,
            });

        var rtf = new RichTextBox();
        rtf.Blocks.Add(par);
        stackEvents.Children.Add(rtf);
        ScrollToEnd(scrollEvents);
    }

Not getting any exception in DisplayEvent method

Deepak
  • 21
  • 1
  • 8

1 Answers1

0

Invalid cross thread access exception occurs, generally when UI elements are accessed from a non-UI thread. In your DisplayEvent method may that is happening.

Enclose the last 2 lines of DisplayEvent in the Dispatcher

   ...
   rtf.Blocks.Add(par);
   Dispatcher.BeginInvoke(() =>
   {
      stackEvents.Children.Add(rtf);
      ScrollToEnd(scrollEvents);
   }
}
nkchandra
  • 5,585
  • 2
  • 30
  • 45
  • void xmppClient_OnBeforeSasl(object sender, SaslEventArgs e) { Dispatcher.BeginInvoke(() => DisplayEvent("OnBeforeSasl")); after this handler I am getting tat exception }; } – Deepak Feb 04 '13 at 12:33
  • 1
    Deepak: It'd probably help if you show us the code related to the crash. Just showing ANY code does NOT help. Show us the line of code where the Exception is thrown (= shown in Debugger) plus some surrounding lines. – HDW Production Feb 05 '13 at 08:35