none
[Help] Create a service that runs a WinForm RRS feed

  • Question

  • Hello friends, I need to create a service that runs my user interface. The files are located within the same folder of the application.

    I would also like my application (form) to detect if the service is working or not ... and in case it is not working, start it.

    I do not understand much, and although they send me to other links I'm going to lose ...

    That's why I ask for your help, I would thank you enough.

    • Edited by Symantecc Monday, August 6, 2018 3:59 PM
    Monday, August 6, 2018 3:58 PM

Answers

  • When the service is created and configured to interact with the desktop, 

    Don't do that.

    That is a legacy feature, left over from days when services were not as secure.  It will not work as expected (as you have seen).


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Symantecc Friday, August 17, 2018 7:41 AM
    Tuesday, August 7, 2018 11:20 AM
    Moderator

All replies

  • Hello,

    The following is a base example, the ServiceControllerStatus possibilities.

    Requires a reference in your project for System.ServiceProcess.

    Sample code

    Dim yourServiceName As String = "TODO"
    Dim sc = ServiceController.GetServices().FirstOrDefault(Function(service) service.ServiceName = yourServiceName)
    If sc IsNot Nothing Then
        If sc.Status = ServiceControllerStatus.Stopped Then
            Try
                sc.Start()
            Catch ioe As InvalidOperationException
                ' decide how to handle specific error
            Catch ex As Exception
                ' decide how to handle unknown error
            End Try
        End If
    End If
    Note, the code was taken from a C# MSDN code sample I wrote.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 6, 2018 4:33 PM
    Moderator
  • I didn't mention running a user interface from the service.

    Windows Services cannot start additional applications because they are not running in the context of any particular user. Unlike regular Windows applications, services are now run in an isolated session and are prohibited from interacting with a user or the desktop. This leaves no place for the application to be run.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 6, 2018 5:12 PM
    Moderator
  • As Karen notes, the proper workflow is for the user to start the desktop application and then connect to the service to control it or interface with its data.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, August 6, 2018 5:19 PM
    Moderator
  • Hi Could you pass me the source code shown to download and open ..? I do not know how to start the project in my Visual Studio


    So, is there no way to run my application through a service?
    • Edited by Symantecc Monday, August 6, 2018 5:20 PM
    Monday, August 6, 2018 5:20 PM
  • Hi Could you pass me the source code shown to download and open ..? I do not know how to start the project in my Visual Studio


    So, is there no way to run my application through a service?

    As I indicated along with Reed, you connect to the service from the desktop not the other way around.

    The source code in the link I provided is in C#. I simply went to the link, browsed code, viewed it and then wrote the VB.NET code from the C# code. I have no plans to write a VB.NET version.

    The intent was to show how to run a unattended service which fires off an event where in the event it calls a service in a class project which in turn writes to a SQL-Server database table. Kept it simple.

    The full intent was to show how to start, stop a service along with how to debug a service thru Visual Studio. The utility expects the service to be in the same Visual Studio solution that has projects it interacts with.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, August 6, 2018 5:30 PM
    Moderator
  • Here's a walkthrough that might help get you started:

    https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, August 6, 2018 6:10 PM
    Moderator
  • Hello friends, my problem is as follows. When the service is created and configured to interact with the desktop, the application runs but ... it does not show any form, the process stays in the background. Likewise, the service initialization configuration would be for when the user logs in.
    Tuesday, August 7, 2018 6:49 AM
  • When the service is created and configured to interact with the desktop, 

    Don't do that.

    That is a legacy feature, left over from days when services were not as secure.  It will not work as expected (as you have seen).


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by Symantecc Friday, August 17, 2018 7:41 AM
    Tuesday, August 7, 2018 11:20 AM
    Moderator