• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

backgroundworker blocks form until finished

I have 5 background workers (3 in examplecode), the matterinfofetcher is taking a very long time to load. (It loads items into a combobox) During this time the form is blocked, eg I can open another combox one time and then the form does nothing anymore until the matter-combobox is filled.

Why ? :(
BackgroundWorker adInfoFetcher = null;
BackgroundWorker sqlInfoFetcher = null;
BackgroundWorker sqlInfoFetcher2 = null;
BackgroundWorker sqlInfoFetcher3 = null;
BackgroundWorker matterInfoFetcher = null;


  private void MemoWindow_Load(object sender, EventArgs e)
        {
            adInfoFetcher = new BackgroundWorker();
            adInfoFetcher.DoWork += new DoWorkEventHandler(adInfoFetcher_DoWork);
            adInfoFetcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(adInfoFetcher_RunWorkerCompleted);
            adInfoFetcher.RunWorkerAsync();

            matterInfoFetcher = new BackgroundWorker();
            matterInfoFetcher.DoWork += new DoWorkEventHandler(matterInfoFetcher_DoWork);
            matterInfoFetcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(matterInfoFetcher_RunWorkerCompleted);
            matterInfoFetcher.RunWorkerAsync();

            sqlInfoFetcher = new BackgroundWorker();
            sqlInfoFetcher.DoWork += new DoWorkEventHandler(sqlInfoFetcher_DoWork);
            sqlInfoFetcher.RunWorkerCompleted += new RunWorkerCompletedEventHandler(sqlInfoFetcher_RunWorkerCompleted);
            sqlInfoFetcher.RunWorkerAsync();
}

void matterInfoFetcher_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            FillComboFromStringCollection(cboMatterNr, e.Result as StringCollection);
        }

void matterInfoFetcher_DoWork(object sender, DoWorkEventArgs e)
        {
            string connectionString = "Data Source=CLUSTER\\SQLCLUSTER;Initial Catalog=MATTERS;user id=sa;password=sa; Asynchronous Processing=true";
            string query = "SELECT DISTINCT CUSTOM_ALIAS + ' (' + C_DESCRIPT + ')' AS Expr1 FROM MHGROUP.CUSTOM2 ORDER BY Expr1";
            e.Result = FetchDataFromDB(connectionString, query);
        }

Open in new window

0
dekempeneer
Asked:
dekempeneer
  • 6
  • 4
  • 2
  • +1
1 Solution
 
AndyAinscowCommented:
Based on what you said then the FillComboFromStringCollection function is taking a long time to complete (or being called multiple times from the thread)
0
 
MathiyazhaganCommented:
you should have to set "WorkerReportsProgress" to true  and add "ProgressChanged" event to report current status ; it will make your form to active . In "ProgressChanged" event you can update progress bar or do something to make it unblocked. you should invoke  method ReportProgress of background worker with status percent which will in turn invokes ProgressChanged event.here sample to refer : http://www.codeproject.com/KB/dotnet/BackgroundWorker_sample.aspx
hope this helps.
0
 
dekempeneerAuthor Commented:
I now copied the FillComboFromStringCollection to another one and called it from matterInfoFetcher_RunWorkerCompleted and I still have the same issue. Yes this is taking a whole while to finish :
 private void FillComboMatterStringCollection(ComboBox combo, StringCollection items)
        {
            foreach (string item in items)
            {

                try
                {
                    combo.Items.Add(item);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
            combo.Enabled = true;
            combo.SelectedIndex = 0;

        }

but what can I do about this ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dekempeneerAuthor Commented:
Mathiyazhagan, I wish :) It is kiond of chinese for me :)
plus that example is again one of another hundred with just one BW, where as I have like 5 of those which should all run without interfering the other cbobox or blocking the bloody form.
0
 
AndyAinscowCommented:
void matterInfoFetcher_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           // FillComboFromStringCollection(cboMatterNr, e.Result as StringCollection);
        }

Just for trying to find the problem - does that make any difference?
0
 
dekempeneerAuthor Commented:
Yes it would, the problem is just that the cbomatternr contains too many items in fact, I have no idea of how many but lets say 50000 items.
Loading this takes a while, but I make this combo disabled as long as it is loading and when finished it is enabled. IT is not a problem this one would take so much time, but it should not block the other combos or even the whole form.
btw, after a minute or so when the items are loaded into the combo all works fine again.
0
 
AndyAinscowCommented:
>>I have no idea of how many but lets say 50000 items.

  • That is why it takes a long time and blocks the form
Possible solutions:
Presort the items and don't let the combo sort them as they are entered.
Add the items in groups of say 100 - so the form can do other work as well
Redesign - in reality can the user actually make sense with so many entries, does it actually respond as the user attempts to select an item / scroll the list part of the combo to find something?
0
 
dekempeneerAuthor Commented:
Add the items in groups of say 100 - so the form can do other work as well
and how would I do that ?

Yes the user can make sense as they are sorted by number and the user knows which number to go to.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can at least make the form responsive while the combobox fills with DoEvents():
*I like Andy's comments about limiting the scope of the data though...
        private void FillComboMatterStringCollection(ComboBox combo, StringCollection items)
        {
            foreach (string item in items)
            {

                try
                {
                    combo.Items.Add(item);
                    Application.DoEvents(); // <-- make form responsive
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
            combo.Enabled = true;
            combo.SelectedIndex = 0;
        }

Open in new window

0
 
dekempeneerAuthor Commented:
that solved it , thank you !
0
 
AndyAinscowCommented:
To group into 100's and keep the app responsive --- aaah, you have accepted someone's comment.

Consider using the split option when multiple experts assist, especially when one has answered the original question and you use a suggestion from them.


ps.  I'd still add in batches.  How long does your current code now take to complete adding to the combo ?  Adding in groups of eg. 100 might only take half as long and still keep the app responding 'instantly' to the user.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Yeah...definitely SPLIT in the future...  =)

100 items would definitely add in an "instant" and the DoEvents() wouldn't be necessary at all...
0
 
dekempeneerAuthor Commented:
ok I will do that in the future, or I can open a new question for the "100 items" and you can solve that , than I'll give you the points there ?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now