Windows Service doesnt receive callback events RRS feed

  • Question

  • I have a Win32 Dll and imported it into my .Net C# Application. This Dll calls some callback functions defined in my app. It works well, but I had to rewrite my app to Windows Service. Now I dont receive the events from Dll. I observed that in service, callback functions run in a separated thread and in app, the main process and callbacks run in a same thread.

    Does anybody has any idea about this?

    Tuesday, April 1, 2008 6:53 PM

All replies

  • Hi Carl,

    You can call the callback functions with the use of delegates. Here is the sample coding which uses delegates for CallBackFunctions.

    Code Snippet

    public delegate void CallBackListener;

    class Sample


    public void CallBackFunction()

    CallBackListener listener = new CallBackListener();


    // Normal Code for the function



    Tuesday, April 1, 2008 8:22 PM
  • Hi.

    I´m using delegate. I believe that InvokeRequire is only for Controls on form app...
    The problem is that in a form app I received an event in a callback and, when I run this app like a windows service I dont receive an event.
    Wednesday, April 2, 2008 11:52 AM
  • You need to post more information regarding the win32 interface that is

    publishing these events.


    Off the top of my head, its possible that your event publisher requires your application to have a message loop (i.e. a window handle) - which is not available by default to Windows Services.

    If so, you will need to tick the box "Allow service to interact with desktop" in the service configuration.

    (under computer management/services, open the properties dialog for your service, its under the "Log On" tab)


     - Adam

    Thursday, April 3, 2008 4:38 AM
  • In any forums on web I´ve found that my callbacks must be located in my main thread.
    But I have no success. My source seems as follow below:

    public partial class Service1 : ServiceBase
        // import dll functions
        [DllImport("myDll.dll", EntryPoint = "_connect")]
        static extern bool _connect(IntPtr OnStateChange);
        // define delegates
        public delegate void cb_OnStateChange(int newState);
        // declare callback assign       
        private static cb_OnStateChange OnChangeState = new cb_OnStateChange(StateChange);

        public Service1()

        static void Main()

        protected override void OnStart(string[] args)

        // ... OnStop, OnPause
        private static void StateChange(int newState)
            log(skcstdStateChange - newState_: " + newState.ToString());

    The problem:
    - When I run on Form App, StateChange is called 2 times (first newState=1; second newState=2 [connecting, connected])
    - When I run on Service App, StateChange is called 1 time (newState=1)

    I cant see the use of Invoke/InvokeRequired.  

    I have tried to use callback in a separated class, public, private, static, on a thread, and nothing Sad.

    Thursday, April 3, 2008 6:44 PM

  • Friday, April 4, 2008 12:33 PM
  • Did you find out the reason for the error ?
    Monday, November 12, 2018 7:40 AM