进入应用程序属性并将输出类型从控制台应用程序更改为 Windows 应用程序。
或者您可以使用下面的代码来完成。
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
在主函数中
const int SW_HIDE = 0;
const int SW_SHOW = 5;
var handle = GetConsoleWindow();
ShowWindow(handle, SW_HIDE); // To hide
ShowWindow(handle, SW_SHOW); // To show
此外,您可以将应用程序作为服务运行。为此,您需要创建一个服务 - 文件->新建项目->Visual C#->Windows->Windows服务。然后创建一个公共方法 StartWork()
并在其中添加所有逻辑。并在 OnStart()
中调用此方法。
protected override void OnStart(string[] args)
{
try
{
this.StartJobs();
}
catch (Exception ex)
{
}
}
public void StartWork()
{
}
在main函数中,你应该创建这个服务并使用System.ServiceProcess.ServiceBase.Run()
来将其作为服务运行,或者调用StartWork()
将其作为控制台应用程序运行。
static void Main(string[] args)
{
TestService = new TestService ();
#if DEBUG
TestService.StartWork()();
#else
System.ServiceProcess.ServiceBase.Run(TestService );
#endif
}