Solved

backgroundworker blocks form until finished

Posted on 2010-09-20
13
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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
 
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 86

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 86

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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