33

I have made a small program in C# that I want to run in the background and it should only appear when a certain key combination is pressed. How can I do this?

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Some Body
  • 1,881
  • 4
  • 19
  • 16

6 Answers6

27

There are at least three ways to do this:

  • Classic Windows Service application. "Creating a Basic Windows Service in C#" article from CodeProject will help you. In that case you use System.ServiceProcess namespace. BTW, in that case you should read "System.ServiceProcess Namespace" article from MSDN. Here is a short quote from it:

    The System.ServiceProcess namespace provides classes that allow you to implement, install, and control Windows service applications. Services are long-running executables that run without a user interface.

  • Memory-Resident Program. But this is almost impossible to do with C#. Use C++ or better C for this purpose, if you want. If you want to search by yourself, just use keyword TSR.

  • Last one is a dirty one. Just create a formless C# application and try to hide it from Task Manager.

Community
  • 1
  • 1
Sergei Danielian
  • 4,938
  • 4
  • 36
  • 58
  • one question. can i add a GUI to windows service ? – Some Body Jun 14 '12 at 05:44
  • 2
    No, but you can create a separate GUI application to interconnect with a service – Sergei Danielian Jun 14 '12 at 05:45
  • any guideline on how can i do that ? – Some Body Jun 14 '12 at 05:47
  • For this case you should use an `IPC Mechanisms`. Start with a question [here](http://stackoverflow.com/questions/56121/ipc-mechanisms-in-c-sharp-usage-and-best-practices) or [here](http://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro), at SO. – Sergei Danielian Jun 14 '12 at 05:50
  • The word "Formless application" did it for me. In case if anyone is wondering: that works kinda need. Since you can easily spawn "monitor forms" if you need any :) In my case: to monitor my logging engine. – Jelmer May 23 '14 at 13:32
11

To allow the program to be completely invisible is, in my opinion, a bad idea. Because the user cannot interact with the program. I would recommend placing it in the SysTray (an icon by the clock in Windows)

    trayIcon      = new NotifyIcon();
    trayIcon.Text = "My application";
    trayIcon.Icon = TheIcon

    // Add menu to the tray icon and show it.
    trayIcon.ContextMenu = trayMenu;
    trayIcon.Visible     = true;

    Visible       = false; // Hide form window.
    ShowInTaskbar = false; // Remove from taskbar.

To monitor keyboard you can use LowLevel Keyboard hook ( see example ) or attach a hootkey (See example)

magol
  • 6,135
  • 17
  • 65
  • 120
  • How about Sticky Keys? Or the Magnifier tool? There are some interesting possibilities.. (granted these don't actually "run in the background" AFAIK) –  Jun 14 '12 at 05:22
  • Well its not a commercial progaram something i am just trying to learn. The progarm would have a GUI first time it runs, afterwards it would run in the background and would only become visible once a certain key combination is pressed. – Some Body Jun 14 '12 at 05:33
8

Create a windows form application, and delete Form1

Modify program.cs Application.Run(new Form1()); to Application.Run();

IlPADlI
  • 1,943
  • 18
  • 22
3

You can create a Windows Service Application. It runs as a background process. No user interface. This can also start automatically when the computer boots. You can see the rest of the background processes in Task Manager or you can type in services.msc in Command Prompt.

This might help. http://msdn.microsoft.com/en-us/library/9k985bc9%28v=vs.80%29.aspx

light
  • 33
  • 7
3

A quick and dirty solution (I think the Window Service Application template is unavailable in Visual Studio Express and Standard):

Start a new Windows Forms Application. Add a new class to the solution and write the code you want inside it.

Go back to the form designer, set the WindowState property to Minimized, and add a Load event to the form. In the event handler hide the form and call your class:

private void Form1_Load(object sender, EventArgs e)
{
    this.Hide(); 
    MyNewClass mynewclass=new MyNewClass();
} 

The application doesn't appear in the taskbar and you don't see it when you hit Alt+Tab. You can add a systray icon to it if you want, just like magol wrote:

NotifyIcon trayIcon = new NotifyIcon();
trayIcon.Icon=new Icon(@"C:\iconfilename.ico");
trayIcon.Visible = true;
0

If you really want to create a program that really run in background, try to create a Windows service. Its there if when you create a new project

  • 1
    ok i have looked done a quick web search on windows service and wikipedia says that a windows service doesn't require user intervention. Maybe i could clearly chalk out my requirement. The program should have a GUI the first time it runs. Afterwards it will only appear when a certain key combination is pressed. Is it possible to make a GUI with Windows service ? – Some Body Jun 14 '12 at 05:26