转到应用程序属性并将输出类型从控制台应用程序更改为 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
此外,您可以将应用程序作为服务运行。为此,您应该创建一个服务 - File->New Project->Visual C#->Windows->Windows Service。然后创建一个公共方法StartWork() 并在那里添加所有逻辑。并在OnStart()中调用这个方法。
protected override void OnStart(string[] args)
{
try
{
this.StartJobs();
}
catch (Exception ex)
{
// catching exception
}
}
public void StartWork()
{
// all the logic here
}
在 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
}