This message is in reply to a prev 1 .
My application refues to close . Some one had suggested that I might
have threads running.. but i solved that problem too . The app still
refuses to close . !!
Here is the code for one of them !!
I call a stop function to disconnect all objects (close ) . I also
make a call to Stop in the Dispose() function .
PLs Help
--------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
using System.Net;
using System.Net.Sock ets;
using System.IO;
namespace ServerT
{
/// <summary>
/// Summary description for Form1.
/// </summary>
///
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
public String file2b=null;
private System.Windows. Forms.Button Exit;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
private System.Windows. Forms.RichTextB ox rtb;
public int port = 6500;
public TcpListener serv,aux;
public int NoClients = 10;
public int ThisClient = 0;
private System.Windows. Forms.Label label1;
public System.Windows. Forms.ListBox listBox1;
public System.Boolean [] isFree;
public ServerT.connObj [] obj;
public Thread myThread;
public Thread startServ;
public Form2 fm;
private System.Windows. Forms.ListBox listBox2;
public System.Collecti ons.Queue myIPQ ;
public bool queueHasChanged =false;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.Label label2;
private System.Windows. Forms.Button button3;
public Thread startServA;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(Exit.Enabled )
this.Stop();
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
Application.Exi t();
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.Exit = new System.Windows. Forms.Button();
this.rtb = new System.Windows. Forms.RichTextB ox();
this.label1 = new System.Windows. Forms.Label();
this.listBox1 = new System.Windows. Forms.ListBox() ;
this.listBox2 = new System.Windows. Forms.ListBox() ;
this.button2 = new System.Windows. Forms.Button();
this.label2 = new System.Windows. Forms.Label();
this.button3 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(296, 8);
this.button1.Na me = "button1";
this.button1.Si ze = new System.Drawing. Size(120, 32);
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Start Server";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// Exit
//
this.Exit.Locat ion = new System.Drawing. Point(296, 88);
this.Exit.Name = "Exit";
this.Exit.Size = new System.Drawing. Size(120, 32);
this.Exit.TabIn dex = 1;
this.Exit.Text = "Stop";
this.Exit.Click += new System.EventHan dler(this.Exit_ Click);
//
// rtb
//
this.rtb.Locati on = new System.Drawing. Point(8, 8);
this.rtb.Name = "rtb";
this.rtb.Size = new System.Drawing. Size(224, 288);
this.rtb.TabInd ex = 2;
this.rtb.Text = "";
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(248, 8);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(32, 32);
this.label1.Tab Index = 5;
//
// listBox1
//
this.listBox1.I temHeight = 20;
this.listBox1.L ocation = new System.Drawing. Point(424, 8);
this.listBox1.N ame = "listBox1";
this.listBox1.S ize = new System.Drawing. Size(160, 124);
this.listBox1.T abIndex = 6;
//
// listBox2
//
this.listBox2.I temHeight = 20;
this.listBox2.L ocation = new System.Drawing. Point(424, 144);
this.listBox2.N ame = "listBox2";
this.listBox2.S ize = new System.Drawing. Size(160, 144);
this.listBox2.T abIndex = 7;
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(296, 48);
this.button2.Na me = "button2";
this.button2.Si ze = new System.Drawing. Size(120, 32);
this.button2.Ta bIndex = 8;
this.button2.Te xt = "Start AServer";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// label2
//
this.label2.Loc ation = new System.Drawing. Point(248, 48);
this.label2.Nam e = "label2";
this.label2.Siz e = new System.Drawing. Size(32, 32);
this.label2.Tab Index = 10;
//
// button3
//
this.button3.Lo cation = new System.Drawing. Point(296, 128);
this.button3.Na me = "button3";
this.button3.Si ze = new System.Drawing. Size(120, 32);
this.button3.Ta bIndex = 11;
this.button3.Te xt = "Exit";
this.button3.Cl ick += new System.EventHan dler(this.butto n3_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(8, 19);
this.ClientSize = new System.Drawing. Size(592, 307);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.button3,
this.label2,
this.button2,
this.listBox2,
this.listBox1,
this.label1,
this.rtb,
this.Exit,
this.button1});
this.FormBorder Style =
System.Windows. Forms.FormBorde rStyle.FixedSin gle;
this.MaximizeBo x = false;
this.MinimizeBo x = false;
this.Name = "Form1";
this.Text = "Server Side";
this.Load += new System.EventHan dler(this.Form1 _Load);
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
if(button1.Text .Equals("Start Server"))
{
startServ = new Thread(new ThreadStart(thi s.startListenin gC));
startServ.Name ="StartServe r";
startServ.Start ();
button1.Enabled = false;
Exit.Enabled = true;
}
}
public void startListeningC ()
{
try
{
label1.BackColo r = System.Drawing. Color.Green;
serv.Start();
while(true)
{
while(!serv.Pen ding())
{
Thread.Sleep(50 0);
if(label1.BackC olor == System.Drawing. Color.Red)
label1.BackColo r = System.Drawing. Color.Green;
else
label1.BackColo r = System.Drawing. Color.Red;
}
this.ThisClient =this.getClient No();
if(this.ThisCli ent!=-1)
{
obj[this.ThisClient] = new connObj();
obj[this.ThisClient].tc = serv.AcceptTcpC lient();
obj[this.ThisClient].No = this.ThisClient ;
isFree[this.ThisClient] = false;
listBox1.Items. Add("Client#"+t his.ThisClient. ToString());
obj[this.ThisClient].myForm = this;
this.myThread = new Thread(new
ThreadStart(obj[this.ThisClient].StartProtocol) );
obj[this.ThisClient].ConnType =
(int)Connection Type.ServerClie ntConnection;
obj[this.ThisClient].myThread = this.myThread;
this.myThread.S tart();
}
else
{
this.Text += "Clients Waiting...";
label1.BackColo r = System.Drawing. Color.Orange;
Thread.Sleep(50 00);
}
}
}
catch(Exception se)
{
MessageBox.Show ("Problem in Main Thread\n"+ se.Message +
"\nExecutio n Stopped");
this.Stop();
}
}
public void startListeningA ()
{
try
{
label2.BackColo r = System.Drawing. Color.Green;
aux.Start();
while(true)
{
while(!aux.Pend ing())
{
Thread.Sleep(50 0);
if(label2.BackC olor == System.Drawing. Color.Red)
label2.BackColo r = System.Drawing. Color.Green;
else
label2.BackColo r = System.Drawing. Color.Red;
}
this.ThisClient = this.getClientN o();
if(this.ThisCli ent!=-1)
{
obj[this.ThisClient] = new connObj();
obj[this.ThisClient].tc = aux.AcceptTcpCl ient();
obj[this.ThisClient].No = this.ThisClient ;
isFree[this.ThisClient] = false;
obj[this.ThisClient].myForm = this;
this.myThread = new Thread(new
ThreadStart(obj[this.ThisClient].StartProtocol) );
obj[this.ThisClient].ConnType =
(int)Connection Type.ServAuxCon nection;
obj[this.ThisClient].myThread = this.myThread;
this.myThread.S tart();
}
else
{
this.StdOut("Se rvers Waiting");
label2.BackColo r = System.Drawing. Color.Orange;
Thread.Sleep(50 00);
}
}
}
catch(Exception se)
{
MessageBox.Show ("Problem in Main Thread\n"+ se.Message +
"\nExecutio n Stopped");
this.Stop();
}
}
public int getClientNo()
{
for(int i=0;i<this.NoCl ients;i++)
{
if(isFree[i]==true)
return i;
}
return -1;
}
private void Exit_Click(obje ct sender, System.EventArg s e)
{
this.Stop();
button1.Enabled = true;
button2.Enabled = true;
Exit.Enabled = false;
}
void Stop()
{
Exit.Enabled = false;
label1.BackColo r = System.Drawing. Color.Red;
label2.BackColo r = System.Drawing. Color.Red;
if(startServ!=n ull)
{
if(startServ.Is Alive)
{
startServ.Suspe nd();
serv.Stop();
}
}
if(startServA!= null)
{
if(startServA.I sAlive)
{
this.startServA .Suspend();
aux.Stop();
}
}
if(this.getClie ntNo()!=0)
{
for(int i=0;i<this.NoCl ients;i++)
{
if(!isFree[i])
{
obj[i].disconnect();
}
}
}
}
private void Form1_Load(obje ct sender, System.EventArg s e)
{
this.Text = System.Net.Dns. GetHostName() + " Central Server";
label1.BackColo r = System.Drawing. Color.Red;
label2.BackColo r = System.Drawing. Color.Red;
isFree = new System.Boolean[this.NoClients];
for(int i=0;i<this.NoCl ients;i++)
{
isFree[i] = true;
}
obj = new connObj[this.NoClients];
this.myIPQ = new System.Collecti ons.Queue();
Exit.Enabled = false;
try
{
serv = new TcpListener(por t);//Client Port
aux = new TcpListener(700 0);//Aux port
}
catch(Exception se)
{
MessageBox.Show (se.Message);
}
}
public void StdOut(String s)
{
rtb.Text += "\n" + s;
}
private void button2_Click(o bject sender, System.EventArg s e)
{
startServA = new Thread(new ThreadStart(thi s.startListenin gA));
startServA.Name ="StartServerA" ;
startServA.Star t();
button2.Enabled = false;
Thread painter = new Thread(new ThreadStart(thi s.paintQ));
painter.Start() ;
Exit.Enabled = true;
}
private void paintQ()
{
while(true)
{
Thread.Sleep(50 0);
System.Collecti ons.IEnumerator iter = this.myIPQ.GetE numerator();
this.listBox2.I tems.Clear();
while(iter.Move Next())
{
listBox2.Items. Add(iter.Curren t.ToString());
}
}
}
private void button3_Click(o bject sender, System.EventArg s e)
{
this.Dispose();
}
}
}