Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Appium Wizard/CommonProgress.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion Appium Wizard/CommonProgress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,46 @@ public CommonProgress()
commonProgressLabel.MaximumSize = new Size(availableWidth, 0);
}

public bool IsCancelled { get; private set; }

private readonly TaskCompletionSource cancelRequested = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);

// Completes when the user clicks Cancel.
public Task CancelRequested => cancelRequested.Task;

// Awaits work, but returns false if the user cancels. After Cancel, the running work gets
// graceMs to finish; if it doesn't, it is abandoned (it keeps running in the background).
public async Task<bool> RunCancellable(Task work, int graceMs = 10000)
{
if (await Task.WhenAny(work, CancelRequested) == work)
{
await work;
return true;
}
await Task.WhenAny(work, Task.Delay(graceMs));
return false;
}

public void EnableCancel()
{
cancelButton.Visible = true;
}

private void cancelButton_Click(object sender, EventArgs e)
{
IsCancelled = true;
cancelButton.Enabled = false;
progressBar1.Style = ProgressBarStyle.Marquee;
commonProgressLabel.ForeColor = Color.Black;
commonProgressLabel.Text = "Cancelling... Please wait for the current step to finish.";
commonProgressLabel.Refresh();
cancelRequested.TrySetResult();
}

public void UpdateStepLabel(string title, string stepText, int progressPercent = 50)
{
Logger.Debug(stepText);
if (!this.Visible)
if (!this.Visible || IsCancelled)
{
return;
}
Expand Down Expand Up @@ -61,6 +97,10 @@ public void UpdateStepLabel(string title, string stepText, int progressPercent =
public void UpdateStepLabel(string stepText)
{
Logger.Debug(stepText);
if (IsCancelled)
{
return;
}
if (this.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
Expand Down
42 changes: 32 additions & 10 deletions Appium Wizard/MainScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -902,16 +902,21 @@ private async void iOSToolStripMenuItem_Click(object sender, EventArgs e)
{
CommonProgress commonProgress = new CommonProgress();
commonProgress.Owner = this;
commonProgress.EnableCancel();
commonProgress.Show();
commonProgress.UpdateStepLabel("Detect iOS Device", "Looking for iOS device...", 10);
List<string> deviceList = new List<string>();
Dictionary<string, object> deviceInfo = new Dictionary<string, object>();
try
{
await Task.Run(() =>
if (!await commonProgress.RunCancellable(Task.Run(() =>
{
deviceList = iOSMethods.GetInstance().GetListOfDevicesUDID();
});
})))
{
commonProgress.Close();
return;
}

if (deviceList.Contains("ITunes not installed"))
{
Expand Down Expand Up @@ -956,19 +961,27 @@ await Task.Run(() =>
if (!isDeviceAlreadyAdded(deviceList[i]))
{
DeviceInformation deviceInformation = new DeviceInformation(main);
await Task.Run(() =>
if (!await commonProgress.RunCancellable(Task.Run(() =>
{
deviceInfo = iOSMethods.GetInstance().GetDeviceInformation(deviceList[i]);
});
})))
{
commonProgress.Close();
return;
}
commonProgress.UpdateStepLabel("Detect iOS Device", "Getting iOS device information...", 70);
if (deviceInfo.Count > 0)
{
try
{
await Task.Run(() =>
if (!await commonProgress.RunCancellable(Task.Run(() =>
{
Model = iOSMethods.GetInstance().GetDeviceModel(deviceInfo["ProductType"]?.ToString() ?? "");
});
})))
{
commonProgress.Close();
return;
}
}
catch (Exception)
{
Expand Down Expand Up @@ -1039,16 +1052,21 @@ private async void androidToolStripMenuItem_Click(object sender, EventArgs e)
{
CommonProgress commonProgress = new CommonProgress();
commonProgress.Owner = this;
commonProgress.EnableCancel();
commonProgress.Show();
commonProgress.UpdateStepLabel("Detect Android Device", "Looking for Android device...", 10);
List<string> deviceList = new List<string>();
Dictionary<string, string> deviceInfo = new Dictionary<string, string>();
try
{
await Task.Run(() =>
if (!await commonProgress.RunCancellable(Task.Run(() =>
{
deviceList = AndroidMethods.GetInstance().GetListOfDevicesUDID();
});
})))
{
commonProgress.Close();
return;
}
int count = deviceList.Count;
if (count == 0)
{
Expand All @@ -1063,10 +1081,14 @@ await Task.Run(() =>
if (!isDeviceAlreadyAdded(deviceList[i]))
{
DeviceInformation deviceInformation = new DeviceInformation(main);
await Task.Run(() =>
if (!await commonProgress.RunCancellable(Task.Run(() =>
{
deviceInfo = AndroidAsyncMethods.GetInstance().GetDeviceInformation(deviceList[i]);
});
})))
{
commonProgress.Close();
return;
}
if (deviceInfo.Count > 0)
{
commonProgress.UpdateStepLabel("Detect Android Device", "Getting Android device information...", 75);
Expand Down
Loading
Loading