Method when I click WPF button doesn't work well
This is my XAML piece of 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 installation 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 on the install button, it launches 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 "MessageBox.Show (" Hello World ") method, it works, the value of the run string changes.
But why doesn't this work with my methods?
Why is "installer.InstallProgram2 ()" not working / ending?
My two methods, they are inside the 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
}
source to share
Without a good, minimal, complete code example, it is impossible to know exactly how best to solve your problem. But some tips might help.
First, you write:
It never executes the first line: 'progressBar.Value = 5'.
This is simply not true. The debugger will tell you about this. It is possible that the first statement in your method will be skipped while the rest are executed.
As for the rest of the statements in the method, maybe they work, maybe they don't. Again, without a complete sample code, nothing here on Stack Overflow can even comment on this.
I can tell you that the code you posted will look as if the assigning instructions were progressBar.Value
not executed because you are executing those instructions on the UI thread, preventing the thread from doing any -screen actions until then until the entire method completes.
It is possible that all of this is not the case with your code. If so, changing your method to look more like this needs 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;
}
The above example runs the various "installer" methods in a separate thread. It uses a new (in .NET.NET) async
/ function await
to make communication between the UI thread and tasks easier. All the code in the method itself is still executed on the UI thread, but the anonymous methods (now) that call your "setter" methods are executed using the thread pool.
C # returns from a method Install()
in each expression await
, allowing the UI thread to continue working normally (for example, updating the UI to reflect the new value for ProgressBar
). When each task is completed, control will return to the method Install()
following the instruction await
for that task; this is repeated until the method reaches its normal return point (for example, a statement return
or, as here, the end of the method body).
Beware of common mistakes when executing code on different threads. The code example above does not say anything about "setter" methods cannot be executed on a separate thread, but & hellip; since the code example is far from complete it doesn't say much. If you think there might be problems with concurrency, please reduce your problem example to a good code example (see the link above) that illustrates these problems sufficiently and posts a new question that specifically asks these problems.
source to share