Reputation: 5
I have created a simple C# Form application and there is another class Mouse_Tracking.cs
When I click start button, thread start and works fine. but when I click stop button Nothing happens
Could you please help me to fix the issue of fallowing code. :( :( :(
here the code of Mouse_Tracking class.
public class Mouse_Tracking
{
public int flag = 1;
public void run()
{
while (flag == 1)
{
//Do Something
}
}
Here the code of start button
private void btn_start_Click(object sender, EventArgs e)
{
var mst = new Mouse_Tracking();
Thread thread1 = new Thread(new ThreadStart(mst.run));
thread1.Start();
}
Here the code of Stop button
private void btn_stop_Click(object sender, EventArgs e)
{
var mst = new Mouse_Tracking();
mst.flag = 0;
}
Upvotes: 0
Views: 163
Reputation: 116098
You are not setting the flag of the object you created in btn_start_Click. Instead you create a new Mouse_Tracking
object and set its value. Use the same instance....
For ex;
declare it as
Mouse_Tracking mst = new Mouse_Tracking();
private void btn_start_Click(object sender, EventArgs e)
{
mst = new Mouse_Tracking();
.......
}
private void btn_stop_Click(object sender, EventArgs e)
{
mst.flag = 0;
}
Upvotes: 4