async Task PopulateInputFile(HtmlElement file, String photo) { file.Focus(); // delay the execution of SendKey to let the Choose File dialog show up var sendKeyTask = TaskEx.Delay(500).ContinueWith((_) => { // this gets executed when the dialog is visible SendKeys.SendWait(photo + "{ENTER}"); }, TaskScheduler.FromCurrentSynchronizationContext()); file.InvokeMember("Click"); // this shows up the dialog await sendKeyTask; file.RemoveFocus(); // delay continuation to let the Choose File dialog hide await TaskEx.Delay(500); }