Odd Thread Problem

  • Thread starter Thread starter NWeller
  • Start date Start date
N

NWeller

Well, I think it is odd.


I have written an application that fires off requests at a COM
component written in VB6. I am creating multiple threads, one for each

test.

I know that the threads are being created, I know that the calls are
being made to the COM component, I know that the COM component is being

called and is returning a result.

What is going wrong is that if I create 1 thread then the mEvent.Set
command in processThread does not ever get reached and the result is
that WaitAll never finishes.

If I create 6 threads, all of them fire mEvent.Set except for the first

thread.

If I create 10, all complete except the first one.

I have been looking at this for a few hours and I cannot see what is
going wrong. I have got MTAThread set and I have no idea why this
happening, I have found no pointers searching.

If anyone can explain what I am doing wrong then I would really
appreciate it, I assume it is something daft that I have done. I
suspect that the first thread is sitting there waiting to do something
but nothing is happening and no exceptions are being raised.

Thanks in advance,
Neil

Part of the code follows:

public PCTestScript RunTests()
{
Thread[] threadArray = new Thread[m_Threads];
ManualResetEvent[] manualEvents = new
ManualResetEvent[m_Threads];
WaitHandle[] waitHandle = new WaitHandle[m_Threads];

for(int i = 0; i < m_Threads; i++)
{
manualEvents = new ManualResetEvent(false);
//waitHandle = (WaitHandle) manualEvents;

PCThread pcThread = new PCThread(manualEvents);
pcThread.TestScriptRow=m_pcts.­PCTestScriptList;
threadArray = new Thread(new
ThreadStart(pcThread.processTh­read));
//Code below...
threadArray.Name=i.ToString­();
threadArray.Start();
}

// wait for the work items to signal before exiting.
ManualResetEvent.WaitAll(manua­lEvents);
Console.WriteLine("Finished - Yahoo");

return m_pcts;
}


public void processThread()
{
try
{
PCInterface pcInterface = new PCInterface();
//Call to Com Component....

//This does not get called the FIRST time, why?
mEvent.Set();
}
catch (Exception ex)
{
Console.Out.WriteLine("It errored");
}
}
 
Well, I think it is odd.


I have written an application that fires off requests at a COM
component written in VB6. I am creating multiple threads, one for each

test.

I know that the threads are being created, I know that the calls are
being made to the COM component, I know that the COM component is being

called and is returning a result.

What is going wrong is that if I create 1 thread then the mEvent.Set
command in processThread does not ever get reached and the result is
that WaitAll never finishes.

If I create 6 threads, all of them fire mEvent.Set except for the first

thread.

If I create 10, all complete except the first one.

I have been looking at this for a few hours and I cannot see what is
going wrong. I have got MTAThread set and I have no idea why this
happening, I have found no pointers searching.

If anyone can explain what I am doing wrong then I would really
appreciate it, I assume it is something daft that I have done. I
suspect that the first thread is sitting there waiting to do something
but nothing is happening and no exceptions are being raised.

Thanks in advance,
Neil

Part of the code follows:

public PCTestScript RunTests()
{
Thread[] threadArray = new Thread[m_Threads];
ManualResetEvent[] manualEvents = new
ManualResetEvent[m_Threads];
WaitHandle[] waitHandle = new WaitHandle[m_Threads];

for(int i = 0; i < m_Threads; i++)
{
manualEvents = new ManualResetEvent(false);
//waitHandle = (WaitHandle) manualEvents;

PCThread pcThread = new PCThread(manualEvents);
pcThread.TestScriptRow=m_pcts.­PCTestScriptList;
threadArray = new Thread(new
ThreadStart(pcThread.processTh­read));
//Code below...
threadArray.Name=i.ToString­();
threadArray.Start();
}

// wait for the work items to signal before exiting.
ManualResetEvent.WaitAll(manua­lEvents);
Console.WriteLine("Finished - Yahoo");

return m_pcts;
}


public void processThread()
{
try
{
PCInterface pcInterface = new PCInterface();
//Call to Com Component....

//This does not get called the FIRST time, why?
mEvent.Set();
}
catch (Exception ex)
{
Console.Out.WriteLine("It errored");
}
}


Why creating them from MTA threads?
VB6 COM objects are STA objects. Save yourself a lot of troubles and create
them in an STA.

Willy.
 
Thanks for the reply Willy.

It is going to call lots of different COM components running under
COM+.

I've actually removed all references to the COM Objects and the first
thread still doesn't ever finish. I am really confused about this one.

Neil
 
NWeller said:
Thanks for the reply Willy.

It is going to call lots of different COM components running under
COM+.

I've actually removed all references to the COM Objects and the first
thread still doesn't ever finish. I am really confused about this one.

Neil

It doesn't matter wheter you call alot of COM components from different
threads, You should try to match the callers apartment to the components
apartment, if you don't, you will incur a performance hit because of
inter-apartment/thread marshaling. Other issues you will encounter are
security related, don't forget that the STA thread (you didn't create) where
the COM object lives doesn't carry the thread identity of the caller (MTA
thread).
That said, I'm unclear why the first thread blocks (are you sure he
blocks?), my guess is that the thread procedure returns before the WaitAll
is called.
BTW. what is the apartment the WaitAll call runs in?

Willy.
 
Back
Top