In my application I had a requirement of notifying the user about the pending applications.
So in the mdiParent
I set a BackgroundWorker
which keeps querying the database to get any pending application, and if it finds any display it on the Tooltip on the MdiParent
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(2000);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
fillnotification();
}
public void fillnotification()
{
int pending = 0;
if( Program.USERPK != 0)
{
DataTable dt = nftrans.getnotification();
pending = dt.Rows.Count;
String message = "You Have " + pending + " Applications Pending For Approval";
// toolTip1.SetToolTip(lblStatus , message);
toolTip1.Show(message , this, lblStatus.Location);
}
}
but when I run the solution I am getting an exception:
Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on.
I understand its due to two different threads but cannot sort this out. Can any one suggest a solution? I tried the ideas I read in related questions But cannot find a correct solution