Entwickler-Ecke

WinForms - WindowsFormsApplication + Service in einer *.exe?


Necaremus - Mi 28.07.10 16:28
Titel: WindowsFormsApplication + Service in einer *.exe?
huhu

ich wollte fragen, ob es möglich ist eine WindowsForm und ein service in ein und die selbe .exe zu packen?
also, dass wenn man die exe normal aufruft, er als Form startet, wenn man die aber übern service-manager startet, er den service startet.

Und falls es möglich ist: wie? :D


Danke schonmal im vorraus.

mfg


danielf - Mi 28.07.10 16:37

Hallo,

du kannst in der Main-Methode einen Argument auswerten und dementsprechend als Window-Service oder als Prozess (Konsole/WinForms) starten.

Zum Beispiel:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
   public class NorthernLightInformerWindowsService : ServiceBase
    {
        private ServiceHost host;

        public static void Main(string[] args)
        {
            if (args.Length > 0 && args[0].ToLower() == "debug")
            {
                Console.WriteLine("Starte Server...");

                NorthernLightsInformerService.StartService();

                Console.WriteLine("Server läuft, bis Sie 'Return' drücken!");
                Console.ReadLine();
                NorthernLightsInformerService.StopService();
            }
            else
            {
                System.ServiceProcess.ServiceBase.Run(
                    new ServiceBase[] { new NorthernLightInformerWindowsService() });
            }
        }

        protected override void OnStart(string[] args)
        {
            host = new ServiceHost(typeof(NorthernLightsInformerService));
            host.Open();
        }

        protected override void OnStop()
        {
            host.Close();
        }
    }


Gruß