C# windows service main loop




















Hans Passant Hans Passant k gold badges silver badges bronze badges. I got a service running at my company based on the above code. It works fine. This was more or less the behavior that was requested in the question.

Mitch Wheat Mitch Wheat k 42 42 gold badges silver badges bronze badges. But if i do this, it will call the Business method once and then never run again? Since the handler method is invoked on a separate thread, it could theoretically be called multiple times before the timer is disabled. Community Bot 1 1 1 silver badge. Anders Fjeldstad Anders Fjeldstad Interrupt ; thread.

Sleep is not the ideal solution, why? Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.

Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked See more linked questions. Related Kea, that doesn't solve the problem of stopping the service while it's in a sleep state.

You have to wait until it comes out of a sleep. Aborting is not really a bad practice if you know what you're doing. Killing a sleeping thread which has no open objects is not dangerous. In the place where you do work with objects you can catch the exception and take your time to clean it all up in a nice way. So if my service round is, lets say, 2 minutes, the user will have to wait up to 2 minutes after he push the stop service button.

This is not a very bad idea but I want to handle this. I want him to wait up to 20 seconds. I can sleep for seconds each time using Span I can wake my service, check if I need to do somehting, and go back to sleep - if not. But again, What to do inside the thrad action. How can I stop it?

Aborting threads should be a last resort to terminate them. I didn't say using monitors and sleep, but using a system timer other than sleep would be a better option, there's no guarantee your thread will sleep for that long. Besides, if the called method should be extended in the future to handle more critical tasks requiring clean up, you have to rewrite that code again to make it work correctly.

Hi You can use a timer or wait on an event object in the other thread. Hi, Why don't you go for a System. You could provide the method that you want to be executed at regular intervals as the handler for the 'Elapsed' event of the timer object. You could set the 'Interval' property to The 'Elapsed' event will be raised for every expiry of the 'Interval' you specified 'AutoReset' property of the Timer object is 'true' by default.

When the user stops the service, just invoke 'Stop ' method on the timer object. Let me try explane again my problem: My service is running just fine. And avoid using new thread for the main action? Friday, February 23, PM. Hadden 0. Monday, June 29, PM.

I'd also like to find the best solution for this. We have a lot of Windows Services that need to do fairly simple tasks: Process a number of files in a folder or database records and then go to sleep for a designated number of minutes or hours, repeat forever The Thread. For example, the following statement converts the string to a long number by using the Parse method:. You can also use the Convert class method ToInt64 to do the same thing:.

For more information, see Parse and Convert. The following example shows how to use command-line arguments in a console application. The application takes one argument at run time, converts the argument to an integer, and calculates the factorial of the number.

If no arguments are supplied, the application issues a message that explains the correct usage of the program. Paste the following code into any text editor, and then save the file as a text file with the name Factorial. From the Start screen or Start menu, open a Visual Studio Developer Command Prompt window, and then navigate to the folder that contains the file that you created.

If your application has no compilation errors, an executable file that's named Factorial. When running an application in Visual Studio, you can specify command-line arguments in the Debug Page, Project Designer. For more information, see the C Language Specification.

The language specification is the definitive source for C syntax and usage. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. Any additional feedback? Note If the examples used async modifier on the Main method, the compiler would generate the same code.

Note You can also use Environment. Tip The args array can't be null. Submit and view feedback for This product This page.



0コメント

  • 1000 / 1000