Debugging runtime tips for a Windows service?

I have a windows service that controls a COM port connected to vendor equipment. This is a very busy hardware that constantly polls other devices on the wire (this is an RS485 network with twisted pair). My software needs to emulate the X-number of the hardware devices on this wire, so I have multi-threaded work with a multi-level state machine to track where the communication protocol is at any time.

The problem with the Windows Service (this is my first, BTW) is that you need some debugging so that you know if the stuff is working properly. When I first developed this status / multithreading code, I had a Windows form with RichTextBox that displayed ASCII characters returning back to the string. It seems like I can’t actually have such a schedule with the service. I tried to open the form in the service through another program that sent service messages received through the OnCustomCommand () handler, but it did not seem to work. I checked "Allow the service to interact with the desktop" and all that. I used the Show () and Hide () methods of my debug form.

I think I don’t need to see all the characters walking along the line, but a person who would be sure would be good (I think I really need to see them :-)). Anyone have any crazy ideas that could help me? I do not want to scare the system with some IPCs that are not designed for the bulk of the data that will surely pass. It will only be a very short-term debugging, though, just a confirmation that the program, RS485-USB key and hardware all work.

+3
source share
5 answers

I answer my question. I tried a couple of suggestions here, but here is what I ended up doing ...

Windows Form RichTextBox. NamedPipeServerStream . Button "" ( 128), "" (129) Windows. "debug off". , 128 ​​ Windows, . Windows , , NamedPipeClientStream BinaryWriter COM-. BackgroundWorker WaitForConnection() . , BinaryReader.ReadString() RichTextBox.

. , , . , . , - . !

0

OutputDebugString , DebugView . Windows XP , PortMon, , . , , . DebugView .

+4

, , Main, .

Edit:

:

class Worker : ServiceBase
{

#if(RELEASE)
        /// <summary>
        /// The Main Thread where the Service is Run.
        /// </summary>
        static void Main()
        {
            ServiceBase.Run(new Worker());
        }
#endif

#if(DEBUG)
        public static void Main(String[] args)
        {
            Worker worker = new Worker();
            worker.OnStart(null);
            Console.ReadLine();
            worker.OnStop();
        }
#endif

        // Other Service code
}
+2

, . "tail" .

+1

, Windows Service, , , , . , , Environment.UserInteractive. , . , , . Program.cs , ServiceBase.Run(servicesToRun)

/// <summary>Runs the provided service classes.</summary>
/// <param name="servicesToRun">The service classes to run.</param>
/// <param name="args">The command-line arguments to pass to the service classes.</param>
private static void RunServices(IEnumerable<ServiceBase> servicesToRun, IEnumerable args)
{
    var serviceBaseType = typeof(ServiceBase);
    var onStartMethod = serviceBaseType.GetMethod("OnStart", BindingFlags.Instance | BindingFlags.NonPublic);
    foreach (var service in servicesToRun)
    {
        onStartMethod.Invoke(service, new object[] { args });
        Console.WriteLine(service.ServiceName + " started.");
    }

    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();

    var onStopMethod = serviceBaseType.GetMethod("OnStop", BindingFlags.Instance | BindingFlags.NonPublic);
    foreach (var service in servicesToRun)
    {
        onStopMethod.Invoke(service, null);
        Console.WriteLine(service.ServiceName + " stopped.");
    }
}

Now you can debug your service, set breakpoints, whatever. When you start the application, you will get a console window corresponding to the display of console messages, and it will remain open until you press the key.

0
source

All Articles