C# 有什么办法可以在后台运行进程?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1115080/
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-06 08:34:14  来源:igfitidea点击:

There is any way to run processes in the background?

c#.netnohup

提问by Jader Dias

There is any command line or .NET method that runs a process in the background hiding any window it tries to open?

是否有任何命令行或 .NET 方法在后台运行进程隐藏它试图打开的任何窗口?

Already tried:

已经尝试过:

 var process = new Process()
 {
      StartInfo = new ProcessStartInfo()
      {
          CreateNoWindow = true,
          WindowStyle = ProcessWindowStyle.Hidden,
          FileName = TheRealExecutableFileNameHere
      }
 }
 process.Start();

With no success so far

到目前为止没有成功

采纳答案by Kenan E. K.

Check out the Matlab Engine.

查看Matlab 引擎

There's even an interesting articleon CodeProject, if this approach fits your needs.

如果这种方法符合您的需要,甚至还有一篇关于 CodeProject的有趣文章

回答by Bevan

It depends on whether you want to start the application minimized, but allow it to interact with the user if required, or if you want to prohibit all access with the user regardless of what happens.

这取决于您是否希望以最小化的方式启动应用程序,但在需要时允许它与用户交互,或者您是否希望无论发生什么都禁止与用户的所有访问。

If the latter, you could run the process under a different desktop context to the current user.

如果是后者,您可以在与当前用户不同的桌面上下文下运行该进程。

Different Desktop contexts are used, for example, by the Login dialog and by Vista UAC - anything that happens in one desktop context is independent of others.

例如,登录对话框和 Vista UAC 使用不同的桌面上下文 - 在一个桌面上下文中发生的任何事情都与其他桌面上下文无关。

Might be a sledgehammer approach to your problem though.

不过,这可能是解决您问题的大锤方法。

回答by Chansik Im

I assume you want a process that is not visible to users while running.

我假设您想要一个在运行时对用户不可见的进程。

You can try the following to see whether this is what you want.

您可以尝试以下操作,看看这是否是您想要的。

  1. Create a simple console app that keeps running (and launch it to test).
  2. Right click on Project --> Properties --> Application tab --> Output type --> Change it from "Console Application" to Windows Application.
  3. Launch the same app one more time (to see whether this is what you want).
  4. Close the app via Windows Task Manager :)
  1. 创建一个保持运行的简单控制台应用程序(并启动它进行测试)。
  2. 右键单击项目 --> 属性 --> 应用程序选项卡 --> 输出类型 --> 将其从“控制台应用程序”更改为 Windows 应用程序。
  3. 再次启动同一个应用程序(看看这是否是您想要的)。
  4. 通过 Windows 任务管理器关闭应用程序 :)

It seems that the process appears in Task Manager, yet, is not visible in the task bar. Alt+TAB cannot bring this process up.

该进程似乎出现在任务管理器中,但在任务栏中不可见。Alt+TAB 无法启动此进程。

I just hope that you are not making any malicious app, though :)

不过,我只是希望您不要制作任何恶意应用程序:)

回答by Ben Griswold

I reviewed my code and it looks nearly identical to yours:

我查看了我的代码,它看起来与您的几乎相同:

ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments)
{
   CreateNoWindow = true,
   WindowStyle = ProcessWindowStyle.Hidden,
   UseShellExecute = false,
   RedirectStandardOutput = true                                               
};

Process process = Process.Start(psi);

The only notable difference (other than formatting and which PSI constructor we chose) is my use of UseShellExecute and RedirectStandardOutput as I needed to read the result of the ran process.

唯一显着的区别(除了格式化和我们选择的 PSI 构造函数之外)是我使用了 UseShellExecute 和 RedirectStandardOutput,因为我需要读取运行过程的结果。

I have found the code above consistently runs a hidden process on XP and Vista. I have also found, however, and you may be experiencing the same, that a hidden process may kick off another process which by default isn't hidden. In other words, if you start hidden Process A and Process A, in turn, kicks off Process B, you have no control as to how Process B will be displayed. Windows which you have no control over may be displayed.

我发现上面的代码一直在 XP 和 Vista 上运行一个隐藏的进程。然而,我也发现,并且您可能遇到同样的情况,隐藏的进程可能会启动另一个默认情况下不隐藏的进程。换句话说,如果您启动隐藏的进程 A 和进程 A,依次启动进程 B,则您无法控制进程 B 的显示方式。可能会显示您无法控制的窗口。

I hope this helps a little. Good luck.

希望这会有帮助。祝你好运。

回答by bohdan_trotsenko

There is noI do not know a pure .Net way to achieve this.

没有我不知道实现这一点的纯 .Net 方式。

Then I thought about kernel Job Objects, but found no similar option in UI restrictions.

然后我想到了内核作业对象,但在UI 限制中没有发现类似的选项。

So, the next (yet unverified) idea is to create than process suspended, create a windows hookthen, which will monitor CallWndProcand filter out WM_SHOW messages. (Then, surely, resume the process, wait in a separate thread till it terminates, remove the hook)

因此,下一个(尚未验证)的想法是创建而不是进程暂停,然后创建一个 windows 挂钩,它将监视CallWndProc并过滤掉 WM_SHOW 消息。(然后,当然,恢复进程,在一个单独的线程中等待直到它终止,移除钩子)

回答by CertifiedCrazy

You may want to try the BackgroundWorker Classin the .Net Framework if you haven't already. It's for executing long running processes on a separate thread to prevent them from impeding the UI. Give it a look.

如果您还没有尝试过.Net 框架中的BackgroundWorker 类,您可能想尝试一下。它用于在单独的线程上执行长时间运行的进程,以防止它们妨碍 UI。看看吧。

回答by Mark Good

Have you tried using the Microsoft DOS startcommand with the /Bswitch?

您是否尝试过使用带有/B开关的 Microsoft DOS启动命令?

Microsoft DOS start command

微软DOS启动命令

For example,

例如,

START /B cmd.exe

回答by Matt

I think what you might want to try is to create a new Windows Station. Check out this article that explains a bit about them on msdn.

我认为您可能想要尝试的是创建一个新的 Windows Station。查看这篇文章,它在 msdn 上对它们进行了一些解释。

http://msdn.microsoft.com/en-us/library/ms681928(VS.85).aspx

http://msdn.microsoft.com/en-us/library/ms681928(VS.85).aspx

回答by Abhijeet Patel

I noticed that if CreateNoWindow = false does absolutely nothing when the Filename is pointing to a Windows executable, if you have access to the source code of the winform app then you might be able to provide a command line argument which controls the default visibility of the form, and do something like this in the Winform App startup code:

我注意到,如果 CreateNoWindow = false 当 Filename 指向 Windows 可执行文件时绝对没有任何作用,如果您有权访问 winform 应用程序的源代码,那么您可能能够提供一个命令行参数来控制默认可见性窗体,并在 Winform 应用程序启动代码中执行以下操作:

static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form1 form1 = new Form1();

        form1.Load += new EventHandler((s,o) =>
            {
              //check if the form should be shown based on command line arg
                if (args.Contains("dontShowWindow"))
                {
                    //hide it
                    form1.ShowInTaskbar = false;
                    form1.Visible = form1.ShowInTaskbar = false;
                }
            }
        );
        Application.Run(form1);
    }

In you calling code, you can now specify "dontShowWindow" as a process Argument:

在您调用代码中,您现在可以将“dontShowWindow”指定为进程参数:

 ProcessStartInfo info = new ProcessStartInfo
        {
            CreateNoWindow = false, 
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false, 
            FileName = @"C:\temp\testWinForm.exe",
            Arguments = "dontShowWindow"
        };
        Process.Start(info);

Hope this helps

希望这可以帮助

回答by Keith Adler

One very simply way to achieve this is to create a Service Account and run the executable under the context of the Service Account user via the Windows Task Scheduler.

实现此目的的一种非常简单的方法是创建一个服务帐户并通过 Windows 任务计划程序在服务帐户用户的上下文中运行可执行文件。

You could use this CodeProject to setup the scheduled task:

您可以使用此 CodeProject 来设置计划任务:

http://www.codeproject.com/KB/cs/tsnewlib.aspx

http://www.codeproject.com/KB/cs/tsnewlib.aspx

You could create the service account programatically in the Domain or local machine very easily with C#.

您可以使用 C# 非常轻松地在域或本地计算机中以编程方式创建服务帐户。

http://www.codeproject.com/KB/system/OSUserMangement.aspxhttp://support.microsoft.com/kb/306273

http://www.codeproject.com/KB/system/OSUserMangement.aspx http://support.microsoft.com/kb/306273

Processes running as scheduled tasks in the context of another user do not appear interactively unless that user is logged in; hence the use of a service account.

在另一个用户的上下文中作为计划任务运行的进程不会以交互方式出现,除非该用户已登录;因此使用服务帐户。