Pattern - Form Initialize


Creating

Open Visual Studio 2022 as Administrator.
Create a New Project, language C#, platform Windows, project type Desktop.
Find "Windows Forms App" and press Next
Change the Project Name to "WinForms_Threading_BWOnInitialize".
Check / change the Location if necessary.
Press Next.
Check / Change the Framework to .NET 6.0.
Press Create.
This will create a solution that contains one project
SS
Displays the Toolbox and drag the following controls on to the Windows Form.
Button - Name="btnCancel", Text="Cancel"
ListBox - Name="lblStopWatch", Text="Not Started"
TextBox - Name="txtText", Multiline=True
SS
Add the following code to the Form1.cs file

namespace WinForms_Threading_BWOnInitialize 
{
    public partial class Form1 : Form
    {
        System.ComponentModel.BackgroundWorker? _workerThread = null;
        bool _keepRunning = false;

        public Form1()
        {
            InitializeComponent();

            method_InstantiateWorkerThread();
            this._workerThread.RunWorkerAsync();
        }

        private void method_InstantiateWorkerThread()
        {
            this._workerThread = new System.ComponentModel.BackgroundWorker();
            this._workerThread.DoWork += method_WorkerThread_DoWork;

            this._workerThread.WorkerReportsProgress = true;
            this._workerThread.ProgressChanged += method_WorkerThread_ProgressChanged;

            this._workerThread.RunWorkerCompleted += method_WorkerThread_RunWorkerCompleted;
            this._workerThread.WorkerSupportsCancellation = true;
        }

        private void method_WorkerThread_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            this._keepRunning = true;

            while (this._keepRunning)
            {
                System.Threading.Thread.Sleep(1000);
                this._workerThread.ReportProgress(0);

                if (this._workerThread.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

        private void method_WorkerThread_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            this.listBox1.Items.Add("another one");
        }

        private void method_WorkerThread_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.listBox1.Items.Add("cancelled");
            }
            else
            {
                this.listBox1.Items.Add("completed");
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this._keepRunning = false;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this._workerThread.CancelAsync();
            this.Close();
        }
    }
}

Running

Build the solution and press F5.
When the Windows Form is displayed the Listbox will be populated every second with an additional item.
While the Listbox is being updated you can type into the Textbox at the same time.
Press the Stop Button to stop the Listbox from being updated.
Press the Cancel Button to close the Windows Form.


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext