John Wesley Harding

I have a service which runs on a Vista machine and has a number of other projects as part of the same solution, which are applications, dll's etc in their own right but the service controls them via code and therefore is referenced neccesarily. All of this works fine but the problem is for instance, when the service fire's up a new instance of an application and the application executes code which shows a file browser dialog I get an error that only STA threads can use these type of commands.

I tried settings the STA state of the thread which its fired on but with strange results (it would actually show the dialog before throwing the error then would throw the error on my 'if (dailogresult)' line stating 'Object reference not set...')

Any help would be great.

On another note this solution, as mentioned has many projects, and some of the forms controls of the applications use the same images and icons, is there a way I can have a 'resource project' that I could reference and be able to assign instances to say, an imagelist


Re: Visual C# General STA Threads and Dialogs

Peter Ritchie

What do you mean "the thread which its fired". The application has it's own thread, which should default to STA (you can ensure that with an attribute on the Program.Main method). You can't change a thread from MTA to STA within the thread; it's already running.




Re: Visual C# General STA Threads and Dialogs

John Wesley Harding

Well I would of thought it had its own thread, but calling the Main method of an application does not seem to load the applications interface even though the code is executed, so to get around this I create a new thread a execute the Main method of the application which works fine but like I said, when I wish to use dialogs an exception is thrown.

I know you cant change the state once started but you can beforehand.




Re: Visual C# General STA Threads and Dialogs

mwalts

Yeah, the key to your problem is that you are using a windows service. Windows services are not normally allowed to interact with the desktop. If you try to run a windows form based application from a service, it will not show up.

There is no real good way around this... If you are in XP you can set the service to interact with the desktop if it is a local system account, but I have heard that even that is done with in Vista (could be wrong)

Good luck

-mwalts




Re: Visual C# General STA Threads and Dialogs

21 Concepts

You could use System.Diagnostics.Process.Start() to spawn a new process for the exe's. Not sure if this will fix your problem or not, but it sounds like it might...

Wes

http://www.21concepts.com





Re: Visual C# General STA Threads and Dialogs

mwalts

Still runs into the issue of it being a service... I know because I tried it when I had the same problem Stick out tongue

-mwalts




Re: Visual C# General STA Threads and Dialogs

John Wesley Harding

Well yes, the issue would still remain, although I was thinking using paths to start the application today.

The main problem is not the visibility of the application however, since I can achieve that and other piece of code among 15,000+ lines works perfectly, just when calling for this dialog is an error thrown.




Re: Visual C# General STA Threads and Dialogs

Peter Ritchie

John Wesley Harding wrote:
Well I would of thought it had its own thread, but calling the Main method of an application does not seem to load the applications interface even though the code is executed, so to get around this I create a new thread a execute the Main method of the application which works fine but like I said, when I wish to use dialogs an exception is thrown.

I know you cant change the state once started but you can beforehand.
No, simply calling a method will not create a new thread (nor will using AppDomain.ExecuteAssembly). Creating a new thread usually makes it MTA. If you use "new Thread" you have to call SetApartmentState() before Start().

But, as has been pointed out, services aren't meant to interact with the desktop. And, in fact there may be no desktop to interact with as services start at boot up, before the user logs in. Services are also given a much smaller heap that "normal" applications and as such don't normally have enough memory to load something with a more than simple UI.






Re: Visual C# General STA Threads and Dialogs

Micky D

Sounds as though the process is running in a Multi-threaded Apartment (MTA) instead of Single Threaded Apartment (STA).

From MSDN:

Process.Start()

...A note about apartment states in managed threads is necessary here. When
UseShellExecute is true on the process component's StartInfo property, make sure you have set a threading model on your application by setting the attribute [STAThread] on the main() method. Otherwise, a managed thread can be in an unknown state...





Re: Visual C# General STA Threads and Dialogs

21 Concepts

Here is a good post that explains the matter: http://tinyurl.com/2h5rng.

It seems the problem (really a solution) stems from the services and the first user logged on running in separate sessions now.

Wes

http://www.21concepts.com