I'm running a winform application that uses a contextMenuStrip on a notifyIcon to exit the application. The exit function stops another thread that is running, turns off the Icon visibility, and then exits the application. For some reason though I have to call Application.Exit() twice to exit the application. I searched through all of my code and didn't find another Application.Run() call, so I'm not sure what would be causing this.
These are the primary methods that are running:
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Thread socketThread = new Thread(ServerStream.RunServer);
socketThread.Start();
Application.Run(new LicenseManagerForm());
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
ServerStream.StopListenerTask();
NotifyIcon.Visible = false;
Application.Exit();
Application.Exit();
}
internal static async void RunServer()
{
// Create an IPv4 TCP/IP socket.
TcpListener serverSocket = new TcpListener(IPAddress.Any, 11000);
// Listen for incoming connections.
serverSocket.Start();
while (!_stopServer)
{
try
{
TcpClient clientRequest = await GetTcpClient(serverSocket, _cancellationTokenSource.Token);
if (clientRequest != null) AuthenticateClient(clientRequest);
}
catch (Exception e)
{
//Program.WriteTextToStatusBar(e.ToString());
_ = MessageBox.Show(e.ToString());
throw;
}
}
}
private static async Task<TcpClient> GetTcpClient(TcpListener listener, CancellationToken token)
{
_ = token.Register(listener.Stop);
try
{
return await listener.AcceptTcpClientAsync().ConfigureAwait(false);
}
catch (ObjectDisposedException)
{
if (token.IsCancellationRequested) return null;
throw;
}
}
internal static void StopListenerTask()
{
_stopServer = true;
_cancellationTokenSource.Cancel();
}
Is there anything here that would cause the first Application.Exit() to fail?