Worker Thread not running and is returning a status of stopped!!!

J

Jim Macbeth

Hi,
We have some code that converts uploaded files from the uploaded type
(word doc etc) to Text and HTML.

The code is as follows:

// Create new thread with STA ApartmentState to invoke the COM objects
Thread cft = new Thread(new ThreadStart(convertFileThread));
cft.ApartmentState = ApartmentState.STA;
cft.Name = "ConvertTo" + destinationType;

convertState = ConvertState.Started;
sThreadMessage = "Could not start conversion thread";
cft.Start(); // Do it

cft.Join(300000); // Wait a maximum of convertTimeout milliseconds
(300000 miliseconds = 5 mins)

if (convertState == ConvertState.Completed)
{
return null;
}
else if (convertState == ConvertState.Running)
{
return string.Format("File conversion could not complete in {0}
seconds", (convertTimeout/1000));
}
else
{
sThreadMessage += "(convert state: "+convertState.ToString()+", thread
state: "+cft.ThreadState.ToString()+")";
return sThreadMessage;
}

This works fine on all servers we have installed the code on except one.

On that server the convertFileThread delegate function never starts. The
code runs through to the last else clause and returns
the following error:

Could not start conversion thread (convert state: Started, thread state:
Stopped)

Is there anything in asp.net that prevents client threads from starting?

Please note that this isn't my code. I have inherited it.

Anyway I am well confused so any help would be most appreciated.

The server having the problem and others that this code works fine on are
running .NET v1.0.3705

Thanks in advance,

Jim
 
N

Natty Gur

Hi,

Can you send more exaption details (stacktrace will be great).

Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
A

Alvin Bruney

I see some problems in the code.

It isn't that the thread isn't started. The thread has started but it state
was changed to stopped by a thread (probably itself, probably another
thread). Are you protecting access to the converState? This would be my
first question. Secondly, you assume the thread has started by assigning
ConvertState.Started but any number of conditions can cause the thread to
not start throwing you off queue. I suggest you pass the thread state in as
the thread parameter and let the thread change this state to started. This
way, when the thread says it has started, it really has started. Or a
convertState.Init would be a good replacement before you call .Start().

You also can't lump all thread conditions into an else block because then
you don't really know what happened to the thread if it hasn't completed or
isn't running. I suggest you refine that if block to take in the other
conditions as well. I'd probably do that before mucking with any code. I am
reasonably sure that the thread has run and has stopped because the thread
state was converte.Started before the start() was called and now it is set
to stopped with an appended error message. That should clue you in that you
need to refine your if block more granular.

It would be good to see what the thread delegate actually contained. Can you
post this? Or key portions of it?
regards
 
J

Jim Macbeth

Alvin,
Thanks for the help.

This isn't my code and I have inherited it and your right in saying that
access to the private vars should be protected.

We have just been changing the db structure of the application and been
concentrating on that conversion only.
We have a dedicated team testing this conversion and at various points there
are file uploads that get converted to HTML and Text. To do this we user
Wordport via a COM component. This is the sole reason for the STA thread.
I think its a little overkill because there is only one call made to the COM
and I am presuming the original developers of this code were concerned with
the proxy/stub interop performance. If this is true then that I feel is
overkill for 1 COM function Call.

The delegate function is as follows:

/// <summary>
/// Method to run COM based file conversion in a separate Thread with STA
ApartmentState
/// </summary>
private void convertFileThread()
{
try
{
convertState = ConvertState.CreatingCOMObjects;
sThreadMessage = "Could not create COM objects";

WordPort.Library oWordPort = new WordPort.Library();
convertState = ConvertState.Running;

oWordPort.ConvertDocument(sSourcePath, sDestinationType,
sDestinationPath, "","");

sThreadMessage = "";
convertState = ConvertState.Completed;
}
catch (Exception er)
{
sThreadMessage = er.Message;
convertState = ConvertState.Failed;
}
}

This WordPort.Library COM also does some impersonation which could be the
other reason why a new thread is used but I am not sure.
I will ask the original development team to comment on this.

I feel like re-writing the whole thing, including the COM but I don't have a
great deal of time to do this but it may come to this.

It works perfectly locally when debugging and on all but this one server.

Anyway thanks for any further help.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top