Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Method not called from OnStart()

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


    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,074 ✭✭✭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