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