By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,115 Members | 894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,115 IT Pros & Developers. It's quick & easy.

Need help with threading..

P: n/a
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!

Jul 27 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

<be*****@gmail.comwrote in message
news:11*********************@r34g2000hsd.googlegro ups.com...
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!
Jul 27 '07 #2

P: n/a
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...@spam.guard.caspershouse.comwrote:
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...@spam.guard.caspershouse.com

<bean...@gmail.comwrote in message

news:11*********************@r34g2000hsd.googlegro ups.com...
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!

Jul 27 '07 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

<be*****@gmail.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
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...@spam.guard.caspershouse.comwrote:
> 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...@spam.guard.caspershouse.com

<bean...@gmail.comwrote in message

news:11*********************@r34g2000hsd.googlegr oups.com...
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!

Jul 27 '07 #4

P: n/a
Oh, darn. In my original stab at this, I was creating the threads and
calling SetApartmentState, and it worked fine, except I couldn't
figure out how to manage multiple threads, ie., if I want to process
50 records using 5 threads, how do I know when I go to process record
6 that one of those threads is free? My head was spinning reading all
the threading material out there, but none seemed to clearly address
this issue (at least in a way that I could understand). ThreadPool
seemed like such a great solution to this problem.

Well, thanks for your time and your help, Nicholas, and if you or
anyone else happens to have any suggestions, I'll be checking back
here regularly.

On Jul 26, 10:23 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

<bean...@gmail.comwrote in message

news:11*********************@19g2000hsx.googlegrou ps.com...
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...@spam.guard.caspershouse.comwrote:
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...@spam.guard.caspershouse.com
<bean...@gmail.comwrote in message
>news:11*********************@r34g2000hsd.googlegr oups.com...
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!

Jul 27 '07 #5

P: n/a
Well, you could still have five threads processing the queue, you just
have to synchronize access to the queue.

So, you spin off your five threads, and they each pull an item from the
queue and work on it. When they are done, they check the queue again. If
there is an item, pull it off, and process. Each thread does this until the
queue is empty.

Of course, you have to make sure that access to the queue is
synchronized, but that's simple enough.

Then, if you need to know when all the processing is complete, you can
have each thread signal an event, and then you just wait on all the events.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<be*****@gmail.comwrote in message
news:11**********************@b79g2000hse.googlegr oups.com...
Oh, darn. In my original stab at this, I was creating the threads and
calling SetApartmentState, and it worked fine, except I couldn't
figure out how to manage multiple threads, ie., if I want to process
50 records using 5 threads, how do I know when I go to process record
6 that one of those threads is free? My head was spinning reading all
the threading material out there, but none seemed to clearly address
this issue (at least in a way that I could understand). ThreadPool
seemed like such a great solution to this problem.

Well, thanks for your time and your help, Nicholas, and if you or
anyone else happens to have any suggestions, I'll be checking back
here regularly.

On Jul 26, 10:23 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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]
- m...@spam.guard.caspershouse.com

<bean...@gmail.comwrote in message

news:11*********************@19g2000hsx.googlegro ups.com...
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...@spam.guard.caspershouse.comwrote:
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...@spam.guard.caspershouse.com
><bean...@gmail.comwrote in message
>>news:11*********************@r34g2000hsd.googleg roups.com...
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!


Jul 30 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.