Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Method not called from OnStart()

  • 15-02-2011 10:11PM
    #1
    Registered Users, Registered Users 2 Posts: 4,326 ✭✭✭


    I have created a Windows Service in C# that I want to execute a method when it is started. Normally this is quite easy; put the code into the OnStart() method of the Service and make sure it's properties are set to "Logon as Local System Account" and "Allow service to interact with Desktop" checkbox is ticked.
    I have done this but the code I have under OnStart will not execute.
    It doesn't give me any errors (I've tried catching exceptions).
    Basically I want it to fetch a global variable and use it somewhere else:
    protected override void OnStart(string[] args)
            {
    
                System.Windows.Forms.MessageBox.Show("Important data is ");
                System.Windows.Forms.MessageBox.Show(MyProject.GlobalVars.ImportantData3.ToString());
    

    When I do the above it just wont start the service. MyProject is the namespace,GlobalVars is the class and ImportantData3 is the variable.
    I am only displaying the variable in a message box to see if it is getting it back as I want to use the variable later on.


Comments

  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    I've never tried it myself, but from what I gather, the MessageBox there won't work in versions of Windows from Vista upwards - they removed the ability for services to interact with the user pretty much completely (obviously you can still do IPC stuff to talk to other processes that are user interactive). I'd suggest a regular debugger or log file approach instead.


  • Registered Users, Registered Users 2 Posts: 4,326 ✭✭✭lukin


    I've never tried it myself, but from what I gather, the MessageBox there won't work in versions of Windows from Vista upwards - they removed the ability for services to interact with the user pretty much completely (obviously you can still do IPC stuff to talk to other processes that are user interactive). I'd suggest a regular debugger or log file approach instead.

    I don't think that's right; I was able to get a message box to display, it's the data I can't get back. I'll try the service on an XP machine to be sure though.
    I don't know how to debug a service from Visual Studio so I can't see what's actually happenining until I have an .exe of it and install it. I'll have a look at how to do that.


Advertisement