private void Button_Click(object sender, RoutedEventArgs e) { try { string trimed_port = port_textBox.Text.Trim(); if (trimed_port == "") { // Error Related Port Number cannot be empty.\ MessageBox.Show("Port number cannot be empty !!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); } else // SOme port number has entered. { int portnumber = Convert.ToInt32(trimed_port); // Converting Port String to Int. string Ip_address = ip_textBox.Text; if (portnumber > 65535) // I.e Empty port or Port number has greaterthan 65535 { MessageBox.Show("Port number should be lessthan 65535!!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); port_textBox.Text = ""; // Setting the Empty text in Port_textbox. port_textBox.Focus(); // to set the port Textbox to be Focused } else // Perfect Port Number. { /* Call Start_TcpServer() Method to satrt Tcp Server. */ HideBackgroundCntent(); Dlg = new Modal_dlg(); Dlg.Owner = Window.GetWindow(this); Server = new ServerClass(); // To Start the TcpListener Server.Initialize_Server(Ip_address, portnumber); // To Start the Tcp Server. Dlg.ShowDialog(); if (Global.Is_Manuallyclosed) { Global.Is_Manuallyclosed = false; Server.ClearAllObbjectsused(); // To Show the Background content ShowBackgroundCntent(); } else //I.e The Tcp Client has been Connected to the Server , SO you should not stop the Server. { /* * 0. Start the Processing Timer to Process the Data_Buffer. * */ Processing_Timer = new System.Timers.Timer(500); // Checking for evey 0.5 Sec whether any Client Connected or not. Processing_Timer.Elapsed += new ElapsedEventHandler(Server.ProcessDataBuffer); Processing_Timer.AutoReset = true; Processing_Timer.Enabled = true; /* * SendDatatoClient() Should be accessible from outside * 1. Close the MainWindow * 2. Display the Commands Window * * /*/ } } // Should close the Main Form & Will Another Form can be Shown. } } catch (Exception ex) { DateTime now_time = DateTime.Now; string time = Convert.ToString(now_time); Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured On Main Form Connect Click: " + ex.Message + time); // Logging the Execptions Ocuured } }
protected override void OnClosed(EventArgs e) // On Closing the MainWindow We Should Close the Application Also. { base.OnClosed(e); Server.ClearAllObbjectsused(); // On Closing the Application Need to clear the Memory of static even thogh CLR 4.0 will clear the Static Mmbers Jus Used for safty Application.Current.Shutdown(); }