G
Guest
I come from the land of Java and C++** where it is relatively easy to create
a pool of threads, send them off to a task, then call the join method and
await all of their completion. However, I cannot for the life of me figure
out how to get this relatively simple concept working in .net - specifically
C#.
I posted some code earlier where I was using ThreadPool.QueueUserWorkItem -
I couldn't figure out how to get my code to wait for the threads to finish
execution without doing some potentially dangerous coding that wouldn't
really work on a multi-processor machine (look up volatile memory for why
this is the case).
However, I read elsewhere that I could use the BeginInvoke and EndInvoke on
a delegate to address this need. However, I am having the exact same problem
as before - now using the delegate my code looks like this:
-----------------------------
MyClass myObj = new MyClass (doTask);
for (int i = start; i <= end; i++)
{
try
{
myObj.BeginInvoke (i,
new AsyncCallback (displayResultOfMethod),
i);
}
catch (Exception ex)
{
m_textBox.AppendText("An error occurred");
}
}
//would like to block here until all of the above threads have finished
-----------------------------
Can someone please show me the code I need to add to get this to work. Also
please do not dwell on these aspects of my message:
1. Thread pools and joins in C++ (see the explanation at the end of this
post as to how I did this in C++ if this is an issue for you).
2. Dangerous coding and volatile memory - in my previous post I suggested a
way I could address my need - but my suggested solution was poor since it
could cause problems in a multi-processor environment - also it wouldn't be
elogant since I would have to do polling to determine when the threads
finished.
Please just let me know what code I'm missing so I can do that simple
concept of blocking until a pool of threads have finished.
Thanks and I'm sorry if this post sounds demanding/argumentative it is just
that my experience on newsgroups is that unless you are very specific then
you will not get an answer to your problem.
Thanks,
Novice
**For C++ I had written my own library specific to a particular OS (since
the standard doesn't specify things like threads) that had a thread pool API
for joins.
a pool of threads, send them off to a task, then call the join method and
await all of their completion. However, I cannot for the life of me figure
out how to get this relatively simple concept working in .net - specifically
C#.
I posted some code earlier where I was using ThreadPool.QueueUserWorkItem -
I couldn't figure out how to get my code to wait for the threads to finish
execution without doing some potentially dangerous coding that wouldn't
really work on a multi-processor machine (look up volatile memory for why
this is the case).
However, I read elsewhere that I could use the BeginInvoke and EndInvoke on
a delegate to address this need. However, I am having the exact same problem
as before - now using the delegate my code looks like this:
-----------------------------
MyClass myObj = new MyClass (doTask);
for (int i = start; i <= end; i++)
{
try
{
myObj.BeginInvoke (i,
new AsyncCallback (displayResultOfMethod),
i);
}
catch (Exception ex)
{
m_textBox.AppendText("An error occurred");
}
}
//would like to block here until all of the above threads have finished
-----------------------------
Can someone please show me the code I need to add to get this to work. Also
please do not dwell on these aspects of my message:
1. Thread pools and joins in C++ (see the explanation at the end of this
post as to how I did this in C++ if this is an issue for you).
2. Dangerous coding and volatile memory - in my previous post I suggested a
way I could address my need - but my suggested solution was poor since it
could cause problems in a multi-processor environment - also it wouldn't be
elogant since I would have to do polling to determine when the threads
finished.
Please just let me know what code I'm missing so I can do that simple
concept of blocking until a pool of threads have finished.
Thanks and I'm sorry if this post sounds demanding/argumentative it is just
that my experience on newsgroups is that unless you are very specific then
you will not get an answer to your problem.
Thanks,
Novice
**For C++ I had written my own library specific to a particular OS (since
the standard doesn't specify things like threads) that had a thread pool API
for joins.