How do I debug Windows services in Visual Studio?

后端 未结 17 781
梦毁少年i
梦毁少年i 2020-11-28 03:56

Is it possible to debug the Windows services in Visual Studio?

I used code like

System.Diagnostics.Debugger.Break();

but it is givi

17条回答
  •  猫巷女王i
    2020-11-28 04:04

    Either that as suggested by Lasse V. Karlsen, or set up a loop in your service that will wait for a debugger to attach. The simplest is

    while (!Debugger.IsAttached)
    {
        Thread.Sleep(1000);
    }
    
    ... continue with code
    

    That way you can start the service and inside Visual Studio you choose "Attach to Process..." and attach to your service which then will resume normal exution.

提交回复
热议问题