Click here to Skip to main content
15,888,082 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
C#
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 :
// Starting backgroundWorker
backgroundWorker1.RunWorkerAsync();
// Creating WaitingDialog instance
waitDlg = new WaitingDialog("Connecting to Server ...", "");
waitDlg.ShowDialog();

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    ProcessActivity();
}

private void ProcessActivity() {
   ....
   // CONNECT TO Server
   if (ConnectToRandomServer() == false)
       activitySuceed =  false;
   return;   
}

private bool ConnectToRandomServer()
{
   .....
   if (success == false)
   {
    MessageBox.Show("Unable to connect to any Server. Please try again.");
    // HERE IT THROWS EXCEPTION 
    //waitDlg.Message = "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
    {
       // THIS WORKS PROPER - B'COZ THREAD IS COMPLETED !!
        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
Posted
Updated 8-Feb-11 22:19pm
v3

BackgroundWorker1.WorkerReportsProgress = True; and try to update it in BackgroundWorker1_ProgressChanged

You need to show some more code where and how you try to set the message.

Or you may look at How to solve "Cross thread operation not valid"[^] to get an idea. It's in VB but you can definitely get some idea.
 
Share this answer
 
v2
Comments
All Time Programming 9-Feb-11 4:16am    
Thanks. Had a look at the link, but didn't get it .CODE has been added above from "Here is the code :". I will have 2 types of messages : "Retrieving data from ..." & "Falure" that are used in methods that are called via backgroundWorker thread. How do I add delegate, I don't know. Do you mean that whenever I want to change the message I should set the msg in some variable and somehow call progresschanged and add waitDlg.Message = newMessage; ??? Kindly guide me. - Thanks
All Time Programming 9-Feb-11 5:01am    
Perak Patel, Thanks a lot. Yes I implemented waitDlg.Message in backgroundWorker1_ProgressChanged and for calling backgroundWorker1.ReportProgress(0, message). Thanks a lot once again.
Prerak Patel 9-Feb-11 5:09am    
You are welcome.
I answered very similar question before, please see the code: Changes step by step to show a control[^].

You will need to work with your server is the background worker, of course.
Another solution would be having a permanent thread created from the very beginning and post UI changes from that thread. If the lifetime session working with the servers is about lifetime of your application, this approach is much better.

—SA
 
Share this answer
 
Comments
All Time Programming 10-Feb-11 1:04am    
Thanks SA. My work is like on a btn click, a activity is done that takes some time. I show a waiting dailog (another component) to let the user know that some & what process is going beyond. Based on the process of the activity, i wanted to update the labels of WaitingDialog dialog. In this case, what Prerak Patel showed worked best for me. Thanks SA.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900