473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Need help with threading..

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
5 2006
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
4
by: Philip Smith | last post by:
Hi I am fairly new to Python threading and my needs are simple(!) I want to establish a number of threads each of which work on the same computationally intensive problem in different ways. ...
1
by: Your Friend | last post by:
Hello All, I'm having issues capturing the output from a program while using threading. Program runs ok when I run without threading. Here's my Python code and the Java class that is called by...
6
by: hzgt9b | last post by:
Using VS 2003, .NET: I developed a windows application that performs several actions based on an input file. The application displays a progress bar as each action executes. Based on new...
3
by: bilosta | last post by:
Hello to everybody I'm new in win apllications and C#. I have a problem with Threading. Here is my problem: I have a form with button named: BupisiStudente, when I click on it a call next...
0
by: smimon | last post by:
Hi I'm trying to run a DTS package from a ASP.NET web page using System.Diagnostics.Process. This DTS takes up to 10 minutes to complete, during which, output is generated which i would like to...
1
by: Nico Blodow | last post by:
Hi all, I hope this hasn't been brought up before, but if it did i missed it, so bear with me please :) anyway, i'm trying to embed Python into a C program. A sample python syript i want to run...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.