简体   繁体   English

如何检测正在执行应用程序的上下文? 无论是从命令提示符还是从Windows窗体内部

[英]How to detect the context from which an application is being executed? Whether from Command Prompt or from within a Windows Form

I am trying to run a set of test cases through one of our internal applications. 我正在尝试通过我们的一个内部应用程序运行一组测试用例。 I need to be able to automate these tests and run them through the command line and log various things. 我需要能够自动执行这些测试,并通过命令行运行它们并记录各种内容。 The problem is that the existing code does not separate the view from the controller and throws MessageBoxes and Alerts everywhere that ask the user to click on a button (in my case just OK/CONTINUE). 问题在于,现有代码无法将视图与控制器分开,并且会在所有要求用户单击按钮的地方抛出MessageBoxes和Alerts(在我的情况下,仅是OK / CONTINUE)。 Currently, it has been decided to introduce state flags that will help determine the context from which the application is being run so one can decide whether to do a Console.WriteLine() or a MessageBox.Show() . 当前,已经决定引入状态标志,这将有助于确定从中运行应用程序的上下文,因此可以决定是执行Console.WriteLine()还是MessageBox.Show() But as you can imagine this has cascaded into a lot of changes and dirty if-else blocks. 但是正如您可以想象的那样,它已级联成许多更改和肮脏的if-else块。

Sadly, it doesn't look feasible to sit down and separate the logic from the view at this stage. 可悲的是,在这个阶段坐下来将逻辑与视图分离似乎并不可行。 So I was wondering if there was a way to detect the context in which the application is executing. 所以我想知道是否有一种方法可以检测应用程序正在执行的上下文。 I would like to replace each of the MessageBox() call with a Notify() call which itself can detect the context - whether to show the output on a command prompt or pop up a form. 我想用本身可以检测上下文的Notify()调用替换每个MessageBox()调用-是在命令提示符下显示输出还是弹出表单。

EDIT_1: Any other suggestions for doing this are also welcome. EDIT_1:也欢迎您提出其他建议。

In my application, I parse the command line arguments and set some internal flags. 在我的应用程序中,我解析命令行参数并设置一些内部标志。 If you add that type of parsing to the entry point of your application, the existance of the command line argument being set to "Y" for example - can set the appropriate flag in the application used to determine where the output is sent. 如果将这种类型的解析添加到应用程序的入口点,则命令行参数的存在例如设置为“ Y”-可以在应用程序中设置适当的标志,用于确定将输出发送到哪里。

Personally, I capture all the "STDOUT" (console) output to a log file, then if not running in "scheduled" mode - I pop up a msgbox() . 就个人而言,我将所有“ STDOUT”(控制台)输出捕获到日志文件,然后,如果未在“计划”模式下运行-我会弹出msgbox()

In C using the Win32 API (off the top of my head, untested): 在使用Win32 API的C语言中(未经测试):

// requires Windows 2000 or later (for GetConsoleWindow)
int parent_owns_the_console()
{
    DWORD pid=0;
    GetWindowThreadProcessId(GetConsoleWindow(), &pid);
    return pid && pid != GetCurrentProcessId();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打开命令提示符并从Windows窗体应用程序执行命令 - open Command Prompt and execute commands from Windows Form Application 如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数? - How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET? 从用户目录检测我的应用程序是否正在执行 - Detect if my application is being executed from the users directory MSBuild 正在从“Visual Studio 命令提示符”中运行 - MSBuild is being run from within the "Visual Studio Command Prompt" 使用SendMessage从Windows窗体应用程序将字符串发送到命令提示符(cmd) - Send String to Command Prompt (cmd) from Windows Form Application using SendMessage 从控制台应用程序获取响应(Windows中的命令提示符) - Get Response from Console Application (command prompt in windows) 如何检测应用程序是从启动启动还是由用户启动? - How to detect whether application started from startup or started by user? 通过命令提示符检测程序是否正在运行 - detect whether a program is being run via command prompt 如何从代码中检测Visual Studio调试执行上下文? - How to detect Visual Studio debug execution context from within code? 如何从窗体的命令提示符中使用Outlook Switch? - How to use Outlook Switch in Command Prompt from Form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM