C# 在 .NET 中开发一个在后台运行的程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11027051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:10:10  来源:igfitidea点击:

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

c#.net

提问by Some Body

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?

我在 C# 中做了一个小程序,我想在后台运行它,它应该只在按下某个组合键时出现。我怎样才能做到这一点?

采纳答案by gahcep

There are at least three ways to do this:

至少有三种方法可以做到这一点:

  • Classic Windows Serviceapplication. "Creating a Basic Windows Service in C#" article from CodeProject will help you. In that case you use System.ServiceProcessnamespace. 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-ResidentProgram. 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# applicationand tryto hideit from Task Manager.

  • 经典的Windows 服务应用程序。“在C#创建基本的Windows服务”,从CodeProject上的文章将帮助您。在这种情况下,您使用System.ServiceProcess命名空间。顺便说一句,在这种情况下,您应该阅读MSDN 上的“ System.ServiceProcess Namespace”文章。这是它的简短引述:

    System.ServiceProcess 命名空间提供允许您实现、安装和控制 Windows 服务应用程序的类。服务是长时间运行的可执行文件,无需用户界面即可运行。

  • 内存驻留程序。但这在 C# 中几乎是不可能的。如果需要,请为此目的使用 C++ 或更好的 C。如果您想自己搜索,只需使用关键字TSR

  • 最后一张是脏的。只需创建一个无格式的 C# 应用程序尝试将其隐藏在任务管理器中即可。

回答by magol

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

在我看来,让程序完全不可见是一个坏主意。然后用户无法与程序集成。我建议将它放在 SysTray 中(Windows 中的时钟图标)

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

    // Add menu to 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)

要监视键盘,您可以使用 LowLevel Keyboard hook(参见示例)或附加一个快捷键(参见示例

回答by ION Renalyte

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

如果你真的想创建一个真正在后台运行的程序,尝试创建一个 Windows 服务。如果你创建一个新项目,它就在那里

回答by light

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.

您可以创建 Windows 服务应用程序。它作为后台进程运行。没有用户界面。这也可以在计算机启动时自动启动。您可以在任务管理器中查看其余的后台进程,也可以在命令提示符中输入 services.msc。

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

这可能会有所帮助。http://msdn.microsoft.com/en-us/library/9k985bc9%28v=vs.80%29.aspx

回答by user1883798

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

一个快速而肮脏的解决方案(我认为 Window Service Application 模板在 Visual Studio Express 和 Standard 中不可用):

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

启动一个新的 Windows 窗体应用程序。向解决方案添加一个新类并在其中编写您想要的代码。

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:

返回到窗体设计器,将 WindowState 属性设置为 Minimized,并向窗体添加 Load 事件。在事件处理程序中隐藏表单并调用您的类:

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:

该应用程序不会出现在任务栏中,并且当您按 Alt+Tab 时也看不到它。如果需要,您可以向其添加系统托盘图标,就像 magol 写道:

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

回答by IlPADlI

Create a windows form application, and delete Form1

创建一个windows窗体应用程序,并删除Form1

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

将 program.cs 修改Application.Run(new Form1());Application.Run();