Reputation: 259
This is my XAML part code:
<DockPanel Margin="1 5 1 0" Height="25">
<Button DockPanel.Dock="Right" Width="70" Margin="6 0 0 0" Content="Install" Click="Install" />
<Button DockPanel.Dock="Right" Width="70" Margin="6 0 0 0" Content="Uninstall"/>
<ProgressBar Name="progressBar"/>
</DockPanel>
And this is the Install method:
private void Install(object sender, RoutedEventArgs e)
{
progressBar.Value = 5;
installer.InstallProgram1();
progressBar.Value = 25;
installer.InstallProgram2();
progressBar.Value = 50;
installer.InstallProgram3();
progressBar.Value = 75;
installer.InstallProgram4();
progressBar.Value = 100;
}
When I click the install button, it runs the Install method but not correctly.
- It never executes the first line: 'progressBar.Value = 5'.
- The second line works well.
- And after nothing works.
I tried to replace my method with 'MessageBox.Show("Hello World")', it works, the progress bar value changes.
But why it doesn't work with my methods?
Why 'installer.InstallProgram2()' doesn't work/finish?
My two methods, they are inside a Installer.cs file:
public void InstallProgram1()
{
// Download the lavfilters executable.
var url = "http://www.videohelp.com/software/LAV-Filters";
var selector = "a.linktool:nth-child(12)";
var filename = downloader.DownloadFromVideoHelp(url, selector);
// TODO: Installation
}
public void InstallProgram2()
{
// Download the madVR archive
var url = "http://www.videohelp.com/software/madVR";
var selector = ".linktool";
var filename = downloader.DownloadFromVideoHelp(url, selector);
// TODO: Installation
}
Upvotes: 4
Views: 480
Reputation: 70691
Without a good, minimal, complete code example it is not possible to know for sure the best way to address your problem. But some advice can be given to help.
First, you write:
It never executes the first line: 'progressBar.Value = 5'.
That is simply false. A debugger would tell you as much. It is just not possible that the first statement in your method would be skipped while the remained are executed.
As for the remainder of the statements in the method, maybe they work, maybe they don't. Again, lacking a complete code example, there's nothing anyone here at Stack Overflow can do to even comment on that.
What I can tell you is that the code you posted is going to appear as though the statements assigning progressBar.Value
aren't being executed, because you are executing those statements in the UI thread, preventing that thread from doing any on-screen updates until the entire method has completed.
It is possible that is all that is wrong with your code. If so, changing your method so that it looks more like this one should help:
private async void Install(object sender, RoutedEventArgs e)
{
progressBar.Value = 5;
await Task.Run(() => installer.InstallProgram1());
progressBar.Value = 25;
await Task.Run(() => installer.InstallProgram2());
progressBar.Value = 50;
await Task.Run(() => installer.InstallProgram3());
progressBar.Value = 75;
await Task.Run(() => installer.InstallProgram4());
progressBar.Value = 100;
}
What the above does is run your various "installer" methods in a separate thread. It uses the new (as of .NET 4.5) async
/await
feature to simplify the interaction between the UI thread and the tasks. All of the code in the method itself is still executed in the UI thread, but the (now) anonymous methods that call your "installer" methods are executed using the thread pool.
C# returns from the Install()
method at each await
statement, allowing the UI thread to continue operating normally (e.g. updating the UI to reflect the new value for the ProgressBar
). When each task is complete, control will return to the Install()
method after the await
statement for that task; this is repeated until the method reaches its normal return point (e.g. a return
statement or, as here, the end of the method body).
Do beware the usual pitfalls of executing code in different threads. There's nothing in the code example you posted that would suggest the "installer" methods can't be run in a separate thread, but…since the code example is far from complete, that's not really saying much. If you feel that there may be concurrency issues to address, please reduce your problem example to a good code example (see the link I provided above) that sufficiently illustrates those problems, and post a new question asking about those issues specifically.
Upvotes: 5