Develop a program that runs in the background in .NET?

Create a windows form application, and delete Form1

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


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.


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)

Tags:

C#

.Net