Newbie question on multi-threading

C

Curious

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;

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!
 
J

Jon Skeet [C# MVP]

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
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.
 
P

Peter Ritchie [C# MVP]

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.
 
C

Curious

I have four processors on my computer. So I can start 4 threads?

Thanks for the article. I'll take a look.
 
C

Curious

Jon,

ParameterizedThreadStart recommeneded in your article is the answer to
my question about passing "item" to the "ProcessAccount" method.
Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

threading 2
Start a new thread to show form - Form never shows 3
.NET Threading Question 17
media with threading 1
Using Multi threading 1
timer/threading 10
Threading - issues 11
C# Threading 8

Top