Reputation: 2031
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?
Upvotes: 33
Views: 96707
Reputation: 6273
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)
Upvotes: 11
Reputation: 2033
Create a windows form application, and delete Form1
Modify program.cs Application.Run(new Form1());
to Application.Run();
Upvotes: 8
Reputation: 31
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;
Upvotes: 3
Reputation: 5015
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.
Upvotes: 26
Reputation: 43
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
Upvotes: 3
Reputation: 13
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
Upvotes: 0