Matme

Hi,

I'm relatively new to .NET and C#. I need to make use of a third party API (COM) that mandates STA. The usage results in a long running process that I want to perform in the background (i.e. another thread) . I want the background thread to send a completed event that can be used to release resources and finally set the thread pointer null. The Async methods provide the functionality that I need but because the ThreadPool is MTA I suspect that I can't make use of them. Does anybody have any suggestions, examples, links, etc that might help me get going
Cheers,
Matt






Re: Visual C# General ThreadPool and STA?

Kent Boogaart

Hi Matt,

You could start your own STA thread:

Code Block

Thread t = new ...;

t.SetApartmentModel(ApartmentModel.STA);

t.Start();


As you've found, the ThreadPool threads are all using MTA (the default) so you won't be able to leverage them (and you typically wouldn't want to anyway for long background processes).

HTH,
Kent




Re: Visual C# General ThreadPool and STA?

Matme

Hi Kent,

Thanks for your reply. I'm thinking about something like this:
Code Block

Thread t;


// ...


void CreateThread()

{

t = new Thread(

delegate()

{

OnRunning();
try
{
DoWork();
}
catch (Exception ex)
{
OnException(ex);
}
finally
{
OnCompleted();
}
});
t.SetApartmentState(ApartmentState.STA);

}


// ...


void OnCompleted()

{

// release resources etc

t = null;

}


// ...



In the OnCompleted() method, I'd really like to wait for the thread to enter the stopped state (e.g a thread join) and then release all the resources (e.g. t = null). I think I'm going to have to use a second thread and make use of EventWaitHandle to achieve this...

Cheers,
Matt






Re: Visual C# General ThreadPool and STA?

Kent Boogaart

Hi Matt,

Firstly, setting t to null does not release any resources. It merely makes the Thread object elligible for garbage collection, assuming it isn't already (which may be the case solely because you've stored a reference to it as a class member).

Also, I'm not sure why you think you need a wait handle to achieve your resource clean up. Can't you just do this:

Code Block

void CreateThread()

{

Thread t = new Thread(

delegate()

{

OnRunning();
try
{
DoWork();
}
catch (Exception ex)
{
OnException(ex);
}
finally
{
OnCompleted();
}
});
t.SetApartmentState(ApartmentState.STA);

t.Start(); // :)

}


// ...


void OnCompleted()

{

// release resources etc - or move this code into the finally block if it's simple

// no need to clean up thread - it will be automatically when it terminates

}


// ...


You don't need to keep a reference to the Thread. It won't be garbage collected until it terminates.

HTH,
Kent




Re: Visual C# General ThreadPool and STA?

Matme

Hi Kent,

It's not clear from the pseudo code that I posted but the class will encapsulate a single long running command that may be called more than once in it's lifetime. To prevent the same command from being started again until it had completed I was going to test the thread ref for null and IsAlive, but I could also use a boolean flag.
I've since realised that creating a new thread each time the command is needed to run is a bit to excessive and that a producer/consumer pattern (thread pool) may be more suitable.

Cheers,
Matt