![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Newbie question on multi-threading I've created a multi-threading program - I have an ArrayList of 10 accounts each of which is represented by an Account object. So I need to create 10 threads to process the accounts (each thread processes one account). The key part of the problem is coded in C# below: private void buttonMultiThead_Click(object sender, EventArgs e) { Account item; for (int i = 0; i < 10; i++) { item = (Account) mAccountList[i]; ThreadStart job = new ThreadStart(ProcessAccount); Thread thread = new Thread(job); thread.Start(); Thread.Sleep(1000); } } static void ProcessAccount(object sender, EventArgs e) { if (mCurrentAccount.Status == "Unprocessed") { mCurrentAccount.Status = "Processed"; } } However, this doesn't work, because I'll need to pass "item" (defined as Account) to the method, "ProcessAccount". But I don't know how to pass this paramter in "ThreadStart". Also, I don't know if my approach in "ProcessAccount" is the right way to launch 10 threads. Would appreciate any advice! |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Newbie question on multi-threading Curious <fir5tsight@xxxxxx> wrote: <snip> Quote: > However, this doesn't work, because I'll need to pass "item" (defined > as Account) to the method, "ProcessAccount". But I don't know how to > pass this paramter in "ThreadStart". Quote: > Also, I don't know if my approach in "ProcessAccount" is the right way > to launch 10 threads. shouldn't be accessing it without synchronization. -- Jon Skeet - <skeet@xxxxxx> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet World class .NET training in the UK: http://iterativetraining.co.uk |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Newbie question on multi-threading Thanks Jon for the pointers! Let me read the article you recommend. |
My System Specs![]() |
| | #4 (permalink) |
| | RE: Newbie question on multi-threading See Jon's reference for how to pass parameters to a thread. I wouldn't recommend 10 threads. I generally only recommend one thread per processor/code. When you introduce more than CPU-bound (i.e. running continuously without going into a wait state) you ensure the system must context switch threads in and out of processors--which is very expensive. You end up causing the system to spend much time context switching instead of doing real work and may end up taking more time that if you hadn't used multiple threads. If you have a single processor that would mean spawning only one background thread that would process the list. With multiple processors I'd spawn one thread per processor that would be responsible for processing part of the list. In cases like this, I would recommend looking at the Parallel Extensions for ..NET, it offers some classes/methods for parallelizing processing of collections like this. -- Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote. http://www.peterRitchie.com/blog/ Microsoft MVP, Visual Developer - Visual C# "Curious" wrote: Quote: > I've created a multi-threading program - I have an ArrayList of 10 > accounts each of which is represented by an Account object. So I need > to create 10 threads to process the accounts (each thread processes > one account). > > The key part of the problem is coded in C# below: > > private void buttonMultiThead_Click(object sender, EventArgs > e) > { > > Account item; > for (int i = 0; i < 10; i++) > { > > item = (Account) mAccountList[i]; > > ThreadStart job = new ThreadStart(ProcessAccount); > Thread thread = new Thread(job); > thread.Start(); > > Thread.Sleep(1000); > > } > } > > > static void ProcessAccount(object sender, EventArgs e) > { > > if (mCurrentAccount.Status == "Unprocessed") > { > mCurrentAccount.Status = "Processed"; > } > > } > > However, this doesn't work, because I'll need to pass "item" (defined > as Account) to the method, "ProcessAccount". But I don't know how to > pass this paramter in "ThreadStart". > > Also, I don't know if my approach in "ProcessAccount" is the right way > to launch 10 threads. > > Would appreciate any advice! > |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Newbie question on multi-threading I have four processors on my computer. So I can start 4 threads? Thanks for the article. I'll take a look. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Newbie question on multi-threading Jon, ParameterizedThreadStart recommeneded in your article is the answer to my question about passing "item" to the "ProcessAccount" method. Thanks! |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Newbie question! | Virtual PC | |||
| Newbie Question #3 | General Discussion | |||
| Newbie question on multi-threading | .NET General | |||
| Vista and multi threading | Vista hardware & devices | |||