Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 101345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:45:11+00:00 2026-05-11T00:45:11+00:00

I have a windows service written in C# that acts as a proxy for

  • 0

I have a windows service written in C# that acts as a proxy for a bunch of network devices to the back end database. For testing and also to add a simulation layer to test the back end I would like to have a GUI for the test operator to be able run the simulation. Also for a striped down version to send out as a demo. The GUI and service do not have to run at the same time. What is the best way to achieve this duel operation?

Edit: Here is my solution combing stuff from this question , Am I Running as a Service and Install a .NET windows service without InstallUtil.exe using this excellent code by Marc Gravell

It uses the following line to test if to run the gui or run as service.

 if (arg_gui || Environment.UserInteractive || Debugger.IsAttached) 

Here is the code.

 using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.ComponentModel; using System.ServiceProcess; using System.Configuration.Install; using System.Diagnostics;  namespace Form_Service {    static class Program    {       ///        /// The main entry point for the application.       ///        [STAThread]       static int Main(string[] args)       {          bool arg_install =  false;          bool arg_uninstall = false;          bool arg_gui = false;          bool rethrow = false;          try          {             foreach (string arg in args)             {                switch (arg)                {                   case '-i':                   case '-install':                      arg_install = true; break;                   case '-u':                   case '-uninstall':                      arg_uninstall = true; break;                   case '-g':                   case '-gui':                      arg_gui = true; break;                   default:                      Console.Error.WriteLine('Argument not expected: ' + arg);                      break;                }             }             if (arg_uninstall)             {                Install(true, args);             }             if (arg_install)             {                Install(false, args);             }             if (!(arg_install || arg_uninstall))             {                if (arg_gui || Environment.UserInteractive || Debugger.IsAttached)                {                   Application.EnableVisualStyles();                   Application.SetCompatibleTextRenderingDefault(false);                   Application.Run(new Form1());                }                else                {                   rethrow = true; // so that windows sees error...                    ServiceBase[] services = { new Service1() };                   ServiceBase.Run(services);                   rethrow = false;                }             }             return 0;          }          catch (Exception ex)          {             if (rethrow) throw;             Console.Error.WriteLine(ex.Message);             return -1;          }       }        static void Install(bool undo, string[] args)       {          try          {             Console.WriteLine(undo ? 'uninstalling' : 'installing');             using (AssemblyInstaller inst = new AssemblyInstaller(typeof(Program).Assembly, args))             {                IDictionary state = new Hashtable();                inst.UseNewContext = true;                try                {                   if (undo)                   {                      inst.Uninstall(state);                   }                   else                   {                      inst.Install(state);                      inst.Commit(state);                   }                }                catch                {                   try                   {                      inst.Rollback(state);                   }                   catch { }                   throw;                }             }          }          catch (Exception ex)          {             Console.Error.WriteLine(ex.Message);          }       }    }     [RunInstaller(true)]    public sealed class MyServiceInstallerProcess : ServiceProcessInstaller    {       public MyServiceInstallerProcess()       {          this.Account = ServiceAccount.NetworkService;       }    }     [RunInstaller(true)]    public sealed class MyServiceInstaller : ServiceInstaller    {       public MyServiceInstaller()       {          this.Description = 'My Service';          this.DisplayName = 'My Service';          this.ServiceName = 'My Service';          this.StartType = System.ServiceProcess.ServiceStartMode.Manual;       }    }  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:45:12+00:00Added an answer on May 11, 2026 at 12:45 am

    You basically have two choices. Either expose an API on the service which you can then call from the UI app OR enable the service to run either as a winforms app or a service.

    The first option is pretty easy – use remoting or WCF to expose the API.

    The second option can be achieved by moving the ‘guts’ of your app into a separate class then create a service wrapper and a win-forms wrapper that both call into your ‘guts’ class.

    static void Main(string[] args) {     Guts guts = new Guts();      if (runWinForms)     {         System.Windows.Forms.Application.EnableVisualStyles();         System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);          FormWrapper fw = new FormWrapper(guts);          System.Windows.Forms.Application.Run(fw);     }     else     {         ServiceBase[] ServicesToRun;         ServicesToRun = new ServiceBase[] { new ServiceWrapper(guts) };         ServiceBase.Run(ServicesToRun);     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 77k
  • Answers 77k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer This link may help. I can't speak from experience with… May 11, 2026 at 3:23 pm
  • added an answer Use re-search-forward and match-string: (when (re-search-forward '\\(\\S-\\)' nil t) (match-string… May 11, 2026 at 3:22 pm
  • added an answer Here is the manual for FMS and configuring storage. http://livedocs.adobe.com/flashmediaserver/3.0/docs/help.html?content=03_configtasks_32.html#1204236 May 11, 2026 at 3:22 pm

Related Questions

I have a large xml document that needs to be processed 100 records at
We have a Windows Service written in C#. The service spawns a thread that
I am working on a new licensing system for our companies software. We are
I just had a laptop crash (spilled water on it). I copied my working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.