Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Terminating/joining a thread in C# [duplicate]

$
0
0

I can't seem to be able to kill my thread in C#. The program seems to get stuck in an infinite loop on the FormClosing event.

EDIT // I'm attempting to end the thread and close the whole program when the FormClosing event gets fired.

Here's the code:

public partial class Form1 : Form{    private Thread thread;    private volatile bool threadRunning = true;    public Form1()    {        InitializeComponent();    }    private void Loop()    {        Console.WriteLine(threadRunning);        while (threadRunning)        {            MethodInvoker mi = delegate { timeLabel.Text = TimeWriterSingleton.Instance.OutputTime(); };            Invoke(mi);        }    }    private void Form1_Load(object sender, EventArgs e)    {        thread = new Thread(Loop);        thread.Start();    }    private void Form1_FormClosing(object sender, FormClosingEventArgs e)    {        threadRunning = false;        thread.Join();    }}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>