带有隐藏窗口和用户输入的C#Console应用程序 [英] C# Console app with hidden window and user input

查看:134
本文介绍了带有隐藏窗口和用户输入的C#Console应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我有一个基于服务器的C#控制台应用程序。该应用程序必须

在其现场启动时隐藏其控制台窗口。所以我在发布时将P / Invoke''dindWindow / ShowWindow组合尽职尽责地隐藏了

控制台窗口。


应用程序(由于遗留原因)等待旧的

方式的Console.ReadLine()语句。


如何终止此类应用程序外部?通过外部我

意味着通过批处理文件或脚本或其他一些?启动是简单的

部分。隐藏的控制台窗口如何接收键盘输入以便关闭代码可以执行?


或者我是否以错误的方式解决这个问题? />

解决方案

Dilip< rd ***** @ lycos.comwrote:


我有一个基于服务器的C#控制台应用程序。该应用程序必须

在其现场启动时隐藏其控制台窗口。所以我在发布时将P / Invoke''dindWindow / ShowWindow组合尽职尽责地隐藏了

控制台窗口。


应用程序(由于遗留原因)等待旧的

方式的Console.ReadLine()语句。


如何终止此类应用程序外部?通过外部我

意味着通过批处理文件或脚本或其他一些?启动是简单的

部分。隐藏的控制台窗口如何接收键盘输入以便关闭代码可以执行?


或者我是以错误的方式进行此操作?



如果你不想要一个控制台,你为什么要把它变成一个控制台应用?只需

创建一个Windows服务或一个简单的Windows窗体应用程序,而不是显示任何表格的




-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复小组,请不要给我发邮件




" Dilip" < rd ***** @ lycos.com写了留言

news:11 ********************* @ k79g2000hse.googlegro ups.com ...


大家好


我有一个基于服务器的C#控制台应用程序。该应用程序必须

在其现场启动时隐藏其控制台窗口。所以我在b / Invoke''dindWindow / ShowWindow组合中尽可能地b / b
在启动时隐藏

控制台窗口。



为什么你有一个没有界面的控制台应用程序?

你对基于服务器的是什么意思?这是否意味着该应用程序将在

a服务器上运行并且可能没有人登录它?


您是否考虑过将其设为Windows服务?


应用程序(由于遗留原因)等待旧的
方式的Console.ReadLine()语句。



如果这是一个新的应用程序,那么遗产会发生什么?部分?


9月6日晚上1:10,Jon Skeet [C#MVP]< sk ... @ pobox.comwrote:


Dilip< rdil ... @ lycos.comwrote:


我有一个基于服务器的C#控制台应用程序。该应用程序必须

在其现场启动时隐藏其控制台窗口。所以我在b / Invoke''dindWindow / ShowWindow组合中尽可能地b / b
在启动时隐藏

控制台窗口。


应用程序(由于遗留原因)等待旧的
方式的Console.ReadLine()语句。


如何在外部终止此类应用程序?通过外部我

意味着通过批处理文件或脚本或其他一些?启动是简单的

部分。隐藏的控制台窗口如何接收键盘输入,以便关闭代码可以执行?


或者我是以错误的方式解决这个问题?



如果你不想要一个控制台,你为什么要把它变成一个控制台应用?只需

创建一个Windows服务或一个简单的Windows窗体应用程序,而不是显示任何表格的



我应该已经用所有这些开始我的问题。


应用程序*是一项服务,但很奇怪它还推出了一个基于Winforms的复杂应用程序(现在请不要问我为什么或如何

- 这就是它的方式)。但是在Vista下,服务无法与桌面进行交互(因为它们在会话0上运行,这与运行的UI应用程序不同,因此无法启动另一个应用程序) />
显示用户界面。


所以我试图通过将该应用程序转换为

控制台应用程序而不是控制台来向后工作窗口隐藏。


当我想关闭它时,我遇到的问题发生了。

因为它现在隐藏了我不知道如何优雅地关闭它。


鉴于有限制,有没有办法从外部的

脚本中做到这一点?它必须是一个外部批处理文件或者其他东西,它可能在开始菜单中有一个捷径。


我愿意接受这样的做法不依赖于Console.ReadLine()。


也许我可以创建一个事件并等待它。


>来自一个完全不同的小app2,可以再次从



脚本启动我可能会在事件上做一个SetEvent因此

打开并让代码优雅地关闭。


那会起作用吗?


Hi All

I have a server based C# console application. This application must
hide its console window when its launched out on the field. So I
dutifully P/Invoke''d FindWindow/ShowWindow combination to hide the
console window at launch time.

The application (for legacy reasons) hangs around by waiting on an old-
fashioned Console.ReadLine() statement.

How can such an application be terminated externally? By externally I
mean via a batch file or script or some such? Launching is the easy
part. How does a hidden console window recieve keyboard input so that
the shut down code can execute?

Or am I going about this the wrong way?

解决方案

Dilip <rd*****@lycos.comwrote:

I have a server based C# console application. This application must
hide its console window when its launched out on the field. So I
dutifully P/Invoke''d FindWindow/ShowWindow combination to hide the
console window at launch time.

The application (for legacy reasons) hangs around by waiting on an old-
fashioned Console.ReadLine() statement.

How can such an application be terminated externally? By externally I
mean via a batch file or script or some such? Launching is the easy
part. How does a hidden console window recieve keyboard input so that
the shut down code can execute?

Or am I going about this the wrong way?

Why have you made it a console app if you don''t want a console? Just
create a Windows Service or a simple Windows Forms app that happens not
to display any forms.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Hi,

"Dilip" <rd*****@lycos.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...

Hi All

I have a server based C# console application. This application must
hide its console window when its launched out on the field. So I
dutifully P/Invoke''d FindWindow/ShowWindow combination to hide the
console window at launch time.

Why you have a console app that have no interface?
What do you mean with "server based"? Does it means that the app will run in
a server and is probably nobody is logged on it?

Did you look into making it a windows service?

The application (for legacy reasons) hangs around by waiting on an old-
fashioned Console.ReadLine() statement.

If this is a new app, what is going on with the "legacy" part?


On Sep 6, 1:10 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:

Dilip <rdil...@lycos.comwrote:

I have a server based C# console application. This application must
hide its console window when its launched out on the field. So I
dutifully P/Invoke''d FindWindow/ShowWindow combination to hide the
console window at launch time.

The application (for legacy reasons) hangs around by waiting on an old-
fashioned Console.ReadLine() statement.

How can such an application be terminated externally? By externally I
mean via a batch file or script or some such? Launching is the easy
part. How does a hidden console window recieve keyboard input so that
the shut down code can execute?

Or am I going about this the wrong way?


Why have you made it a console app if you don''t want a console? Just
create a Windows Service or a simple Windows Forms app that happens not
to display any forms.

I should''ve prefaced my question with all of this.

The application *was* a service but strangely it also launches a
complex Winforms based application (now please don''t ask me why or how
-- thats just the way it is). Under Vista however Services cannot
interact with the desktop (as they run on Session 0 which is different
from the ones UI apps run) and hence cannot launch another application
that displays UI.

So I am trying to work backwards by converting that application to a
console app but w/the console window hidden.

The problem I am running into happens when I want to shut it down.
Because its now hidden I don''t know how to shut it down gracefully.

Given the constraints is there any way to do it from an external
script? It has to be an external batch file or something that
probably has a short cut to it in the Start menu.

I am open to approaches that don''t rely on Console.ReadLine().

Maybe I could create an event and wait on it.

>From a totally different tiny app2 that can be launched from the

script once again I could probably do a SetEvent on the event thus
opened and get the code to shut down gracefully.

Will that work?


这篇关于带有隐藏窗口和用户输入的C#Console应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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