0

I am sorry if i do/did something wrong here, this is my first question in this website.

I am trying to completly exit my application (process) when a BeginConnect throws an error (mainly SocketException).

My code right now :

try
{
     Socket.BeginConnect(new IPEndPoint(IPAddress.Parse(ip), port), new AsyncCallback(ConnectionCallBack), Socket);
}
catch (System.Exception ex)
{
     OnError(new ErrorEventArgs(ex));
}

The ErrorEvent fires, and this is where i try to exit my application :

if (e.Ex is SocketException)
{
     MessageBox.Show(LanguageManager.GetText("Erreur1"));
     client.Dispose();
     Application.Exit();
}

But then, the debug mode is still running.. or if i try it without the debug mode, i find the process still there in the task manager.

Thanks for helping !

Haytam
  • 4,643
  • 2
  • 20
  • 43
  • Try to extend BeginConnect with timeout : http://stackoverflow.com/questions/1062035/how-to-configure-socket-connect-timeout –  Jun 30 '16 at 00:47

1 Answers1

0

I suspect your problem is with the main process still waiting for its child thread to shutdown before exiting. Try closing your socket connection using the Close() api, let the thread shutdown gracefully and see if the main process exits.

Documentation: Socket.Close()

Alternatively you can do System.Environment.Exit(1), but it is a brutal way of killing the application. You generally won't have to do it that way but if you have to then use it.

https://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.110).aspx

Environment.Exit Method (Int32)

.NET Framework (current version) Other Versions Terminates this process and returns an exit code to the operating system.

Example:

enum ExitCode : int {
  Success = 0,
  ForceTermination = 1,
  SocketException = 2
}

System.Environment.Exit(ExitCode.ForceTerminate);
Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • None of the two solutions work.. the process is still there :/ When i try the application without the server connexion, it closes the process when i hit the red "X" button, so there are no other threads used except with the Async Callback – Haytam Jun 30 '16 at 09:28
  • Not sure why :/ did a quick research these guy seems to have solve it. http://stackoverflow.com/questions/554408/why-would-application-exit-fail-to-work let me know how u go with it lol – Samuel Toh Jun 30 '16 at 10:52