Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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?

like image 586
Some Body Avatar asked Jun 14 '12 05:06

Some Body


People also ask

How do I make a program run in the background?

Select Start , then select Settings > Privacy > Background apps. Under Background Apps, make sure Let apps run in the background is turned On. Under Choose which apps can run in the background, turn individual apps and services settings On or Off.

What is background process in C#?

BackgroundWorker Events DoWork event is the starting point for a BackgroundWorker. This event is fired when the RunWorkerAsync method is called. In this event hander, we call our code that is being processed in the background where our application is still doing some other work. this.


2 Answers

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.

like image 65
Sergei Danielian Avatar answered Oct 02 '22 21:10

Sergei Danielian


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)

like image 26
magol Avatar answered Oct 02 '22 22:10

magol