vb.net 后台运行程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12987720/
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
Running program in background
提问by Ruba
I've a mini project I have to do in "Information System Security" subject, the idea is I have to run a program in background when the user is browsing the net and perform some actions.
I've made some search and I think i may need to write a windows service program. Is that correct? or what should I do to make my program run in background?
please any help.
Thanks.
我有一个必须在“信息系统安全”主题中完成的小项目,想法是当用户浏览网络并执行一些操作时,我必须在后台运行一个程序。
我进行了一些搜索,我想我可能需要编写一个 Windows 服务程序。那是对的吗?或者我应该怎么做才能让我的程序在后台运行?
请任何帮助。
谢谢。
回答by Luis Rodriguez
To make your program run in background you can use event Shownof your form.
要使您的程序在后台运行,您可以使用Shown表单的事件。
put this into Shown event:
将其放入 Shown 事件中:
private void Form1_Shown(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
回答by caesay
Just create a Winforms application, delete the form, and put your code in the Main() method in Program.cs. Keep it alive as long as you need, and run whatever background tasks you need too.
只需创建一个 Winforms 应用程序,删除该表单,然后将您的代码放入 Program.cs 的 Main() 方法中。只要您需要,就让它保持活动状态,并运行您需要的任何后台任务。
回答by maja
If you don't want to write a service-Program you can hide your program-windows after the program started with
如果您不想编写服务程序,您可以在程序启动后隐藏您的程序窗口
ShowWindow(HWND,int);
In order to get the Window-Handler to your own Window, you can use for example
为了让 Window-Handler 到你自己的 Window,你可以使用例如
HWND explorer=::FindWindow(NULL,"explorer.exe");
or
或者
HWND h = ::GetTopWindow(0);
while ( h )
{ if(IsWindowVisible(h)==1 && /*check if it's your Window*/)
{ ShowWindow(h,0);//will hide Window
break;
}
h = ::GetNextWindow( h , GW_HWNDNEXT);
}
You will have to
你不得不
#include <winuser.h>
to use this functions
使用这个功能

