Fik løst problemerne. Opdagede at jeg faktisk ikke havde brug for background workers siden jeg brugte asynkrone operationer. Troede fejlagtigt også at selve Callbacks'ne kørte videre i samme tråd som den der kaldte Begin* men det gør det ikke.
Fik lavet 2 dialogbokse; en til klienter og en til server. Der er 2 ting der nok bør ændres. Den ene er at jeg sætter en bool når der klikkes annuller som callbacken check fra en anden tråd, men umiddelbart virker det. Den anden at server dialog boksen begynder at lytte allerede fra constructoren og ikke først når der bliver kaldt ShowDialog. Det betyder at kan ende med en løs socket hvis man aldrig kalder ShowDialog. Nogle forslag til at løse den?
Hvis nogen er interesserede er her koden til server og klient dialogboksen. I server-version venter den bare på en forbindelse og man kan annullere. I klienten kan man skrive IP den vil forsøge at forbinde. Man kan også annullere forsøget her og starte igen. Begge bruger standard DialogResult's, OK for forbindelse oprettet, Cancel for annullering og Abort for fejl.
Klientside:
- public partial class ConnectDialog : Form
- {
- private int _dots = 0;
- private bool _connecting = false;
-
- private TcpClient _tcpClient;
-
- public TcpClient Client
- {
- get { return _tcpClient; }
- private set { _tcpClient = value; }
- }
-
- public ConnectDialog()
- {
- InitializeComponent();
- }
-
- private bool Connecting
- {
- get { return _connecting; }
- set {
- if (_connecting == value)
- return;
- _connecting = value;
- if (value)
- {
- timerAnimation.Enabled = true;
- buttonConnect.Text = "Cancel";
- }
- else
- {
- timerAnimation.Enabled = false;
- buttonConnect.Text = "Connect";
- labelAddress.Text = "Address of server";
- }
- }
- }
-
- //Dual function. Connect/Cancel
- private void buttonConnect_Click(object sender, EventArgs e)
- {
- if (!Connecting)
- {
- IPAddress serverAddress;
- if (!IPAddress.TryParse(textAddress.Text, out serverAddress))
- {
- MessageBox.Show("Address does not appear to be a valid IP address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- return;
- }
- try
- {
- _tcpClient = new TcpClient();
- _tcpClient.BeginConnect(serverAddress, Properties.Settings.Default.Port, new AsyncCallback(ConnectCallback), _tcpClient);
- Connecting = true;
- }
- catch (SocketException se)
- {
- Connecting = false;
- MessageBox.Show("Connection Error: " + se.Message);
- CloseSocket();
- }
- }
- else
- {
- Connecting = false;
- CloseSocket();
- }
- }
-
- private void ConnectCallback(IAsyncResult ar)
- {
- if (!Connecting)
- return;
- try
- {
- TcpClient client = (TcpClient)ar.AsyncState;
- client.EndConnect(ar);
- MessageBox.Show("Successfully connected to server");
- this.BeginInvoke(new Action(() => this.Close()));
- }
- catch (SocketException se)
- {
- MessageBox.Show("Connection error: " + se.Message);
- Connecting = false;
- CloseSocket();
- }
- catch (Exception e)
- {
- MessageBox.Show("Error: " + e.Message);
- Connecting = false;
- CloseSocket();
- }
- }
-
- private void buttonQuit_Click(object sender, EventArgs e)
- {
- Connecting = false;
- DialogResult = DialogResult.Cancel;
- CloseSocket();
- this.Close();
- }
-
- private void CloseSocket()
- {
- if (_tcpClient != null)
- _tcpClient.Close();
- }
-
- private void timerAnimation_Tick(object sender, EventArgs e)
- {
- _dots = (_dots + 1) % 4;
- string dots = new String('.', _dots);
- labelAddress.Text = "Connecting" + dots;
- }
- }
Serverside:
- public partial class HostDialog : Form
- {
- private int _dots = 0;
- private bool _exit = false;
- private TcpListener _tcpListener;
-
- private TcpClient _tcpClient;
-
- public TcpClient Client
- {
- get { return _tcpClient; }
- private set { _tcpClient = value; }
- }
-
- public HostDialog()
- {
- InitializeComponent();
- StartServer();
- }
-
- private void StartServer()
- {
- try
- {
- _tcpListener = new TcpListener(IPAddress.Any, Properties.Settings.Default.Port);
- _tcpListener.Start();
- _tcpListener.BeginAcceptTcpClient(new AsyncCallback(AcceptCallback), _tcpListener);
- }
- catch (SocketException se)
- {
- MessageBox.Show("Error starting server: " + se.Message);
- DialogResult = DialogResult.Abort;
- this.Close();
- }
- }
-
- private void AcceptCallback(IAsyncResult ar)
- {
- if (_exit)
- return;
- try
- {
- TcpListener tcpListener = (TcpListener)ar.AsyncState;
- TcpClient tcpClient = tcpListener.EndAcceptTcpClient(ar);
- _tcpClient = tcpClient;
- IPEndPoint otherEnd = (IPEndPoint)tcpClient.Client.RemoteEndPoint;
- MessageBox.Show("Client connected from IP: " + otherEnd.Address);
- timerAnimation.Enabled = false;
- DialogResult = DialogResult.OK;
- }
- catch (SocketException se)
- {
- MessageBox.Show("Connection error: " + se.Message);
- DialogResult = DialogResult.Abort;
- }
- catch (Exception e)
- {
- MessageBox.Show("Error: " + e.Message);
- DialogResult = DialogResult.Abort;
- }
- _tcpListener.Stop();
- this.BeginInvoke(new Action(() => this.Close()));
- }
-
- private void timerAnimation_Tick(object sender, EventArgs e)
- {
- _dots = (_dots + 1) % 4;
- string dots = new String('.', _dots);
- label1.Text = "Waiting for client" + dots;
- }
-
- private void buttonCancel_Click(object sender, EventArgs e)
- {
- _exit = true;
- _tcpListener.Stop();
- DialogResult = DialogResult.Cancel;
- this.Close();
- }
- }
Indlæg senest redigeret d. 05.06.2011 12:04 af Bruger #14645