App refuses to close


E

Ebrahim

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net;
using System.Net.Sockets;
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.ComponentModel.Container components = null;
private System.Windows.Forms.RichTextBox 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.Collections.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
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent 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.Dispose();
}
}
base.Dispose( disposing );
Application.Exit();

}

#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 InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.Exit = new System.Windows.Forms.Button();
this.rtb = new System.Windows.Forms.RichTextBox();
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.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(296, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 32);
this.button1.TabIndex = 0;
this.button1.Text = "Start Server";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Exit
//
this.Exit.Location = new System.Drawing.Point(296, 88);
this.Exit.Name = "Exit";
this.Exit.Size = new System.Drawing.Size(120, 32);
this.Exit.TabIndex = 1;
this.Exit.Text = "Stop";
this.Exit.Click += new System.EventHandler(this.Exit_Click);
//
// rtb
//
this.rtb.Location = new System.Drawing.Point(8, 8);
this.rtb.Name = "rtb";
this.rtb.Size = new System.Drawing.Size(224, 288);
this.rtb.TabIndex = 2;
this.rtb.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(248, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(32, 32);
this.label1.TabIndex = 5;
//
// listBox1
//
this.listBox1.ItemHeight = 20;
this.listBox1.Location = new System.Drawing.Point(424, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(160, 124);
this.listBox1.TabIndex = 6;
//
// listBox2
//
this.listBox2.ItemHeight = 20;
this.listBox2.Location = new System.Drawing.Point(424, 144);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(160, 144);
this.listBox2.TabIndex = 7;
//
// button2
//
this.button2.Location = new System.Drawing.Point(296, 48);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(120, 32);
this.button2.TabIndex = 8;
this.button2.Text = "Start AServer";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(248, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(32, 32);
this.label2.TabIndex = 10;
//
// button3
//
this.button3.Location = new System.Drawing.Point(296, 128);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(120, 32);
this.button3.TabIndex = 11;
this.button3.Text = "Exit";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(8, 19);
this.ClientSize = new System.Drawing.Size(592, 307);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button3,
this.label2,
this.button2,
this.listBox2,
this.listBox1,
this.label1,
this.rtb,
this.Exit,
this.button1});
this.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Server Side";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());



}

private void button1_Click(object sender, System.EventArgs e)
{

if(button1.Text.Equals("Start Server"))
{

startServ = new Thread(new ThreadStart(this.startListeningC));

startServ.Name ="StartServer";
startServ.Start();
button1.Enabled = false;
Exit.Enabled = true;


}


}

public void startListeningC()
{
try
{

label1.BackColor = System.Drawing.Color.Green;
serv.Start();

while(true)
{
while(!serv.Pending())
{
Thread.Sleep(500);
if(label1.BackColor == System.Drawing.Color.Red)
label1.BackColor = System.Drawing.Color.Green;
else
label1.BackColor = System.Drawing.Color.Red;

}
this.ThisClient=this.getClientNo();

if(this.ThisClient!=-1)
{
obj[this.ThisClient] = new connObj();
obj[this.ThisClient].tc = serv.AcceptTcpClient();
obj[this.ThisClient].No = this.ThisClient;
isFree[this.ThisClient] = false;
listBox1.Items.Add("Client#"+this.ThisClient.ToString());

obj[this.ThisClient].myForm = this;
this.myThread = new Thread(new
ThreadStart(obj[this.ThisClient].StartProtocol));
obj[this.ThisClient].ConnType =
(int)ConnectionType.ServerClientConnection;
obj[this.ThisClient].myThread = this.myThread;
this.myThread.Start();

}
else
{
this.Text += "Clients Waiting...";
label1.BackColor = System.Drawing.Color.Orange;
Thread.Sleep(5000);
}

}



}
catch(Exception se)
{
MessageBox.Show("Problem in Main Thread\n"+ se.Message +
"\nExecution Stopped");
this.Stop();
}
}

public void startListeningA()
{
try
{

label2.BackColor = System.Drawing.Color.Green;
aux.Start();

while(true)
{
while(!aux.Pending())
{
Thread.Sleep(500);
if(label2.BackColor == System.Drawing.Color.Red)
label2.BackColor = System.Drawing.Color.Green;
else
label2.BackColor = System.Drawing.Color.Red;

}

this.ThisClient = this.getClientNo();
if(this.ThisClient!=-1)
{
obj[this.ThisClient] = new connObj();
obj[this.ThisClient].tc = aux.AcceptTcpClient();
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)ConnectionType.ServAuxConnection;
obj[this.ThisClient].myThread = this.myThread;
this.myThread.Start();

}
else
{
this.StdOut("Servers Waiting");
label2.BackColor = System.Drawing.Color.Orange;
Thread.Sleep(5000);
}



}



}
catch(Exception se)
{
MessageBox.Show("Problem in Main Thread\n"+ se.Message +
"\nExecution Stopped");
this.Stop();
}
}

public int getClientNo()
{
for(int i=0;i<this.NoClients;i++)
{
if(isFree==true)
return i;
}
return -1;
}



private void Exit_Click(object sender, System.EventArgs e)
{
this.Stop();
button1.Enabled = true;
button2.Enabled = true;
Exit.Enabled = false;

}

void Stop()
{
Exit.Enabled = false;
label1.BackColor = System.Drawing.Color.Red;
label2.BackColor = System.Drawing.Color.Red;
if(startServ!=null)
{
if(startServ.IsAlive)
{
startServ.Suspend();
serv.Stop();
}
}

if(startServA!=null)
{
if(startServA.IsAlive)
{
this.startServA.Suspend();
aux.Stop();
}
}
if(this.getClientNo()!=0)
{
for(int i=0;i<this.NoClients;i++)
{
if(!isFree)
{
obj.disconnect();

}
}
}
}



private void Form1_Load(object sender, System.EventArgs e)
{

this.Text = System.Net.Dns.GetHostName() + " Central Server";
label1.BackColor = System.Drawing.Color.Red;
label2.BackColor = System.Drawing.Color.Red;
isFree = new System.Boolean[this.NoClients];
for(int i=0;i<this.NoClients;i++)
{
isFree = true;

}
obj = new connObj[this.NoClients];
this.myIPQ = new System.Collections.Queue();
Exit.Enabled = false;
try
{
serv = new TcpListener(port);//Client Port
aux = new TcpListener(7000);//Aux port


}
catch(Exception se)
{
MessageBox.Show(se.Message);
}
}

public void StdOut(String s)
{
rtb.Text += "\n" + s;
}



private void button2_Click(object sender, System.EventArgs e)
{
startServA = new Thread(new ThreadStart(this.startListeningA));

startServA.Name ="StartServerA";
startServA.Start();
button2.Enabled = false;
Thread painter = new Thread(new ThreadStart(this.paintQ));
painter.Start();
Exit.Enabled = true;
}

private void paintQ()
{
while(true)
{
Thread.Sleep(500);

System.Collections.IEnumerator iter = this.myIPQ.GetEnumerator();
this.listBox2.Items.Clear();
while(iter.MoveNext())
{
listBox2.Items.Add(iter.Current.ToString());
}
}
}

private void button3_Click(object sender, System.EventArgs e)
{
this.Dispose();

}






}

}
 
Ad

Advertisements

H

hOSAM

I did not read your code, but I guess you need something like:

System.Environment.Exit( 0 );

on your way out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top