[英]How to open a window in console application?
我需要在應用程序中顯示一個窗口,但這是控制台。
我試過使用:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
代碼保持這樣。
static void Main(string[] args)
{
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
ConnectToServer();
RequestLoop();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
但是窗口不會打開,如果我先放置代碼,則會打開窗口,但是控制台不會執行命令。
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
ConnectToServer();
RequestLoop();
}
這兩個代碼都不起作用。
在第一個代碼中,窗口不會打開。
在第二個代碼中,打開了窗口,但是控制台不執行命令
嘗試此操作,您會忘記“ Console.ReadLine();”,如果它不存在,那么您的控制台應用程序將被關閉,並且您的窗口也會被關閉。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Task.Run(() => Application.Run(new Form1()));
Console.ReadLine();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.