Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Newbie question on multi-threading

Reply
 
Old 03-16-2008   #1 (permalink)
Curious


 
 

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 SpecsSystem Spec
Old 03-17-2008   #2 (permalink)
Jon Skeet [C# MVP]


 
 

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".
See http://pobox.com/~skeet/csharp/threads/parameters.shtml
Quote:

> Also, I don't know if my approach in "ProcessAccount" is the right way
> to launch 10 threads.
mCurrentAccount looks like it's mutable shared data - which means you
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 SpecsSystem Spec
Old 03-17-2008   #3 (permalink)
Curious


 
 

Re: Newbie question on multi-threading

Thanks Jon for the pointers!

Let me read the article you recommend.
My System SpecsSystem Spec
Old 03-17-2008   #4 (permalink)
Peter Ritchie [C# MVP]


 
 

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 SpecsSystem Spec
Old 03-17-2008   #5 (permalink)
Curious


 
 

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 SpecsSystem Spec
Old 03-17-2008   #6 (permalink)
Curious


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46