views:

126

answers:

2

what is wrong with this code below? The conn_PageDeleted is coming from a background thread and i am trying to update a label every time i get a call back. I get an error stating

Parameter count mismatch.

Here is the code:

    private void cmdDeletePage_Click(object sender, EventArgs e)
    {
         worker = new BackgroundWorker();
         worker.DoWork += new DoWorkEventHandler(worker_DoWork);
         worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
         worker.RunWorkerAsync();
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        lblDeleteStatus.Text = "";
        MessageBox.Show("Complete");
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Connecter conn = new Connecter("a", "m");
        conn.PageDeleted += new Connecter.PageDeletedHandler(conn_PageDeleted);
        bool success = conn.DeletePage(txtPageToDelete.Text, chkRecursive.Checked);
    }


    public delegate  void UpdateLabelHandler(object sender, string name);

    void conn_PageDeleted(object sender, string name)
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new UpdateLabelHandler(UpdateMe));
        }
        else
        {
            lblDeleteStatus.Text = name;                
        }
    }

    private void UpdateMe(object sender_, string name_)
    {
        lblDeleteStatus.Text = name_;
    }
+1  A: 

Your delegate has to match the signature of the event handler, something like this:

public delegate void UpdateLabelHandler(object sender, string strArgs);

Edit: Since you have edited the code to include this ... I will amend this accordingly.... Looking at your edited code, I have to question this:

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        Connecter conn = new Connecter("a", "m");
        conn.PageDeleted += new Connecter.PageDeletedHandler(conn_PageDeleted);
        bool success = conn.DeletePage(txtPageToDelete.Text, chkRecursive.Checked);
    }

You are wiring up a 'PageDeleted' event handler....and call 'DeletePage' method after it, I presume that in turn invokes the event handler 'conn_PageDeleted' within the 'DoWork' body, it goes out of scope when the 'BackgroundWorker' thread is finished...and since 'conn' is in local scope of the 'worker_DoWork' method, that gets destroyed, and somehow your event handler gets messed up! Can you confirm this?

Hope this helps, Best regards, Tom.

tommieb75
@tommieb75 - i have that (updated above)
ooo
There could possibly be a race condition in the thread itself,after calling DeletePage, the thread goes out of scope, then an event that gets fired, it may/may not get trapped by your event handler and hence blows up...that's my feeling of it...have you tried adding a delay within the end of the 'worker_DoWork' method, a Thread.Sleep() perhaps? This will confirm that your event is handled and working after a delay. If not, the money is on the DeletePage somehow. Wrap up the code in try/catch blocks to help you...
tommieb75
+2  A: 

You should pass the parameters to the UpdateMe method, try this:

void conn_PageDeleted(object sender, string name)
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new UpdateLabelHandler(UpdateMe), new object[] {sender, name}); //<-- the update goes here
        }
        else
        {
            lblDeleteStatus.Text = name;                
        }
    }
Sameh Serag