Hello!
Here is a small article -
Show progress on long-running operations, which describes approach to keep the UI responsive, starting long-running operations in separate thread. This approach is native and without any 3d-party libraries and even without BackgroundWorker.
For example, the code below is sample simulates a treatment of group of files:
private void startBtn_Click(object sender, EventArgs e)
{
string[] filePaths = ...
UpdateProgressBar(0);
ThreadPool.QueueUserWorkItem(new WaitCallback(new Action<object>(delegate(object state)
{
DoSomethingWithFiles(filePaths);
})));
}
private void DoSomethingWithFiles(string[] filePaths)
{
for (int i = 0; i < filePaths.Length; i++)
{
string fileText = File.ReadAllText(filePaths[i]);
...
UpdateProgressBar((i + 1) * 100 / filePaths.Length);
}
UpdateProgressBar(100);
}
private void UpdateProgressBar(int currentValue)
{
if (progressBar1.InvokeRequired)
progressBar1.BeginInvoke(new Action(delegate() { UpdateProgressBar(currentValue); }));
else
progressBar1.Value = currentValue;
}