Click here to Skip to main content
15,898,010 members
Home / Discussions / C#
   

C#

 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 11:23
Jacob Dixon12-Apr-10 11:23 
GeneralRe: Stopping threads (semi-safely).... Pin
William Winner12-Apr-10 11:01
William Winner12-Apr-10 11:01 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 11:07
Jacob Dixon12-Apr-10 11:07 
GeneralRe: Stopping threads (semi-safely).... Pin
William Winner12-Apr-10 12:49
William Winner12-Apr-10 12:49 
GeneralRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 13:40
Jacob Dixon12-Apr-10 13:40 
AnswerRe: Stopping threads (semi-safely).... Pin
Jacob Dixon12-Apr-10 14:38
Jacob Dixon12-Apr-10 14:38 
GeneralRe: Stopping threads (semi-safely).... Pin
DaveyM6912-Apr-10 14:46
professionalDaveyM6912-Apr-10 14:46 
AnswerRe: Stopping threads (semi-safely).... Pin
DaveyM6912-Apr-10 14:44
professionalDaveyM6912-Apr-10 14:44 
Reading through the messages above, you don't seem to have grasped the 'unsubscribe from event' way of dealing with this.

If you make sure that your thread updates the UI only via an event then by unsubscribing to that event, the UI update code will not get called! All the InvokeRequired stuff should be in the event consumer's handler method.

It's not clear whether you wish to let the thread complete naturally after the application closes or terminate it immediately. By setting IsBackground to true, it will terminate. Not setting (or setting to false) will allow it to continue.

Here's some demo code which may help explain!
C#
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Forms;

namespace ThreadingDemo
{
    public partial class FormMain : Form
    {
        MyThreadedClass myThreadedClass;

        public FormMain()
        {
            InitializeComponent();
            myThreadedClass = new MyThreadedClass();
            myThreadedClass.GotDataCompleted += myThreadedClass_GotDataCompleted;
            FormClosing += FormMain_FormClosing;
            Shown += new EventHandler(FormMain_Shown);
        }

        void FormMain_Shown(object sender, EventArgs e)
        {
            // Call one of these depending on your requirements for thread termination

            //RunThread();
            //RunThreadAsBackground();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Unsubscribing from event ensures UI will not be updated once form is closed!
            myThreadedClass.GotDataCompleted -= myThreadedClass_GotDataCompleted;
        }
        private void myThreadedClass_GotDataCompleted(object sender, GotDataCompletedEventArgs e)
        {
            if (InvokeRequired)
                Invoke(new MethodInvoker(delegate { myThreadedClass_GotDataCompleted(sender, e); }));
            else
            {
                foreach (string item in e.Data)
                    Text = item;
            }
        }
        // Application will close but thread will continue to run until completed.
        private void RunThread()
        {
            myThreadedClass.Begin();
        }
        // Thread will terminate when application closes.
        private void RunThreadAsBackground()
        {
            myThreadedClass.SetAsBackground();
            myThreadedClass.Begin();
        }
    }

    public class MyThreadedClass
    {
        public event EventHandler<GotDataCompletedEventArgs> GotDataCompleted;

        private Thread thread;

        public MyThreadedClass()
        {
            thread = new Thread(new ThreadStart(Start));
        }

        public void Begin()
        {
            thread.Start();
        }
        protected virtual void OnGotDataCompleted(GotDataCompletedEventArgs e)
        {
            EventHandler<GotDataCompletedEventArgs> eh = GotDataCompleted;
            if (eh != null)
                eh(this, e);
        }
        private void Start()
        {
            GotDataCompletedEventArgs e = new GotDataCompletedEventArgs();
            e.AddData("Data 1");
            // Simulate long operation
            Thread.Sleep(10000);
            e.AddData("Data 2");
            OnGotDataCompleted(e);
        }
        public void SetAsBackground()
        {
            thread.IsBackground = true;
        }
    }

    public class GotDataCompletedEventArgs : EventArgs
    {
        private List<string> data;

        public GotDataCompletedEventArgs()
        {
            data = new List<string>();
        }

        public ReadOnlyCollection<string> Data
        {
            get { return data.AsReadOnly(); }
        }

        internal void AddData(string item)
        {
            data.Add(item);
        }
    }
}

Dave

If this helped, please vote & accept answer!


Binging is like googling, it just feels dirtier. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

QuestionMultithreaded drawing Pin
Groulien12-Apr-10 9:07
Groulien12-Apr-10 9:07 
AnswerRe: Multithreaded drawing Pin
Tarakeshwar Reddy12-Apr-10 9:37
professionalTarakeshwar Reddy12-Apr-10 9:37 
AnswerRe: Multithreaded drawing Pin
ntrceptr12-Apr-10 9:46
ntrceptr12-Apr-10 9:46 
AnswerRe: Multithreaded drawing Pin
William Winner12-Apr-10 10:16
William Winner12-Apr-10 10:16 
QuestionUser and Password Prompt Pin
Jassim Rahma12-Apr-10 5:59
Jassim Rahma12-Apr-10 5:59 
AnswerRe: User and Password Prompt Pin
Dave Kreskowiak12-Apr-10 6:14
mveDave Kreskowiak12-Apr-10 6:14 
GeneralRe: User and Password Prompt Pin
Jassim Rahma12-Apr-10 6:28
Jassim Rahma12-Apr-10 6:28 
GeneralRe: User and Password Prompt Pin
J4amieC12-Apr-10 6:35
J4amieC12-Apr-10 6:35 
AnswerRe: User and Password Prompt Pin
Ravi Bhavnani12-Apr-10 19:17
professionalRavi Bhavnani12-Apr-10 19:17 
QuestionWebBrowser Control - .NET 3.5 - C# Pin
Jon Braunsma12-Apr-10 4:58
Jon Braunsma12-Apr-10 4:58 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
Not Active12-Apr-10 5:26
mentorNot Active12-Apr-10 5:26 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
itsravie12-Apr-10 5:46
itsravie12-Apr-10 5:46 
AnswerRe: WebBrowser Control - .NET 3.5 - C# Pin
Jon Braunsma12-Apr-10 9:40
Jon Braunsma12-Apr-10 9:40 
GeneralRe: WebBrowser Control - .NET 3.5 - C# Pin
Dave Kreskowiak12-Apr-10 9:55
mveDave Kreskowiak12-Apr-10 9:55 
Questionhow to get textbox value from one class to another Pin
Sr...Frank12-Apr-10 4:44
Sr...Frank12-Apr-10 4:44 
AnswerRe: how to get textbox value from one class to another Pin
ramzg12-Apr-10 5:13
ramzg12-Apr-10 5:13 
AnswerRe: how to get textbox value from one class to another Pin
DaveyM6912-Apr-10 14:55
professionalDaveyM6912-Apr-10 14:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.