从C#关闭最小化/图标化的过程 [英] Closing a minimized/iconized process from C#

查看:155
本文介绍了从C#关闭最小化/图标化的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:我需要从C#程序中关闭一个已经运行的进程. 问题在于该过程现在作为图标运行(最小化到任务栏),除非用户至少打开一次(在无人看管的计算机上永远不会发生),否则它将永远不会 有一个主窗口.

Here's my issue: I need to close a process, already running, from a C# program. The problem is that the process now runs as an icon (minimized to taskbar), and unless the user opens it at least once (which will never happen on unattended machines), it'll never have a main window.

我的另一个要求是,该应用程序必须关闭,而不是被杀死.我需要它将其内存缓冲区写入磁盘-杀死它会导致数据丢失.

The other requirement that I have is that the application be closed not killed. I need it to write it's memory buffers to disk - and killing it causes data loss.

这是我到目前为止尝试过的:

Here's what I tried so far:

        foreach (Process proc in Process.GetProcesses())
        {
            if (proc.ProcessName.ToLower().StartsWith("myapp"))
            {
                if (proc.MainWindowHandle.ToInt32() != 0)
                {
                    proc.CloseMainWindow();
                    proc.Close();
                    //proc.Kill();  <--- not good!
                }
            }
        }

在最小化窗口时发现 MainWindowHandle == 0 后,我添加了 if 子句.删除如果没有帮助. CloseMainWindow() Close()均不起作用. Kill()可以,但是如上所述-这不是我所需要的.

I've added the if clause, after discovering that MainWindowHandle == 0 when the window was minimized. Removing the if doesn't help. Neither the CloseMainWindow() nor the Close() work. The Kill() does, but as mentioned above - it's not what I need.

任何想法都可以接受,包括使用奥术Win32 API函数:)

Any idea would be accepted, including the use of arcane Win32 API functions :)

推荐答案

以下是一些答案和说明:

Here are some answers and clarifications:

rpetrich : 之前尝试过您的方法,问题是,我不知道窗口名称,它因用户而异,因版本而异-仅exe名称保持不变.我所拥有的只是进程名称.正如您在该程序的MainWindowHandle上方的代码中所看到的那样,该值为0.

rpetrich: Tried your method before and the problem is, I don't know the window name, it differs from user to user, version to version - just the exe name remains constant. All I have is the process name. And as you can see in the code above the MainWindowHandle of the process is 0.

罗杰: 是的,我的意思是任务栏通知区域-感谢您的澄清. 我需要呼叫PostQuitMessage.我只是不知道怎么做,仅给出一个过程,而不是一个窗口.

Roger: Yes, I did mean the taskbar notification area - thanks for the clarification. I NEED to call PostQuitMessage. I just don't know how, given a processs only, and not a Window.

克雷格: 我很高兴解释这种情况:该应用程序具有命令行界面,允许您指定指示其作用以及将结果保存在何处的参数.但是一旦运行,停止它并获得结果的唯一方法是在托盘通知中右键单击它,然后选择退出".

Craig: I'd be glad to explain the situation: the application has a command line interface, allowing you to specify parameters that dictate what it would do and where will it save the results. But once it's running, the only way to stop it and get the results is right-click it in the tray notification are and select 'exit'.

现在我的用户想要编写脚本/批处理该应用程序.他们从批处理开始完全没有问题(只需指定exe名称和一堆标志),但随后就陷入了正在运行的进程.假设没有人会更改提供API以便在运行时停止它的过程(它已经很旧了),我需要一种人为关闭它的方法.

Now my users want to script/batch the app. They had absolutely no problem starting it from a batch (just specify the exe name and and a bunch of flags) but then got stuck with a running process. Assuming no one will change the process to provide an API to stop it while running (it's quite old), I need a way to artificially close it.

类似地,在无人值守的计算机上,可以通过任务调度或操作控制程序来启动用于启动进程的脚本,但是无法关闭进程.

Similarly, on unattended computers, the script to start the process can be started by a task scheduling or operations control program, but there's no way to shut the process down.

希望可以澄清我的情况,再次感谢所有尝试提供帮助的人!

Hope that clarifies my situation, and again, thanks everyone who's trying to help!

这篇关于从C#关闭最小化/图标化的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆