Implementing Thread in ASP.NET


I thought of implementing Thread in ASP.NET application.

Please find the steps and techniques here to create a sample windows application which uses Thread.
Excuse for any mistakes and please suggest your thoughts or inputs.

I have a simple class named - Loader, which is having a method Load which will keep running for some times.

Loader.cs
public class NotificationEventArgs
    {
        public string Status { get; set; }
    }

    public delegate void MyHandler(object source, NotificationEventArgs e);
   
    public class Loader
    {
        public event MyHandler LoadCompleted;

        public Loader() {
           
        }

        public void Load(Form1 form) {
           
            Thread.Sleep(5000);
            LoadCompleted("comp", null);
        }
    }


Form.cs
public partial class Form1 : Form {
        private Thread thread;

        private delegate void ResetCallback();
        public Form1() {
            InitializeComponent();
            ThreadButton.BackColor = Color.SteelBlue;
        }

        private void ThreadButton_Click(object sender, EventArgs e) {
           
            var testLoader=new Loader();
           
            //testLoader.Load();
            thread=new Thread(() => testLoader.Load(this));
            thread.Start();
            testLoader.LoadCompleted+=threadCompleteHandler;

            StatusLabel.Text = @"Thread started..";
            ThreadButton.BackColor = Color.Tomato;

            //Thread td=new Thread(new ParameterizedThreadStart(testLoader.Load);
        }

        private void threadCompleteHandler(object source,NotificationEventArgs e) {
            ResetForm();
           
        }

        private void ResetForm() {
            if(StatusLabel.InvokeRequired && ThreadButton.InvokeRequired) {
                var reset=new ResetCallback(ResetForm);
                Invoke(reset, new object[] {});
            } else {
                StatusLabel.Text = @"Thread completed..";
                ThreadButton.BackColor = Color.YellowGreen;
            }
           
        }
    }






Comments