How do I access a control within a static method?
abcdef123ghi opened this issue · 1 comments
abcdef123ghi commented
I need to show the message on a Datagridview,I tried the follwoing code,but it doesn't work
static void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
{
// handle message received
string message = System.Text.Encoding.UTF8.GetString(e.Message);
string guildstr = Guid.NewGuid().ToString();
string topicstr = "/lists/thenumber";
string clientstr = "client";
string usernostr = "userno";
int index = myform.MsgDGView.Rows.Add();
myform.MsgDGView.Rows[index].Cells[0].Value = "1"; // Exception occured
myform.MsgDGView.Rows[index].Cells[1].Value = guildstr;
myform.MsgDGView.Rows[index].Cells[2].Value = topicstr;
myform.MsgDGView.Rows[index].Cells[3].Value = clientstr;
myform.MsgDGView.Rows[index].Cells[4].Value = usernostr;
myform.MsgDGView.Rows[index].Cells[5].Value = message;
myform.MsgDGView.Rows[index].Cells[6].Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
for (int i = 0; i < myform.MsgDGView.RowCount; i++)
{
myform.MsgDGView.Rows[index].Cells[0].Value = (i + 1).ToString();
}
myform.MsgTextBox.Text = message;
}
billbarni commented
I don't know if it is related to the UI thread.
Try this:
Invoke((Action)delegate
{
myform.MsgTextBox.Text = message;
});