Improve the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#)
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 5.1k
- Avg merge
- 9h 11m
- Merged PRs (30d)
- 1
Description
## Improve the code for ShowAutoClosingTaskDialog in TaskDialogDemo Form1 (vb & c#)
In the example of ShowAutoClosingTaskDialog it only works well with 5 seconds or less, to make that example easier to understand or if more than 5 seconds of waiting are used, a couple of code changes would have to be made in both Form1.cs as in Form1.vb.
### Issue:
The sample code fails when the result of **remainingTenthSeconds * 2** is greater than 100.
The sample code fails when a value of more than 5 seconds is used, since when assigning the value of **remainingTenthSeconds = _seconds * 10_**, the maximum value of the progress bar turns out to be less than the resulting value of: **remainingTenthSeconds * 2**.
The actual code is:
#### For C#
```
page.ProgressBar.Value = 100 - remainingTenthSeconds * 2;
```
#### For VB:
```
page.ProgressBar.Value = 100 - remainingTenthSeconds * 2
```
### Solution:
The new update code should be something like this:
#### For C#:
```
// Set then maximum value for the ProgressBar
var maximum = remainingTenthSeconds * 2;
page.ProgressBar.Maximum = maximum;
// Create a WinForms timer that raises the Tick event every tenth second.
using (var timer = new Timer()
{
Enabled = true,
Interval = 100
})
{
timer.Tick += (s, e) =>
{
remainingTenthSeconds--;
if (remainingTenthSeconds > 0)
{
// Update the remaining time and progress bar.
page.Text = string.Format(textFormat, (remainingTenthSeconds + 9) / 10);
page.ProgressBar.Value = maximum - remainingTenthSeconds * 2;
}
else
{
// Stop the timer and click the "Reconnect" button - this will
// close the dialog.
timer.Enabled = false;
reconnectButton.PerformClick();
}
};
TaskDialogButton result = TaskDialog.ShowDialog(this, page);
if (result == reconnectButton)
Console.WriteLine("Reconnecting.");
else
Console.WriteLine("Not reconnecting.");
}
```
#### For VB:
```
' Set then maximum value for the ProgressBar
Dim maximum = remainingTenthSeconds * 2
page.ProgressBar.Maximum = maximum
' Create a WinForms timer that raises the Tick event every tenth second.
Using timer = New Windows.Forms.Timer() With {
.Enabled = True,
.Interval = 100
}
AddHandler timer.Tick,
Sub(s, e)
remainingTenthSeconds -= 1
If remainingTenthSeconds > 0 Then
' Update the remaining time and progress bar.
page.Text = String.Format(textFormat, (remainingTenthSeconds + 9) \ 10)
page.ProgressBar.Value = maximum - remainingTenthSeconds * 2
Else
' Stop the timer and click the "Reconnect" button - this will
' close the dialog.
timer.Enabled = False
reconnectButton.PerformClick()
End If
End Sub
Dim result As TaskDialogButton = TaskDialog.ShowDialog(Me, page)
If result = reconnectButton Then
Console.WriteLine("Reconnecting.")
Else
Console.WriteLine("Not reconnecting.")
End If
End Using
```
Hope this helps.
Guillermo
---
#### Issue metadata
* Issue type: sample-update
Contributor guide
Assessment
This issue has not been assessed yet.