I have some code trying to set the value of a progress bar in WPF using a method like this:
private void SetPercentage(int channel, int percentage)
{
switch(channel)
{
case 1:
ch1Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch1Bar.Value = (double)percentage));
break;
case 2:
ch2Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch2Bar.Value = (double)percentage));
break;
case 3:
ch3Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch3Bar.Value = (double)percentage));
break;
case 4:
ch4Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch4Bar.Value = (double)percentage));
break;
case 5:
ch5Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch5Bar.Value = (double)percentage));
break;
case 6:
ch6Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch6Bar.Value = (double)percentage));
break;
case 7:
ch7Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch7Bar.Value = (double)percentage));
break;
case 8:
ch8Bar.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new DelegateMethod(() => ch8Bar.Value = (double)percentage));
break;
}
}
Every chXBar
where X is 1 through 8 is a separate progress bar. This method is being called in a loop within another thread (created manually with the Thread
class). The loop sets one channel value at a time, and quite slowly (with a Thread.Sleep
to slow it down).
However none of these invokes seem to work; the values on the progress bars don't change (they always stay zero). The code compiles fine and there are no exceptions thrown in debugging. I really don't want to write 8 delegates and 8 methods to use them.
Does anyone have any pointers?
(P.S. I'm using .NET 4.5 on Windows 7 x64)