C# / Threading - Progressbar with Database Transactions

To some, threading can be slightly confusing. If you happen to want to do a transaction on the computer you are writing the application for that may take a while, whether it be a database, communication with some PLC, saving large files, and happen to want to include a Progressbar, this sample may help. I am not going to step much into handling stepping with Progressbars, but rather the threading. It was found that thread pools are easily the 'weapon of choice' when it comes to processing asynchronous I/O, post work items, etc. If you don't use threading, you may see that your application locks up for the length of time it takes to process these transactions. In terms of code, you do the following:
using (SqlConnection sqlConn = new SqlConnection(String.Format("Data Source = {0}; User Id = {1}; Password = {2}; Timeout = 0; Integrated Security = True;", cboDatasources.SelectedItem.ToString(), txtUserName.Text, txtPassword.Text)))
    // Open SQL Connection
        // Do some work here
    // Close SQL Connection
Once you have the groundwork for the threading, if you need to modify any content inside threads that were generated in the main threads, such as controls, you need to Invoke.
Invoke(new Action(delegate
     lblProgressUpdate.Text = "Something happening..."; 


Popular posts from this blog

C# - ListView Item Spacing (Padding)

C# - Performance Counters

IIS / ASP.NET - Disabling Compatibility Mode/View (Internet Explorer)