Windows services with window forms in one process

I have a C # application that runs as a Windows service managing socket connections and other things. In addition, there is another application of Windows forms for managing and configuring this service (systray with starting, stopping, displaying a form with configuration parameters).

I use .net remoting for IPC, and that was fine, but now I want to show some real traffic and other reports, and remote execution will not meet my performance requirements. Therefore, I want to combine both applications in one.

Here is the problem:

When I ran the form from windows service, nothing happened. Going to the search, I found that I need to right-click the service, go to "Login" and check the box "Allow the service to interact with the desktop." Since I do not want to ask my users to do this, I again received code to run it again to set this parameter in user regedit during installation. The problem is that even setting this option does not work. I need to open the login parameters (it is verified), uncheck the box and check again.

So how to solve this? What is the best way to have a windows service with a systray control in the same process available for any user login?

UPDATE: Thanks for the comments guys. I agree that it is better to use IPC, and I know that mixing Windows services and user interfaces is bad. Although I want to know how to do this.

+6
c # windows windows-services
source share
8 answers

Two separate processes that interact using your chosen technology. UI services are a bad idea . Do not follow this path - you will regret it.

I had very good results, having service communication through a simple socket connection - document your service protocol well, keep it as simple as possible, and it will be easier than you think.

+18
source share

In practice, you should not associate your service with the management user interface.

+1
source share

I agree with Greg. Perhaps you could explore another IPC mechanism. Perhaps use sockets and your own protocol. Or, if your service management application can only manage the service on the local computer, you can use named pipes (even faster).

+1
source share

Here's a way that mixes Services and forms

http://www.codeproject.com/KB/system/SystemTrayIconInSvc.aspx

+1
source share

I figured out how to do this from in this article (click the "Edit" link in the "Methods" table).

string wmiPath = "Win32_Service.Name='" + SERVICE_NAME + "'"; using (ManagementObject service = new ManagementObject(wmiPath)) { object[] parameters = new object[11]; parameters[5] = true; // Enable desktop interaction service.InvokeMethod("Change", parameters); } 
+1
source share

I have a solution in a few steps, this is a plan

  • we are not going to create a service project with aa windows form, instead we are going to create a visual studio solution containing a Windows service project, a Windows form project, and an installation project.

  • The idea is to have a database or file, or whatever is convenient for you, when storing data in which you save the settings that your Windows services will always use. Therefore, your Windows service and windowing application should be able to modify and retrieve data from it.

  • In the main form of your Windows application, drag NotifyIcon into the form on the properties tab, browse and select the .ico image (you can create it in the visual studio, but this is another topic that you can go to Google or contact me). This will be displayed in the system tray when the application starts, and the main form is active or shown, try, run the application.

  • Add both of them as outputs to the solution installation project. To add a project to the installation project, they must be in one solution. Right-click the installation project in the solution explorer, highlight the add, and then select the project output, add the Windows service and window form outputs, and you will see them in the solution explorer as part of the installation project.

  • adding a windows service goes further than that, but that's another google it topic

  • Creating a shortcut for a Windows application and adding it to the startup folder is another google theme or contact me.

    NOTE. Program your form so that the close button does not appear, and the form is passed to Me.visible = false, and double-clicking on the taskbar icon is the only way to set me.visible = true.that at any time when the computer starts, the form application Windows also starts and the visible is immediately set to false, but since it has an icon with an icon, it will appear on the taskbar and double-click it so that the form is visible for editing the settings that you save for the service, the service also starts It is automatically, as you would install it when configuring the service in the installation project. my mail iamjavademon@gmail.com for a better illustration using screenshots. And explain in full

+1
source share

It is very simple - you need to create one thread to execute application events. Like this (source code for C ++ with CLR, but you can do it in C #):

 ref class RunWindow{ public: static void MakeWindow(Object^ data) { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); Application::Run(gcnew TMainForm()); }; }; 

And create a thread in the main

 int main(array<System::String ^> ^args) { bool bService = RunAsService(L"SimpleServiceWithIconInTrayAndWindow"); if (bService) { System::Threading::Thread ^thread = gcnew System::Threading::Thread(gcnew ParameterizedThreadStart(RunWindow::MakeWindow)); thread->Start(); ServiceBase::Run(gcnew simpleWinService()); Application::Exit(); } else { Application::EnableVisualStyles(); Application::SetCompatibleTextRenderingDefault(false); // Create the main window and run it Application::Run(gcnew TMainForm()); } return 0; } 
0
source share

The main problems with interactive services:

  • Security - another process can send messages through its message pump, thereby gaining access to the SYSTEM / LOCAL process.

  • Incomplete - the online service never sees shell messages, so it cannot interact with the notification area icons.

We regularly use TCP and UDP connections to transfer information from services to other exes and, in some cases, MSMQ.

0
source share

All Articles