Skip to content

Commit ff68054

Browse files
committed
Improve wait logic
1 parent a4405ba commit ff68054

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

ProxyDialogAutoFiller/ProxyDialogAutoFiller.cs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ internal static void WatchDialog(RuntimeContext context)
3131
foreach (var targetDefinition in targetDefinitions)
3232
{
3333
//ブラウザのプロセスのうち、メインウィンドウがあるものに絞り込み
34-
var filterdElements = Process.GetProcessesByName(targetDefinition.ProcessName).Where(_ => _.MainWindowHandle != IntPtr.Zero);
35-
foreach (var filterdElement in filterdElements)
34+
var filteredElements = Process.GetProcessesByName(targetDefinition.ProcessName).Where(_ => _.MainWindowHandle != IntPtr.Zero);
35+
foreach (var filteredElement in filteredElements)
3636
{
37-
var targetPid = filterdElement.Id;
37+
var targetPid = filteredElement.Id;
3838
var windowCondition = new AndCondition(
3939
new PropertyCondition(AutomationElement.ProcessIdProperty, targetPid),
4040
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
@@ -104,7 +104,7 @@ internal static void LoginToProxy(RuntimeContext context, AutomationElement targ
104104
{
105105
return;
106106
}
107-
context.Logger.Log($"Found proxy dialog");
107+
context.Logger.Log($"Found proxy dialog.");
108108
var textTypeDescendants = proxyDialogElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text));
109109

110110
bool isTargetProxy = false;
@@ -173,29 +173,43 @@ internal static void LoginToProxy(RuntimeContext context, AutomationElement targ
173173
{
174174
return;
175175
}
176+
Task.Delay(50).Wait();
176177
loginButton.Invoke();
177-
context.Logger.Log($"Logged in proxy");
178-
178+
context.Logger.Log($"Click login button.");
179179
// プロキシーダイアログが消えていることを確認する。
180-
// 最大10秒待つ。
181-
for (int i = 0; i < 10; i++)
180+
// ユーザー名、パスワードに誤りがあった場合などに、短時間で連続してダイアログの表示とOKが繰り返され、ユーザーが対処できなくなる可能性がある。
181+
// そのため、ダイアログが表示されている場合、ここで15秒間待機し、ダイアログが閉じられなかった場合にユーザーがキャンセルや正しいユーザー名
182+
// パスワードが入力できるようにする。
183+
for (int i = 0; i < 15; i++)
182184
{
183185
proxyDialogElement = targetRootElement.FindFirst(TreeScope.Descendants, proxyDialogCondition);
184186
if (proxyDialogElement == null)
185187
{
186188
break;
187189
}
190+
// ChromeでloginButton.Invoke()の実行までが早すぎて応答しないことがあるので
191+
// ここで一度だけリトライする。
192+
if (i == 0)
193+
{
194+
try
195+
{
196+
Task.Delay(500).Wait();
197+
loginButton.Invoke();
198+
context.Logger.Log($"Retry to click login button.");
199+
}
200+
catch { }
201+
}
188202
Task.Delay(1000).Wait();
189203
}
190204

191205
if (proxyDialogElement != null)
192206
{
193-
context.Logger.Log($"Dialog not closed");
207+
context.Logger.Log($"Dialog not closed. Maybe failed to login to the proxy.");
208+
}
209+
else
210+
{
211+
context.Logger.Log($"Success to login to the proxy.");
194212
}
195-
196-
// ユーザー名、パスワードに誤りがあった場合などに、短時間で連続してダイアログの表示とOKが繰り返され、ユーザーが対処できなくなる可能性がある。
197-
// そのため、ここでさらに10秒間待機し、ダイアログが閉じられなかった場合にユーザーがキャンセルや正しいユーザー名、パスワードが入力できるようにする。
198-
Task.Delay(10000).Wait();
199213
}
200214
catch (Exception ex)
201215
{

0 commit comments

Comments
 (0)