background worker
can please refer me article can see how several background worker processes can used in application?
samples see, using 1 in application.
thanks
yes, expected,but, see them being used in action , why asked articles refer to.
thanks
gave code in action only. needed implement event handlers. anyways, give example. suppose want peform following 2 operations on 2 seperate background workers.
1. find numbers between 1 2000000 , add them list (first backgroundworker this)
2. find multiples of 3 between 1 2000000 , add them list (second backgroundworker this)
so here goes code.
using system; using system.collections.generic; using system.componentmodel; using system.windows.forms; using system.threading; namespace multiplebackgroundworkerexample { public partial class sampleform : form { list<int> evennumbers; list<int> multiplesof3; public sampleform () { evennumbers = new list<int>(); multiplesof3 = new list<int>(); initializecomponent(); } private void sampleform _load(object sender, eventargs e) { backgroundworker workertogetprimenumbers = new backgroundworker(); workertogetprimenumbers.dowork += new doworkeventhandler(workertogetprimenumbers_dowork); workertogetprimenumbers.runworkerasync(); backgroundworker workertomultiplesof3 = new backgroundworker(); workertomultiplesof3.dowork += new doworkeventhandler(workertomultiplesof3_dowork); workertomultiplesof3.runworkerasync(); } void workertomultiplesof3_dowork(object sender, doworkeventargs e) { for (int = 0; < 2000000; i++) { if (i % 2 == 0) { evennumbers.add(i); } thread.sleep(200); //this statement added demo pupose only. } } void workertogetprimenumbers_dowork(object sender, doworkeventargs e) { for (int = 0; < 2000000; i++) { if (i % 3 == 0) { multiplesof3.add(i); } } thread.sleep(200); //this statement added demo pupose only. } } }
hope thishelps understand concept.
please mark post answer if solved problem. happy programming!
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment