Your secondary threads are still running and the main thread will only exit once these thread(s) have finished what they needed to do.
Because you have infinite loop
while (true)...
that thread will never exit so you will need to stop it.
Here is some code to stop it.
public partial class Monitor : UserControl
{
check chk = new check();
public static Thread thread;
public Monitor()
{
InitializeComponent();
thread = new Thread(chk.changecolor);
chk.getButton = this.button1;
thread.Start();
}
private void button1_Click(object sender, EventArgs e)
{
chk.isChange = true;
}
public void StopThread()
{
chk._CanRun = false;
}
}
Also, you need to change your infinite loop
try something like this
class check
{
bool t = false;
bool change = false;
public bool _CanRun = true;
Button b;
public void changecolor() {
while (_CanRun)
{
Thread.Sleep(300);
}
}
public bool isChange {
get{ return t;}
set{
t= value;
change = value;
}
}
public Button getButton {
get {return b;}
set {b = value;}
}
}
This will let the thread exit gracefully.
Here you should call the stopThread from the main from
private List<monitor> MonitorList = new List<monitor>();
public Main()
{
InitializeComponent();
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Main_FormClosing);
flowLayoutPanel1.AutoScroll = true;
flowLayoutPanel1.Dock = DockStyle.Fill;
Monitor oMonitorTemp = null;
for (int i = 0; i < 10; i++)
{
oMonitorTemp = new Monitor();
flowLayoutPanel1.Controls.Add(oMonitorTemp);
MonitorList.Add(oMonitorTemp);
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
foreach (Monitor mon in MonitorList)
{
mon.StopThread();
}
}
</monitor></monitor>
You should also look at
AutoResetEvent
Try this link ->
http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx[
^]
Regards
Terence