 |
|
|
Using this Method of debugging a service is there a way from inside the debugger to call the OnStop() Method? I have used your method for debugging a service but am having issues in my Shutdown code and getting the debugger in there to help would be great.
Thanks,
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
It depends on what your bug is.
If it's just a problem within the OnStop then change the call to sleep forever below with a call to your OnStop.
// Put a breakpoint on the following line to always catch // your service when it has finished its work System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
However if it's the actual calling of OnStop that seems to be the problem, then I'd suggest reading through some of the earlier comments as there are more than a few people who've taken this idea and gone even further with it.
I just love Koalas - they go great with Bacon.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
My problem is in the code that OnStop() executes (my code to shutdown the service). Since in debug it is not really a service (but an application per this article) I have no way to call OnStop() while in debug. Does that make my question clearer?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
.. dont ever call SetServiceStatus() or RequestAdditionalTime() if not running under the service process or you earn exceptions.
To avoid them, create new overloads of them in your ServiceBase derived class like:
protected new void RequestAdditionalTime(Int32 nTime) { if(!Environment.UserInteractive) base.RequestAdditionalTime(nTime); }
Leon[^] - Enterprise Anti-Spam Server
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Kick ass sample. love it absolutely. Its really helped me catch some nasty bug.
void izmoto(char* szKwazi);
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
I came up with what I think is a cleaner way to do this, perhaps you will agree:
Instead of using the #if compiler conditional to test for debug mode, which is perfect except for (as you mentioned) having to switch between Debug/Release mode all the time, you can use a command-line argument. Set the command line argument to "debugmode" or something simliar on the Debug tab in your project properties, then test for the existence of this argument in Main. Now when you run the project from Visual Studio, the command line arg will be set and you'll always run the non-service code; outside of Visual Studio, your app will run only as a Windows service.
// The main entry point for the process static void Main(string[] args) {
if(args.Length > 0 && args[0] == "debugmode") {
// Debug code: this allows the process to run as a non-service. // It will kick off the service start point, but never kill it. // Shut down the debugger to exit Service1 service = new Service1(); service.(); // Put a breakpoint on the following line to always catch // your service when it has finished its work System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
} else {
System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Yep that would work - the only issue with it is the extra step to add the commandline argument (trivial) and of course the chance that you may release debug code as a service. With my one it really didn't behave well if you tried to run the debug version as a service - so you knew when you had screwed up and put out a debug build.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
If you're simply cutting and pasting from the article - then you won't get anywhere. You need to follow the instructions in the article and create a new C# Windows Service project in Visual Studio, once you've done that you'll be left with a service class called Service1. You would normally change this name to something more suitable and put in your current Service code.
The Service1 code itself would probably look like:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text;
namespace WindowsService1 { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); }
protected override void OnStart(string[] args) { // TODO: Add code here to start your service. }
protected override void OnStop() { // TODO: Add code here to perform any tear-down necessary to stop your service. } } }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Well I have service that is running I am trying make changes to it and need to do debug. The item in the service is OnStart. Which does not work. Error message = No overload for method 'OnStart' takes '0' arguments
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
In which case I'd still recommend building a new service, making the changes I described and then transferring over that 'model' to your existing service. After that read through the comments below which discuss some options related to OnStart.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
Hello...
I have a Windows Service that in OnStart method set 2 timers that tick when determined time has elapsed.
I can debug OnStart method using your technique, but the timers never get called. Any way to do it?
Thanks
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi Nicholas,
The two simplest options are: Stick the initiation of the two timers into a separate method called by both the OnStart and the Debug code. Directly call the OnStart method from the Debug code.
Neither is rocket science but then neither is this technique. I would doubt that there would be any other issues, for instance with the event handlers for your timers, it should just go.
Regards, Lee
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I've had this issue when I used the wrong Timer before. Never have figured this out, but it seems to me if you are using a System.Windows.Forms.Timer vs. a System.Timers.Timer you get different functionality, especially in a service.
Try out the opposite timer (probably you'll want to stick with System.Timers.Timer).
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
Often the simplest things are the most amazing...I'm obviously easily impressed.
Suggestions offered by jriesen below were also very useful.
Thanks very much
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |