Solved

backgroundworker blocks form until finished

Posted on 2010-09-20
13
316 Views
Last Modified: 2013-12-16
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
Comment
Question by:dekempeneer
  • 6
  • 4
  • 2
  • +1
13 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33715770
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
 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 33715906
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
 

Author Comment

by:dekempeneer
ID: 33715925
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
 

Author Comment

by:dekempeneer
ID: 33716065
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33716477
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
 

Author Comment

by:dekempeneer
ID: 33716513
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33716593
>>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
 

Author Comment

by:dekempeneer
ID: 33716615
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 33716986
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
 

Author Closing Comment

by:dekempeneer
ID: 33717013
that solved it , thank you !
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 33718548
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33718979
Yeah...definitely SPLIT in the future...  =)

100 items would definitely add in an "instant" and the DoEvents() wouldn't be necessary at all...
0
 

Author Comment

by:dekempeneer
ID: 33723358
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now