Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

backgroundworker blocks form until finished

Posted on 2010-09-20
13
Medium Priority
?
324 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 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