• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1066
  • Last Modified:

How to kill hanging process after Async EndConnect(ar) throw an exception?

I am using Async socket connection (.NET 2003, c#) to connect from Windows (client) to Linux (server).  When server is down, client's EndConnect(ar) throws an exception which I catch and properly get the error message.  However, this running process is still hanging in Windows task manager which I have to manually kill it.  Does anyone know how I can kill this running process after an exception arise?  Below are partial codes.

private static void ConnectCallback(IAsyncResult ar)
{
  try
  {
    Socket client = (Socket) ar.AsyncState;
    client.EndConnect(ar);
    Console.WriteLine("Socket connected to {0}",
    client.RemoteEndPoint.ToString());
    connectDone.Set();
  }
  catch (Exception e)
  {
    MessageBox.Show(e.ToString(), "Socket Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    MessageBox.Show("Server is not responding!", "Socket Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    ############
    # What should I do here to kill current running process?
    ############
  }
}
0
chuang3
Asked:
chuang3
  • 4
  • 2
1 Solution
 
gregoryyoungCommented:
signal your main thread to exit (most likely with an autoreset event .... This has little to do with you process exiting it is the main thread that is in a wait state of some sort which needs to exit.

Cheers,

Greg
0
 
chuang3Author Commented:
Hi Greg,
  Thanks for the replied!  I am new to .NET C# world and don't quite understand what you mean by "autoreset event".  Can you be more specific in terms of coding wise?  Thanks in advance!
0
 
mastooCommented:
(just passing by, no points to me)  We probably need you to post your main startup code to see what it does after hooking up your ConnectCallback.  It's either looping, sleeping, or waiting on something.  The suggestion is this wait on an AutoResetEvent.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gregoryyoungCommented:
yep, that would make things easier as we can just show show you how to make the change.
0
 
chuang3Author Commented:
Here is the source codes.  If I debug into it, it fails at "client.EndConnect(ar);" in "ConnectCallback(IAsyncResult ar) " function.  Then, the exception catches it and show error message.  After clicks "OK" in message window, application process is still hanging in Windows' task manager.

public void AsynchConnect(bool expectReceive)
{
  public byte[] receiveBuffer = new byte[64 * 1024];
  public byte[] streamBuffer = new byte[128 * 1024];
  public byte[] sendBuffer = new byte[32 * 1024];
  Array.Clear(receiveBuffer , 0, receiveBuffer.Length);
  Array.Clear(streamBuffer, 0, streamBuffer.Length);
  Array.Clear(sendBuffer , 0, sendBuffer.Length);  try
  {
     IPHostEntry ipHostInfo = Dns.Resolve("hostname");
     IPAddress ipAddress = ipHostInfo.AddressList[0];
     IPEndPoint remoteEP = new IPEndPoint(ipAddress, "9093");
     Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     client.Blocking = false;
     client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
     connectDone.WaitOne();
     Send(client);
     sendDone.WaitOne();
     Thread.Sleep(2000);
     if (expectReceive)
     {
       Receive(client);
       receiveDone.WaitOne();
       Thread.Sleep(1000);
       FileStream RecieveStream = File.Create("receiveFileName");
       RecieveStream.Write(receiveBuffer, 0, 0);
       RecieveStream.Close();
     }

     client.Shutdown(SocketShutdown.Both);
     client.Close();
   }
   catch (Exception e)
   {
     MessageBox.Show(e.ToString(), "Socket Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
   }
}

private static void ConnectCallback(IAsyncResult ar)
{
  try
  {
    Socket client = (Socket) ar.AsyncState;
    client.EndConnect(ar);
    Console.WriteLine("Socket connected to {0}",
    client.RemoteEndPoint.ToString());
    connectDone.Set();
  }
  catch (Exception e)
  {
    MessageBox.Show(e.ToString(), "Socket Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    MessageBox.Show("Server is not responding!", "Socket Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    ############
    # What should I do here to kill current running process?
    ############
  }
}

public static bool Login()
{
  AsynchConnect(true);
  // Process to read received file and varify user.
  // return true if a valid user, return false if not.
}

static void Main()
{
  // Verify User Login
 bool validUser = Login();
 if (validUser)
   Application.Run(new MyClient());
 else
   Application.Exit();
}
0
 
gregoryyoungCommented:
Application.Exit()
0
 
gregoryyoungCommented:
or set connectDone and handle the case that the socket is not connected. i.e.

     connectDone.WaitOne();
     if(client.Connected) {
     Send(client);
     sendDone.WaitOne();
     Thread.Sleep(2000);
     if (expectReceive)
     {
       Receive(client);
       receiveDone.WaitOne();
       Thread.Sleep(1000);
       FileStream RecieveStream = File.Create("receiveFileName");
       RecieveStream.Write(receiveBuffer, 0, 0);
       RecieveStream.Close();
     }


  catch (Exception e)
  {
    MessageBox.Show(e.ToString(), "Socket Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    MessageBox.Show("Server is not responding!", "Socket Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally {
    connectDone.Set();
    }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now