Re: Need help with threading..
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 26 Jul 2007 22:23:20 -0400
Here is the problem, the threads in the thread pool are part of the MTA apartment. This might not work with your out of process COM server.
You might have to create a new thread, and explicitly call SetApartmentState on the Thread before you call the code so that you get the apartment threading correct.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<bean330@xxxxxxxxx> wrote in message news:1185502502.161351.41300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks, Nicholas. You're right, it is an out-of-process server.
So, I've been doing more reading about the ThreadPool, and I think it
is the right thing for me (I have a few minor concerns, but I'll live
with them).
So here's a sample of what I think I need to do:
int i;
int j;
j = numRecords;
for (i = 0; i < j; i++)
{
MyKindOfObject myObject = new MyKindOfObject();
myObject.propertyA = "red";
myObject.propertyB = "blue";
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc), myObject);
}
static void ThreadProc(Object stateInfo)
{
MyKindOfObject myObject = (MyKindOfObject) stateInfo;
myObject.CallMe();
}
Does this look generally right? that line
MyKindOfObject myObject = (MyKindOfObject) stateInfo;
looks a little odd to me, but I suppose I'll give it a go at work
tomorrow.
On Jul 26, 9:29 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
What do you need the threads for if you are really executing the process
in another thread? You said that they are COM executables which honestly, I
don't know what that is, unless you mean it is a COM out-of-process server,
which is really the term you are going for.
If this was a regular program that you were calling through the Process
class, then threads wouldn't be an issue, since you are handling the work in
another process, and only need to be notified when the process is done. You
don't need another thread to do this.
But if this is an out of process server, then you are making calls in
process which are being marshaled to the other process. In this aspect, it
is no different than calling any other object that does work, since you have
to wait for the call to return. I would use the ThreadPool as you suggest,
and in your code that is run in the thread pool, when you are done making
the call, place another entry in the thread pool.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@xxxxxxxxxxxxxxxxxxxxxxxxxxx
<bean...@xxxxxxxxx> wrote in message
news:1185497734.193146.66140@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hey, I'm somewhat new to C# and I need a little help, please!
> I'm selecting a bunch of records, setting properties on a COM
> executable and then calling a method on that executable to run. I
> want to run the executable in separate threads because they can be
> long-running and it would be optimal for us to run a bunch
> simultaneously.
> I've got that part working - it's pretty easy in C#. What I'm having
> a hard time with is managing the threads. That is, I am using a
> dynamic number of threads and I'm unclear on how to set off these
> executables in x number of threads, and then wait for one of those
> threads to free up before attempting to send off another. I've looked
> a little at the ThreadPool, but I'm unclear if this is how I should be
> proceeding.
> Any advice would be greatly appreciated. Thanks!
.
- Follow-Ups:
- Re: Need help with threading..
- From: bean330@xxxxxxxxx
- Re: Need help with threading..
- References:
- Need help with threading..
- From: bean330
- Re: Need help with threading..
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Need help with threading..
- From: bean330@xxxxxxxxx
- Need help with threading..
- Prev by Date: Re: Need help with threading..
- Next by Date: Check If Commit Works Correctly
- Previous by thread: Re: Need help with threading..
- Next by thread: Re: Need help with threading..
- Index(es):
Relevant Pages
|