BackgroundWorker not triggering RunWorkerCompleted if Argument (array) changes size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cvoce
    New Member
    • Feb 2013
    • 1

    BackgroundWorker not triggering RunWorkerCompleted if Argument (array) changes size

    Hi,

    I am trying to go off and collect data on a regular basis, which can take time, then once I have collated it all, use it in the GUI thread. The data can change - sometimes only 1 message, sometimes several.

    If I use a simple example and pass a string in to RunWorkerAsync( ) then change the string by setting e.Result in the DoWork function, RunWorkerComple ted gets called (and has the new string in e.Result).

    If I init an array as empty and pass this to RunWorkerAsync, then populate this array, changing its size with Array.Resize() and pass this result back as e.Result, then RunWorkerComple ted never fires.

    What am I doing wrong?

    Code example:

    Code:
    void main()
    {
      string[][] test = new string[0][];
      bgw.RunWorkerAsync(test)
    }
    
    void DoWork(object state, DoWorkEventArgs e)
    {
      string[][] test = e.Argument as string[][];
      Array.Resize(ref test,2);
      Array.Resize(ref test[0],2);
      Array.Resize(ref test[1],2);
      test[0][0] = "first feed, first message";
      test[0][1] = "first feed, second message";
      test[1][0] = "second feed, first message";
      test[1][1] = "second feed, second message";
    
      e.Result = test;
    }
    
    void RunWorkerCompleted(object state, RunWorkerCompletedEventArgs e)
    {
      //this never fires at all!!
      string[][] test = e.Result as string[][];
      textBox1.Text = test[0][0];
    }
    Stepping through the code, e.Result looks correct immediately after setting it in DoWork, but then the app hangs and RunWorkerComple ted never fires.

    All help gratefully received.
    Last edited by acoder; Feb 22 '13, 12:23 AM. Reason: Please use [code] tags when posting code
Working...