Hello,
I have a dialog (WaitingDialog) that is mainly used to display the happening activities. It has a Title & Message that sets the respective labels.
public string Title {
get { return title; }
set { title = value;
label1.Text = title;
}
}
public string Message {
get { return message; }
set { message = value;
msgLbl.Text = value;
}
}
public void set(string title, string message){
Title = title;
Message = message;
}
In my class, I create an instance of
WaitingDialog
and on processing want to update its labels. I use
backgroundWorker
in my class. And in the methods if I try to set the Message I get InvalidException -
"Cross-thread operation not valid: Control 'msgLbl' accessed from a thread other than the thread it was created on." exception. If I try to set in
backgroundWorker1_RunWorkerCompleted()
, it doesn't throw any exception but doesn't update the labels.
Here is the code :
backgroundWorker1.RunWorkerAsync();
waitDlg = new WaitingDialog("Connecting to Server ...", "");
waitDlg.ShowDialog();
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
ProcessActivity();
}
private void ProcessActivity() {
....
if (ConnectToRandomServer() == false)
activitySuceed = false;
return;
}
private bool ConnectToRandomServer()
{
.....
if (success == false)
{
MessageBox.Show("Unable to connect to any Server. Please try again.");
return false;
}
statusLabel.Text = "Retrieving Data of Server : " + selected.ServerHost;
waitDlg.Message = "Retrieving Data of Server : " + selected.ServerHost;
......
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (activitySuceed == false)
{
statusLabel.Text = "Failed";
backgroundWorker1.CancelAsync();
waitDlg.Close();
return;
}
else
{
waitDlg.set("Saving Data", "");
IOUtility.SaveEncyptedLogin(loginDet);
waitDlg.set("Opening...", "Ultimate VPN Client");
OpenClient();
waitDlg.Close();
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
How can I achieve to set the values in the
waitingDialog
. Why it doesn't update the label text's?
Any help is highly appreciated.
Thanks