Updating progressbar created on different thread - c#

I am currently trying to update a progress bar that is defined in another window that is created by a different thread.
I need to send progress updates from the main thread in order to have the correct value displayed on progress bar.
Any help is appreciated, thanks in advance. Below is the code:
private void StartLoadingWindow(object sender, RoutedEventArgs e)
{
var t = new Thread(ThreadLoadingWindow);
t.SetApartmentState(ApartmentState.STA); //Mandatory
t.Start();
}
private void ThreadLoadingWindow()
{
var w = new LoadingWindowsControl();
w.Closed += (sender, args) =>
{
//Exit Dispatcher when Window closes
Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.SystemIdle);
Dispatcher.Run();
};
w.ShowDialog();
}
Inside the LoadingWindowsControl I have created a simple mvvm progress bar.

The typical way would be to use a Progress<T> object. If this is created on the UI thread it will invoke the ProgressChanged on the UI thread. Attach an event handler to this that updated your progress bar.
It looks however like you are creating multiple UI threads, and this is probably not a good idea. If you want to do something in the background you should use Task.Run, await the result, and update the UI. If you need continuous update you should follow the Progress<T> example. Create an object that you inform of the updates, and let it post the update work to the the UI thread.

Related

C# Progressbar value in foreach()

I have this code:
private void button1_Click(object sender, EventArgs e)
{
var max = 0;
foreach (var address in textBox2.Text.Split(','))
{
max = +1;
}
var maxp = 100 / max;
foreach (var address in textBox2.Text.Split(','))
{
SendMessage(address);
progressBar1.Value = +maxp;
}
}
It calculates how many emails are in the textbox, and then makes a proportion. To each email sent adds the value of progress, the problem is that when I press the button, the progressbar does not move. When all emails are sent the progressbar moves to the end of stroke.
How can I do?
This happens because your loop and the ui are executing on the same thread. While the loop is busy, it can't update the ui
You can use a BackgroundWorker to run the loop on a different thread in the background, then use the BackgroundWorker's ProgressChanged event to update your progressbar. You can learn more about BackgroundWorkers here
Again you are executing code on a thread other than the UI thread. In WinForms you must invoke back to the UI thread and in WPF you must use application dispatcher.
WinForms:
Invoke((MethodInvoker) delegate {
DoSomethingOnUiThread();
});
WPF:
Application.Current.Dispatcher.Invoke(() =>{
DoSomethingOnUiThread();
});
You block the UI thread so it cannot refresh the UI until the processing leaves the method.
See three different solutions here with explicitly used threads, BackgroundWorker and async-await techniques.

Time-consuming task in MainForm, progress bar in another form

I have a main UI that doing some time-consuming work. When it is executing, I would like to open a second form with a progress bar (marquee style) to indicate "working on it".
I have seen people putting the time-consuming task in the BackgroundWorker, however, I would like to run in the main UI thread.
The time-consuming task will be executed in MainForm. I would like to reuse the progress bar for various process, so I am writing a second form ProgressBarForm with BackgroundWorker in it, that would start the _mainWork at the same time as showing progress bar, and will stop and close the ProgressBarForm when _mainWork is done.
Because forms are modals, I am thinking of showing ProgressBarForm in the BackgroundWorker in order not to block MainForm.
Please note that I am not running mainForm in BackgroundWorker. My backgroundWorker just show the form and perhaps report a timer.
public partial class ProgressBarFom : UControl
{
public delegate void MainWork();
private MainWork _mainWork;
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//progressBar.Hide();
this.OnClose(sender, e);
//
backgroundWorker.Dispose();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
//show this ProgressBarForm
this.ShowDialog();
//stop backgroundWorker
//calling this.Close() in RunWorkerComplete
if (backgroundWorker.CancellationPending == true)
{
e.Cancel = true;
return;
}
}
public void CallProgressBar(object sender, EventArgs e)
{
//progressBar.Show();
backgroundWorker.RunWorkerAsync();
_mainWork();
if (backgroundWorker.IsBusy)
backgroundWorker.CancelAsync();
}
}
In MainForm, I am passing mainwork and call ExecWithProgressBar
private void ExecWithProgressBar()
{
ProgressBarFom .MainWork mainWork = new ProgressBarFom .MainWork(ProgressBarMainWork);
ProgressBarFom prBar = new ProgressBarFom (mainWork);
prBar.CallProgressBar(null, null);
}
Some problems I encoutered
Inside DoWork, the same modal issue occurs. ShowDialog() will block the thread and therefore I never get to check CancellationPending to close ProgressBarForm.
ProgressBarForm starts later then the mainWork. I thought when I called CallProgressBar, the backgroundWorker should start well before my mainWork.
Is worker.Dispose() necessary in RunWorkerComplete?
Would it be a better choice to run mainWork in Worker? And why? I decided to let the main thread run this to not disturb the normal flow, what in Main thread will remain in Main thread, Progress bar is like an addon. If we bring it to the worker, would we need another thread to for progress bar itself?
Unless you do some very ugly hacks (like running more than one message loop inside your application) you cannot display a dialog if the thread running the main window is busy. All dialogs use the same thread to do the display update stuff in WinForms. In fact, they even must be running on the same thread.
There's one (sometimes acceptable) hack using Application.DoEvents(), but I wouldn't use it either, because it gets you into a lot of problems as well.
So the simple answer is: This doesn't work. Use a background worker to do lengthy processing.

WPF TabControl tab changes not refreshed

I have a WPF TabControl with two TabItems. I am trying to change the selected tab on code behind on a Button click event and execute some other code. In this example:
private void Button_Click(object sender, RoutedEventArgs e)
{
ConvertDataTabControl.SelectedIndex = 1;
System.Threading.Thread.Sleep(2000);
...
}
I would expect the UI to refresh and move from Tab 0 to Tab 1 and only then execute the Sleep method, but the UI is refreshed only after Button_Click finishes execution. I tried calling InvalidateVisual, but it does not work.
Is there a way to force the UI to refresh before executing Sleep?
Your code runs on the UI thread by default, so nothing else can be executed on the UI thread (such as updating the layout) until the thread finishes executing.
There are many ways of releasing control of the UI thread before the code finishes executing, but I find the simplest is to use a Task from the Task Parallel Library which can be used to run code on a separate thread.
For example,
Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
// Other code here
});
It should be noted that UI objects can only be modified on the UI thread, so if your "other code here" updates a UI object, you'll probably want to use the Dispatcher to execute code on the UI thread, like this:
Dispatcher.BeginInvoke(() =>
{
// Code to update the UI
});
try
Dispatcher.BeginInvoke(()=>
{
ConvertDataTabControl.SelectedIndex = 1;
});
The problem is you are doing your work (sleep) on the UI thread. You can use a task/backgroundworker/etc to do the work in an other thread and then do set the ui changes back to the UI thread:
private void Button_Click(object sender, RoutedEventArgs e)
{
Dispatcher callback = Dispatcher.CurrentDispatcher;
ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
{
//Do some work
System.Threading.Thread.Sleep(2000);
//callbackk to ui thread to do ui work. You can also use BeginInvoke...
callback.Invoke(new Action(() => {ConvertDataTabControl.SelectedIndex = 1;}));
//Do some more work
System.Threading.Thread.Sleep(2000);
...
}
}
It is just a example to get the idea.

C#, Updating a Progress Bar Using Background Worker From a Different Class

I am attempting to update the progress bar on a main form with the work being done in a different class. For example:
private void transfer_Click(object sender, EventArgs e)
{
Guid aspnetUserId = Guid.Parse(System.Configuration.ConfigurationSettings.AppSettings["ASPNetUserID"]);
WC1toWC2Transfer transfer = new WC1toWC2Transfer(aspnetUserId);
backgroundWorker1.RunWorkerAsync(transfer);
}
And then in the background method actually call the method:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
WC1toWC2Transfer transfer = e.Argument as WC1toWC2Transfer;
transfer.WC1ToWC2EmployerTransfer(log, wc1ConnStr, wc2ConnStr, progressBar1);
}
In the WC1ToWC2EmployerTransfer method, I'm setting the progress bar maximum and updating the value everytime something is transferred to the database in this method, but when I do this nothing happens. There's code inside the method that runs a stored procedure in a database, but as soon as it hits that portion of the code, it stops debugging and I have to run the process again.
Am I doing something wrong here? Do I need to rewrite what I have so the actual methods are in the main form and not in a different class? I'm a junior developer (just started a few months ago) so forgive me if I'm doing something blatantly wrong or if I didn't explain this well enough.
Any help would be greatly appreciated.
You cant alter the UI unless you are on the main thread, which you BackgroundWorker will not be.
What you need to do is create an event handler in the main form to handle the backgroundworker's ProgressChanged event.
eg
// this method should be in your main form.
private void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// update your progress bar here.
}
In your background worker, you call the ReportProgress method which will fire the ProgressChanged event.
There is a nice example here.
I think the reason is that you get an IllegalCrossThreadException, because you're attempting to access the control from a different thread than it was created. The BackgroundWorker provides a ReportProgress method and a ProgressChanged event that is typically used for such updating and which will be executed on the UI thread. When accessing the progressbar from another thread than the UI thread, do it like this:
if(progressBar1.InvokeRequired) {
progressBar1.Invoke(new MethodInvoker(delegate { progressBar1.Maximum = someValue; }));
}
As a sidenote: It's not very good design to pass the progressbar to your worker class. The form could close, it could get disposed and the worker would not know anything about it, eventually failing with an ObjectDisposedException that probably isn't caught. Additionally, you're making the worker dependent on System.Windows.Forms when it probably doesn't need to. Rather let your worker report progress through an event and pass that on to your progressbar from the class that created the worker.

Popping a MessageBox for the main app with Backgroundworker in WPF

In a WPF app, I am using a BackgroundWorker to periodically check for a condition on the server. While that works fine, I want to pop a MessageBox notifing the users if something fails during the check.
Here's what I have:
public static void StartWorker()
{
worker = new BackgroundWorker();
worker.DoWork += DoSomeWork;
worker.RunWorkerAsync();
}
private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
Thread.Sleep(5000);
var isOkay = CheckCondition();
if(!isOkay)
MessageBox.Show("I should block the main window");
}
}
But this MessageBox does not block the main window. I can still click on my WPF app and change anything I like with the MessageBox around.
How do I solve this? Thanks,
EDIT:
For reference, this is what I ended up doing:
public static void StartWorker()
{
worker = new BackgroundWorker();
worker.DoWork += DoSomeWork;
worker.ProgressChanged += ShowWarning;
worker.RunWorkerAsync();
}
private static void DoSomeWork(object sender, DoWorkEventArgs e)
{
while (!worker.CancellationPending)
{
Thread.Sleep(5000);
var isOkay = CheckCondition();
if(!isOkay)
worker.ReportProgress(1);
}
}
private static void ShowWarning(object sender, ProgressChangedEventArgs e)
{
MessageBox.Show("I block the main window");
}
Replace
MessageBox.Show("I should block the main window");
with
this.Invoke((Func<DialogResult>)(() => MessageBox.Show("I should block the main window")));
that will cause the message box to be on the main thread and will block all access to the UI until it gets a response. As a added bonus this.Invoke will return a object that can be cast in to the DialogResult.
It doesn't only not block the main window, it is also very likely to disappear behind it. That's a direct consequence of it running on a different thread. When you don't specify an owner for the message box then it goes looking for one with the GetActiveWindow() API function. It only considers windows that use the same message queue. Which is a thread-specific property. Losing a message box is quite hard to deal with of course.
Similarly, MessageBox only disables windows that belong to the same message queue. And thus won't block windows created by your main thread.
Fix your problem by letting the UI thread display the message box. Use Dispatcher.Invoke or leverage either the ReportProgress or RunWorkerCompleted events. Doesn't sound like the events are appropriate here.
Call ReportProgress and pass this to MessageBox.Show.
As Stephen and Hans have said, use the ReportProgress event, and pass data to the UI thread. This is especially important if you want to do anything other tha a MessageBox (for isntance, update a control) because the background thread can't do this directly. You'll get a cross-thread exception.
So whatever you need to do (update progress bar, log messages to the UI, etc.), pass data to the UI thread, tell the UI thread what needs to be done, but let the UI thread do it.
I modified it like this and worked fine for me
return Application.Current.Dispatcher.Invoke(() => MessageBox.Show(messageBoxText, caption, button, icon));

Categories