im quite the newbie when it comes to htmlelement class!
What i am trying to do right now is to fill a very very boring external web page with loads of data that already exists on the computer (the program is executed locally on said computer). one of those annoying and time consuming things to do manually is to select picture of an equipment installation from your computer and then submit it to that form, doing so, is possible by using the HTMLElement class and webBrowser control, but i hate the unsafe coding required to do so, i call it unsafe because it has race condition issues, here take a look:
The following code was not created by me
async Task PopulateInputFile(HtmlElement file)
{
file.Focus();
var sendKeyTask = Task.Delay(500).ContinueWith((_) =>
{
SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");
}, TaskScheduler.FromCurrentSynchronizationContext());
file.InvokeMember("Click");
await sendKeyTask;
await Task.Delay(500);
}
async Task Populate()
{
var elements = webBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement file in elements)
{
if (file.GetAttribute("name") == "file")
{
file.Focus();
await PopulateInputFile(file);
}
}
}
Ok, now image your computer has the HDD sleeping, or its in a very stressfull antivirus scanning + updating windows at the same time, naturally the modal window to appear can take even up to 10 seconds. what then that means is youre typing the path of the file and pressing enter way before the shown dialog appears which will cause the program to get stuck never leaving that failed to execute line of code, nor presenting the user or the coder with any error (which is expected).
What im trying to do is exactly that but at the same time avoiding that very obvious race condition, i was considering checking each 500ms in a loop to see if window is open and only then execute the sendkeys, something like this:
AutoUploadPicture(string path)
{
fileUploadDialogElement.Focus();
Task.Run(()=>PerformAsyncCheckup());
fileUploadDialogElement.InvokeMember("click");
CancelTask = true;
}
bool CancelTask = false;
PerformAsyncCheckup()
{
while (webBrowser1.AnyDialogFormShowing==false && CancelTask == false)
{
Thread.Sleep(200);
}
if (CancelTask ==false)
SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");
Is what i want possible? ive searched google and stackoverflow and all solutions were based on some random timer which always risks race condition errors.
Best Regards!
What I have tried:
PerformAsyncCheckup()
{
while (webBrowser1.AnyDialogFormShowing==false && CancelTask == false)
{
Thread.Sleep(200);
}
if (CancelTask ==false)
SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");