我有一个带计时器的windows服务。它很难调试。因为我启动了服务,并在代码的不同部分设置了断点。当我附加进程时,我希望服务从头开始,而不是在中间代码中有断点的某个随机位置。很难像一个普通的应用程序那样进行调试,因为你知道起点。看起来在后台有一些进程尚未完成。所以每次我都会开始调试,而不是从第一个断点开始,而是从应用程序中间的某个随机断点开始。
我想知道windows服务是如何在进程、线程等方面工作的……我如何从头开始调试呢?
发布于 2009-04-17 16:51:12
当然,我假设您说的是.Net。我总是使用以下代码来调试我的服务。我把它放在我想让调试器启动的地方。启动该服务,它会自动启动Visual Studio。对我来说效果很好。
System.Diagnostics.Debugger.Launch();
System.Diagnostics.Debugger.Debug();
发布于 2009-04-17 20:47:15
只需按F5即可。您可以像运行应用程序一样定期运行Windows服务。
因为我们没有其他的命令行参数,所以我们只是使用任何命令行参数的存在作为一个信号来作为一个普通的windows应用程序运行。您还可以要求存在特定的命令行参数(即/debug)。
If sArgs IsNot Nothing AndAlso sArgs.Length > 0 Then
' If there are command-line args then run in non-service mode
Dim svc As ServiceMain = New ServiceMain(True)
svc.OnStart(Nothing)
System.Windows.Forms.Application.Run()
svc.OnStop()
Else
' If no command-line args then run normally
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New ServiceMain(False)}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End If发布于 2009-04-25 17:35:39
您可以使用"Image File Execution Options",并在每次服务启动时将其配置为在调试器中启动。
该调试器可以是WinDBG或Visual Studio。
https://stackoverflow.com/questions/761120
复制相似问题