Simple threading, why is this happening? (C # WinForms)

I am currently studying thread implementation in C # WinForms, and I created this simple application:

I'm just wondering why the memory usage in this application continues to grow after I start, stop, start and stop the application again. I had the thought that my stream instance does not stop and does not interrupt when I click the stop button. Please review my code below and any help or suggestions would be greatly appreciated.

alt text

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ThreadingTest
{
    public partial class Form1 : Form
    {
        private delegate void TickerDelegate(string s);
        bool stopThread = false;
        TickerDelegate tickerDelegate1;
        Thread thread1;

        public Form1()
        {
            InitializeComponent();
            tickerDelegate1 = new TickerDelegate(SetLeftTicker);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            thread1 = new Thread(new ThreadStart(disp));
            thread1.Start();
        }

        void disp()
        {
            while (stopThread == false)
            {
                listBox1.Invoke(tickerDelegate1, new object[] { DateTime.Now.ToString() });
                Thread.Sleep(1000);
            }
        }

        private void SetLeftTicker(string s)
        {
            listBox1.Items.Add(s);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            stopThread = true;
            if (thread1.IsAlive)
            {
                thread1.Abort();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            stopThread = false;
            thread1 = new Thread(new ThreadStart(disp));
            thread1.Start();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (thread1.IsAlive)
            {
                MessageBox.Show("Is Alive!");
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
    }
}
+5
source share
3 answers

OK, there are a few recommendations:

Use flying flag

... , .

volatile bool stopThread = false;

IsBackground true: , , " ", .

thread1.IsBackground = true;
thread1.Start();

, , ... , Abort, :

... Abort , . , , , finally . , . , .

Abort

, , Interrupt :

private void btnStop_Click(object sender, EventArgs e)
{
    // have another method for re-use
    StopThread();
}

private void StopThread()
{
    stopThread = true;

    // the time out is 100 ms longer than the thread sleep
    thread1.Join(1100);

    // if the thread is still alive, then interrupt it
    if(thread1.IsAlive)
    {
        thread1.Interrupt();
    }
}

, ""... thread1 , , . , .

private void btnStart_Click(object sender, EventArgs e)
{
    // stop the previous thread
    StopThread();

    // create a new thread
    stopThread = false;
    thread1 = new Thread(new ThreadStart(disp));
    thread1.IsBackground = true;// set it to background again
    thread1.Start();
}

, :

void disp()
{
    try
    {
        while (stopThread == false)
        {
            listBox1.Invoke(tickerDelegate1, new object[] { DateTime.Now.ToString() });
            Thread.Sleep(1000);
        }
    }
    catch(ThreadInterruptedException)
    {
        // ignore the exception since the thread should terminate
    }
}

, ... ... , : !;)

+6

. - 1 , . , Abort ( , .) ​​ , Abort , , .

.NET, , . GC.Collect(), .

+3

In btnStop_Click you set the flag to true, but then immediately check if it is all alive. This does not give anyone the opportunity to naturally cease. Instead, you should wait in the stream using Join (as hydrogen suggests), and if it expires (for some reason), then interrupt the stream.

0
source

All Articles