Display Message To User

  • Thread starter Thread starter MDB
  • Start date Start date
M

MDB

Hello All,

I have an application with 2 threads. The main thread is the application
and the secondary thread checks for messages every minute and when there are
messages, I display them to the user. The problem is that since I am
opening a form from the second thread, it will lock up both threads if the
user is doing something at the time the message is displayed. Can anyone
point me in the right direction on how I should be doing this?

Here is the code I use to display a message

if(myApp.Classes.Globals.gfrmInstantMessage ==
null){myApp.Classes.Globals.gfrmInstantMessage = new frmInstantMessage();}
myApp.Classes.Globals.gfrmInstantMessage.LoadMessageBox("Messages","No New
Messages","");
myApp.Classes.Globals.gfrmInstantMessage.ShowDialog();
myApp.Classes.Globals.gfrmInstantMessage.BringToFront();
myApp.Classes.Globals.gfrmInstantMessage.Focus();
Application.DoEvents();

LoadMessageBox looks like this inside my gfrmInstantMessage Form:

Public void LoadMessageBox(string strHeader, string strMessage, string
sFrom)
{

txtMessage.WordWrap = true;
this.Text = strHeader;
txtMessage.Text = strMessage.Replace("<br>","\r\n");

if(sFrom != PMMobile.Classes.Globals.gstrDispatchId && sFrom != "")
{
txtMessage.Text = "From:\r\n" + sFrom + "\r\n\r\n" + "Message:\r\n" +
txtMessage.Text;
strFrom = sFrom;
txtMessage.Height = 200;
btnReply.Visible=true;
}
else
{
txtMessage.Height = 248;
btnReply.Visible=false;
}
if(this.Text == "Directions")
{
txtMessage.WordWrap = false;
txtMessage.ScrollBars = ScrollBars.Both;
}
}

Thanks
 
It is considered to be a wrong thing to do to create UI elements on
secondary threads in CF. Granted, it is possible to do, but there are
numerous side effects, and thus it is something to avoid. Instead, use
Form.Invoke from the secondary thread to pop notifiction messages
 
Okay, thanks. I was just hoping I was missing something since I have about
20 forms where this could be called from. I assume then that I have to keep
track of what form I am on and then have the same function in each form.
Then in the secondary thread, call the function in side the current form.
 
Back
Top