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?
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With