Temporarily switch to STA threading - how???

  • Thread starter Thread starter Peter Row
  • Start date Start date
P

Peter Row

Hi,

I think I am having some threading issues in a project I am porting from VB6
webclass to VB.NET.
Therefore I would like to temporarily set the threading mode to STA to get
around it until I have
fully ported and tested all code.

I have class that implement IHttpFactory, thus I have a GetHandler method.
However if I try to use the [STAThread] directive, like this:

Public Class MyFactory Implements IHttpHandler

[STAThread] Public Overridable Function GetHandler( ..... )

.... I then get the error that I haven't implemented GetHandler.

If as the first line of my GetHandler implementation I say:

CurrentThread.ApartmentState = CurrentThread.AppartmentState.STA

Then it does not change the threading model used it stays on MTA.

Anybody have any ideas on how I might achieve this?

Regards,
Peter
 
Hi,

It's okay I found another suitable solution.
I used a SyncLock which is adequate for this temporary hack whilst porting.

Regards,
Peter
 
I was going to say, you really can't switch back and forth from STA to
MTA/free-threading.
Part of the reason is that under the covers the system will at some points
likely call CoInitialize, and you can't initialize COM in the same process
with different threading models.

-Rob Teixeira [MVP]

Peter Row said:
Hi,

It's okay I found another suitable solution.
I used a SyncLock which is adequate for this temporary hack whilst porting.

Regards,
Peter
Peter Row said:
Hi,

I think I am having some threading issues in a project I am porting from VB6
webclass to VB.NET.
Therefore I would like to temporarily set the threading mode to STA to get
around it until I have
fully ported and tested all code.

I have class that implement IHttpFactory, thus I have a GetHandler method.
However if I try to use the [STAThread] directive, like this:

Public Class MyFactory Implements IHttpHandler

[STAThread] Public Overridable Function GetHandler( ..... )

... I then get the error that I haven't implemented GetHandler.

If as the first line of my GetHandler implementation I say:

CurrentThread.ApartmentState = CurrentThread.AppartmentState.STA

Then it does not change the threading model used it stays on MTA.

Anybody have any ideas on how I might achieve this?

Regards,
Peter
 
Back
Top