C# 控制台窗口在gui后面启动

C# 控制台窗口在gui后面启动,c#,console-application,C#,Console Application,我已经为服务器创建了gui,但每当我启动gui时,控制台窗口也会打开,如果我关闭控制台窗口,gui也会关闭。控制台窗口没有做任何事情,它只是在我启动gui时打开。我怎样才能避免这种情况?我只希望gui启动,而不是控制台窗口 namespace ServerUI { public partial class Server : Form { public Server() { InitializeComponent(); //default port

我已经为服务器创建了gui,但每当我启动gui时,控制台窗口也会打开,如果我关闭控制台窗口,gui也会关闭。控制台窗口没有做任何事情,它只是在我启动gui时打开。我怎样才能避免这种情况?我只希望gui启动,而不是控制台窗口

namespace ServerUI
{
public partial class Server : Form
{
    public Server()
    {
        InitializeComponent();

        //default port number
        textBox1.Text = "8001";

        button1.Click += button1_Click;
    }

    //Global Variables
    public static class Globals
    {
        public const string IP = "127.0.0.1";

        public static int port_number = 0;
        public static string selected = "";
        public static string selected_1 = "";
    }

    //IP address of server
    static IPAddress ipAddress = IPAddress.Parse(Globals.IP);

    //List of active clients connected to server
    List<Socket> active_clients = new List<Socket>();

    static Socket serverSocket;
    static byte[] buffer = new Byte[1024];
    public static ManualResetEvent allDone = new ManualResetEvent(false);

    private void button1_Click(object sender, EventArgs e)
    {
        if (String.IsNullOrEmpty(textBox1.Text.Trim()))
        {
            System.Windows.Forms.MessageBox.Show("Port Number Empty", "Error");
            return;
        }
        else
        {
            bool check = int.TryParse(textBox1.Text, out Globals.port_number);
            if (!check)
            {
                MessageBox.Show("Port Number not in correct format. Enter Port Number again", "Error");
                return;
            }
            client_pkt_parsing.client_list.Clear();
            foreach (DataGridViewRow myRow in dataGridView1.Rows)
            {
                myRow.Cells[1].Value = null; // assuming you want to clear the first column
            }
            foreach (DataGridViewRow myRow in dataGridView2.Rows)
            {
                myRow.Cells[1].Value = null; // assuming you want to clear the first column
            }
            //Starting Server
            StartServer();
        }
    } //end of button1_Click

    public void StartServer()
    {
        byte[] bytes = new Byte[1024];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Globals.port_number);

        try
        {
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(localEndPoint);
            serverSocket.Listen(10);
            serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
            infoBox.Text = "Server started. Waiting for a connection...";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }//end of StartServer

    public void AcceptCallback(IAsyncResult ar)
    {
        allDone.Set();
        try
        {
            Socket clientSocket = serverSocket.EndAccept(ar);
            infoBox.Text = infoBox.Text + "\r\n" + string.Format(DateTime.Now.ToString("HH:mm:ss") + " > Client : " + clientSocket.RemoteEndPoint.ToString() + "connected");
            clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), clientSocket);
            serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
        }
        catch (Exception ex)
        {

        }
    }//end of AcceptCallback

    public void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            int received = 0;
            Socket current = (Socket)ar.AsyncState;
            received = current.EndReceive(ar);
            byte[] data = new byte[received];

            if (received == 0)
            {
                return;
            }

            Array.Copy(buffer, data, received);
            string text = Encoding.ASCII.GetString(data);
            testBox.Text = testBox.Text + "\r\n" + text;
            //Send(current, "hello");
            buffer = null;
            Array.Resize(ref buffer, current.ReceiveBufferSize);

            current.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
        }
        catch (Exception ex)
        {

        }
    }//end of RecieveCallback


}
}
namespace服务器用户界面
{
公共部分类服务器:表单
{
公共服务器()
{
初始化组件();
//默认端口号
textBox1.Text=“8001”;
按钮1.点击+=按钮1\u点击;
}
//全局变量
公共静态类全局
{
public const string IP=“127.0.0.1”;
公共静态int端口号=0;
选择公共静态字符串=”;
已选择公共静态字符串_1=“”;
}
//服务器的IP地址
静态IPAddress-IPAddress=IPAddress.Parse(Globals.IP);
//连接到服务器的活动客户端列表
列出活动客户端=新建列表();
静态套接字服务器套接字;
静态字节[]缓冲区=新字节[1024];
public static ManualResetEvent allDone=新的ManualResetEvent(false);
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(String.IsNullOrEmpty(textBox1.Text.Trim())
{
System.Windows.Forms.MessageBox.Show(“端口号为空”、“错误”);
返回;
}
其他的
{
bool check=int.TryParse(textBox1.Text,out Globals.port_编号);
如果(!检查)
{
MessageBox.Show(“端口号格式不正确。请再次输入端口号”,“错误”);
返回;
}
client_pkt_parsing.client_list.Clear();
foreach(DataGridViewRow myRow在dataGridView1.Rows中)
{
myRow.Cells[1].Value=null;//假设要清除第一列
}
foreach(DataGridViewRow myRow在dataGridView2.Rows中)
{
myRow.Cells[1].Value=null;//假设要清除第一列
}
//启动服务器
StartServer();
}
}//结束按钮1\u单击
public void StartServer()
{
字节[]字节=新字节[1024];
IPEndPoint localEndPoint=新IPEndPoint(ipAddress,Globals.port_number);
尝试
{
serverSocket=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
绑定(localEndPoint);
serverSocket.Listen(10);
serverSocket.BeginAccept(新的AsyncCallback(AcceptCallback),null);
infoBox.Text=“服务器已启动。正在等待连接…”;
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message、Application.ProductName、MessageBoxButtons.OK、MessageBoxIcon.Error);
}
}//StartServer的结束
公共无效接受回调(IAsyncResult ar)
{
allDone.Set();
尝试
{
Socket clientSocket=serverSocket.EndAccept(ar);
infoBox.Text=infoBox.Text+“\r\n”+string.Format(DateTime.Now.ToString(“HH:mm:ss”)+”)客户端:“+clientSocket.RemoteEndPoint.ToString()+”已连接”);
clientSocket.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,新异步回调(ReceiveCallback),clientSocket);
serverSocket.BeginAccept(新的AsyncCallback(AcceptCallback),null);
}
捕获(例外情况除外)
{
}
}//接受回调结束
public void ReceiveCallback(IAsyncResult ar)
{
尝试
{
接收到的int=0;
插座电流=(插座)ar.AsyncState;
已接收=当前的EndReceive(ar);
字节[]数据=新字节[已接收];
如果(接收==0)
{
返回;
}
复制(缓冲区、数据、接收);
字符串文本=Encoding.ASCII.GetString(数据);
testBox.Text=testBox.Text+“\r\n”+文本;
//发送(当前“你好”);
缓冲区=空;
Array.Resize(ref buffer,current.ReceiveBufferSize);
current.BeginReceive(buffer,0,buffer.Length,SocketFlags.None,新异步回调(ReceiveCallback),current);
}
捕获(例外情况除外)
{
}
}//ReceiveCallback结束
}
}

这通常发生在使用错误的项目模板启动时。它是可以修复的。使用项目+属性,应用程序选项卡。将“输出类型”设置从控制台应用程序更改为Windows应用程序。不再使用控制台。

这些被称为主机终端,您是否在一个通常在终端上运行的应用程序中创建GUI?可能会显示一些代码?还有一些细节。这是什么样的申请?设置中的控制台或GUI?这是GUI不是控制台您可以从IDE的开始图标(通常是调试)或生成的.exe文件运行此设置吗?抱歉,我是.net新手,IDE开始图标在哪里?我正在从调试文件夹中的.exe文件启动GUI。