0
現在我有這樣的:後臺運行服務器
[STAThread]
static void Main()
{
if (flag) //client view
Application.Run(new Main_Menu());
else
{
Application.Run(new ServerForm());
}
}
ServerForm.cs
public partial class ServerForm : Form
{
public ServerForm()
{
InitializeComponent();
BeginListening(logBox);
}
public void addLog(string msg)
{
this.logBox.Items.Add(msg);
}
private void button1_Click(object sender, EventArgs e)
{
}
private async void BeginListening(ListBox lv)
{
Server s = new Server(lv);
s.Start();
}
}
Server.cs
public class Server
{
ManualResetEvent allDone = new ManualResetEvent(false);
ListBox logs;
///
///
/// Starts a server that listens to connections
///
public Server(ListBox lb)
{
logs = lb;
}
public void Start()
{
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Loopback, 1440));
while (true)
{
Console.Out.WriteLine("Waiting for connection...");
allDone.Reset();
listener.Listen(100);
listener.BeginAccept(Accept, listener);
allDone.WaitOne(); //halts this thread
}
}
//other methods like Send, Receive etc.
}
我想運行我的ServerForm
(它有ListBox可以從Server
打印msg)。我知道ListBox
參數不起作用,但我不能運行Server
inifinite循環沒有掛起ServerForm
(我甚至不能移動窗口)。我也嘗試過線程 - 不幸的是,它不適用於。
表單是一個UI組件,不是應該在套接字上偵聽的東西。將服務器分成它自己的類,讓它報告它的狀態(例如通過事件)並將它託管在後臺工作者,任務或線程中。 – CodeCaster
因此,如果我添加到我的'ServerForm' EvenListener和生成即使從'服務器'將'ServerForm'接收它? –
使用背景工作。 – jdweng