CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Feb 2005
    Posts
    568

    Run console application as windows service

    is it possible i run console application as windows service or wrap an console application as windows service...
    pls provide some sample...

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Run console application as windows service

    Not as it is. You need specialized program which will run as service and host the console application. Look e.g. at FireDaemon.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Feb 2005
    Posts
    568

    Re: Run console application as windows service

    Can anyone show how can i use visual studio to create this?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run console application as windows service

    Quote Originally Posted by lsy
    Can anyone show how can i use visual studio to create this?
    Are you asking how to create a Windows Service in Visual Studio? If so, what Visual Studio version are you using?

  5. #5
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Run console application as windows service


  6. #6
    Join Date
    Feb 2005
    Posts
    568

    Re: Run console application as windows service

    sorry, i still don't know what is that abt?? do there have any simple example?

  7. #7
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: Run console application as windows service

    That is a very very basic sample. I would suggest going back to that link and reading up on it. It's very easy to use. Just copy paste it in your project and mess around with it.

  8. #8
    Join Date
    Feb 2005
    Posts
    568

    Re: Run console application as windows service

    Quote Originally Posted by mariocatch
    That is a very very basic sample. I would suggest going back to that link and reading up on it. It's very easy to use. Just copy paste it in your project and mess around with it.
    I can create using the default template in visual studio->windows service right??
    do u have any website which start from there?? it seems more easy!

  9. #9
    Join Date
    Feb 2005
    Posts
    568

    Re: Run console application as windows service

    i followed this website to create the service...
    http://avinashkt.blogspot.com/2007/0...s-service.html
    After i install the service it show success... but when i try to start the service it show...

    Error 1083: the executable program that this service is configured to run in does not implement the service

    how can i resolve this?

  10. #10
    Join Date
    May 2011
    Posts
    3

    Re: Run console application as windows service

    If your console application is really simply and you don't want to trouble of recoding it as a service you can try using a command line application I wrote which can be found here: http://www.RunAsService.com it's a single executable no setup and a single command will have you up and running.

  11. #11
    Join Date
    Apr 2011
    Posts
    23

    Re: Run console application as windows service

    interesting program good work luisperezphd

    I could see many good uses for such a program.

    I would however like to say there has got to be a way to do this safely and effectively for any program that you make I find that this is a rather interesting question and I hope it gets answered in the manner which it was presented. After all of course it is very possible to run any program as a service with fire deamon or something like this ....

    Of course it will work but there is really nothing to be learned from it it is simply using another persons work to accomplish a task so I can see why this may be an issue for someone who is trying to learn how to accomplish something by example. There has been a number of times I too have wanted to run an application as a service and have the service start when windows does.

    It used to annoy me that my virus scan along with 10 other programs were running on the task bar but with a bigger screen and higher resolutions and grouped instances of a program introduced in windows xp then tabbed browsing introduced first to me by firefox (while it could have been opera that first had tabbed browsing IDK) This all helped keep less clutter and made stuff a bit easier to find and with the previews of whats running and first the logitech application switcher then the application switcher used by windows 7 10 programs running don't seem like too much clutter anymore not to mention the ability to hide unused icons or always hide specific icons. Windows has came a long way in increasing productivity which is why I have purchased so many copies of windows 7 to support its grand design.

    Even still I could see the need for wanting to know how to run a program as a service I am guessing it would have to be done through an installer with the proper rights as an administrator for security reasons. While I may be wrong it is what makes logical since to me.

  12. #12
    Join Date
    May 2011
    Posts
    3

    Re: Run console application as windows service

    Hey infriger, you make a good point, my original post suggesting using runasservice is a valid solution if we assume the problem was that the original poster just wanted a quick a dirty way to run as console application as a service.

    It doesn't help though if they were trying to learn how to write a service, or convert their application into a service.

    I originally created the tool because I think the process of installing a service is not complicated, but more complicated and inflexible than it needs to be. I like that in .NET the applications use xcopy deploy, meaning you can just copy the files and run them. Services don't work that way, they require changes to the registry.

    Even worse the default implementation of .NET services in Visual Studio require you to embed the service information like the name of the service into your application, but then require you to use an external application to install it (installutil I think).

    Also to debug your service using Visual Studio you have to attach to it.

    None of these are big hassles, but just needless, and they add up. Using the tool I created you can just write a console application, which is pretty straight forward, then install it with the ease you would use xcopy.

    That said, writing a service isn't very different from any other application. In a console application the Main method gets called when the application is executed, the main method also gets called first on a service, BUT, that is not where you want to put your loop or bulk of your code.

    Instead you want to create a separate class that extends from ServiceBase (I think), and override OnStart(). In your Main method you would call:

    System.ServiceProcess.ServiceBase.Run(new MyService());

    That would be your simplest conversion. A more advance conversion would take advantage of features available to services and redesign the application to take advantage of them. For example the pausing functionality.

    There are also other considerations for example the fact that the same resources might not be available to the service that would have otherwise been available to the regular console application. For example access to the files on the desktop. Also there's the fact that the service might not be logged in as the current user.

    More than likely you will uncover and resolve these issues in the process of trying to convert it. Note you would come across a lot of these same issues using runasservice.

    Here's a link to Microsoft's documentation on creating a service: http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx

  13. #13
    Join Date
    Dec 2010
    Posts
    2

    Re: Run console application as windows service

    Hi. While changing your program to explicitly support the Windows Services API is certainly an option, simply launching it from a wrapper may be a better choice in many circumstances (when you don't have control over the code, don't want to deal with the complexities of the API, etc.).

    The free XYNService at CodeProject (http://www.codeproject.com/KB/system/xyntservice.aspx) is a fine choice to run a console application as a service. Check out AlwaysUp (http://www.coretechnologies.com/products/AlwaysUp/) if you can afford to go with a more fully featured, commercial solution.

    Good luck!

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Run console application as windows service

    In my opinion, it's just too simple to learn how to write a proper windows service C# than to hack with some sort of service wrapper.

    At the end of the day, the wrappers usually fall short of providing the benefits of a windows service.

    Either you need those benefits or you don't. If you need them, then learn how to write a C# windows service.

    It's pretty simple.

  15. #15
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Run console application as windows service

    IMO its a pretty useful too though. you don't always have the ability to alter a command line app (say, like some 3rd party utility) to be a windows service, and in instances like that, or where its not cost effective I think a service wrapper app like this is a great idea. similar btw to daemontools in linux which is like a windows services in windows but lets you run command line apps (which windows does not).
    Last edited by MadHatter; May 10th, 2011 at 08:19 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured