473,405 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

App refuses to close

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.ToStr ing());

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[i]==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[i])
{
obj[i].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[i] = 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();

}


}

}
Nov 15 '05 #1
1 1821
I did not read your code, but I guess you need something like:

System.Environment.Exit( 0 );

on your way out.
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Wayne Aprato | last post by:
Can anyone please help me with the following madness in Access 2003 (2000 file format). I am using 2 similar funtions, the first which works fine to save reports as snapshots: On Error Resume...
7
by: Daniel | last post by:
I have an winform application that uses System.IO.StreamReader and Serializer.Deserialize to load some data from a file, if any error is detected in the file a new file should be written to the...
1
by: ticmanis | last post by:
Hello, I'm having trouble getting MSIE 6.0 (running on XP SP2) to accept a cookie which works fine in both Firefox and wget. The web server is Boa 0.94.13 (a small embedded server) using PHP...
13
by: teric2 | last post by:
Hi Everyone! I have a form that opens from a another form set as a dialog box via a command button. I have tried about everything I can think of to make this form maximize when it opens. I've used...
3
by: RadhikaG | last post by:
my dropdownlist_selectedindexchange method just refuses to get invoked when i switch from any value in the dropdown list to the default value. why??? how do i get it work for this as well?
12
by: Rainer Queck | last post by:
Hi NG, I have no idea why, but since a while a dataset refuses to write to a specific xml file. I get the error message "Der Zugriff auf den Pfad...
0
by: poe | last post by:
Hello, I have a large database table running on SQL Server 8.0. It currently has nearly 98 million records on it's ways to 125 million. I am doing inserts as quickly as the server can handle it,...
0
by: VaBa | last post by:
Hi, Need some help.. I have a VBA class module in MS ACCESS. In the same MS ACCESS app, I am calling a .NET DLL (which i have exposed as COM-visible) and passing BYREF an instance of the VBA class...
5
by: NDayave | last post by:
How Do, I have a rather simple website, which has some SQL embedded in the ASP to write user data to the underlying mdb database. The first SQL statement looks up all the records in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.