-1

I have an application in which i am generating status messages frequently. I am printing all these messages to message box. Instead of using message box i want to use list box or multi line text box . How should i proceed.

void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
    publishstatusTextbox.Items.Insert(0, DateTime.Now.ToString("hh:mm:ss:fff") + "MessageID=");
}

I'm getting following exception

cross thread operation not valid "publishstatusListbox" is accessed from the thread other than the thread it was created on

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
IoT Information
  • 125
  • 1
  • 2
  • 5
  • 1
    Possible duplicate of [Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – Gilad Green Oct 26 '16 at 06:54
  • Run this code in UI thread – Vivek Nuna Oct 26 '16 at 07:18

1 Answers1

0

you would want to do something like this that will group all of your error and then put them all in one message box.

var errors = new List<string>();

try
{ 
    //your code here 
}
catch(Exception ex) {
    errors.Add(ex.InnerException.ToString());
}

var sb = new StringBuilder();
foreach (var errorMessage in errors)
{   
    sb.AppendLine(errorMessage);
}

if (errors.Count > 0)
{
    MessageBox.Show(sb.ToString(), "Errors Present", MessageBoxButton.OK, MessageBoxImage.Error);
}
Simon Price
  • 3,011
  • 3
  • 34
  • 98