Re: Need help with threading..

Tech-Archive recommends: Fix windows errors by optimizing your registry



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!



.



Relevant Pages

  • Re: Reducing the size of executables produced by GNAT
    ... 120K or 45K or 10K CGI executables won't make a difference because the ... The Ada Web Server library from AdaCore was designed ... I do not know how to do with the exceptions runtime. ...
    (comp.lang.ada)
  • Re: c++ web scripting
    ... > a compiler, produce an executable, put it on a server, and stroll away. ... the executables themselves are cached, not the HTML output of those ... Web server invokes the CGI handler for .cpp files, ServeCPPFile. ...
    (comp.lang.cpp)
  • Re: Globals not deleted
    ... project included a DLL COM server with a thread pool. ... UTILEXPORT LONG Unlock; ... The COM server worked as expected. ... DLL, the server never shutdown so, even when using regsvr32.exe to ...
    (microsoft.public.vc.atl)
  • Re: authenticating remote peer question
    ... InitializeSecurityContext - you get data to send to the server ... AcceptSecurityContext - you may get data to return to the client ... Microsoft MVP, MCSD ... I have a service which accepts executables send by remote machine and ...
    (microsoft.public.win32.programmer.networks)
  • Re: Need help with threading..
    ... it is an out-of-process server. ... MyKindOfObject myObject = new MyKindOfObject; ... You said that they are COM executables which honestly, ... If this was a regular program that you were calling through the Process ...
    (microsoft.public.dotnet.languages.csharp)