patterncsharpMinor
Looping until browser document is ready
Viewed 0 times
loopinguntilbrowserreadydocument
Problem
I made a
```
private async void DeleteProduct()
{
if (string.IsNullOrEmpty(TextBoxFile.Text))
{
MessageBox.Show(
"Vous n'avez pas entré l'emplacement du fichier!", "Attention", MessageBoxButton.OK,
MessageBoxImage.Exclamation);
}
else
{
MainwebBrowser.Source = "http://www.easyrea.net/cart/detail".ToUri();
do
{
await Task.Delay(500);
} while (!MainwebBrowser.IsDocumentReady);
MainwebBrowser.ExecuteJavascript(
"document.getElementById('login_login').value = '" + Properties.Settings.Default.Username + "';");
MainwebBrowser.ExecuteJavascript(
"document.getElementById('login_password').value = '" + Properties.Settings.Default.Password + "';");
MainwebBrowser.ExecuteJavascript("document.forms['loginForm'].submit()");
do
{
await Task.Delay(500);
} while (!MainwebBrowser.IsDocumentReady);
DataTable dt = LoadXls(dlg.FileName);
progressBar1.Value = 0.0;
foreach (DataRow row in dt.Rows)
{
if (!_checkcancelled)
{
string text = "#product" + row["Référence"];
string cmd =
MainwebBrowser.ExecuteJavascriptWithResult(
string.Format("$('{0}').find('.cmd').text();", text));
string existed =
MainwebBrowser.ExecuteJavascriptWithResult(
string.Format("$('{0}').find('#quantity').text();", text));
if (cmd != "" & existed != "")
{
int intcmd;
bool isintcmd = int.TryParse(Regex.Match(cmd, @"\d+").Value, out intcmd);
int intexisted;
bool
do...while loop in order to wait for the web browser to load completely in order to enter username and password. I don't know whether this impacts the system a lot because I repeated it in at least 4 places. Is there a better solution?```
private async void DeleteProduct()
{
if (string.IsNullOrEmpty(TextBoxFile.Text))
{
MessageBox.Show(
"Vous n'avez pas entré l'emplacement du fichier!", "Attention", MessageBoxButton.OK,
MessageBoxImage.Exclamation);
}
else
{
MainwebBrowser.Source = "http://www.easyrea.net/cart/detail".ToUri();
do
{
await Task.Delay(500);
} while (!MainwebBrowser.IsDocumentReady);
MainwebBrowser.ExecuteJavascript(
"document.getElementById('login_login').value = '" + Properties.Settings.Default.Username + "';");
MainwebBrowser.ExecuteJavascript(
"document.getElementById('login_password').value = '" + Properties.Settings.Default.Password + "';");
MainwebBrowser.ExecuteJavascript("document.forms['loginForm'].submit()");
do
{
await Task.Delay(500);
} while (!MainwebBrowser.IsDocumentReady);
DataTable dt = LoadXls(dlg.FileName);
progressBar1.Value = 0.0;
foreach (DataRow row in dt.Rows)
{
if (!_checkcancelled)
{
string text = "#product" + row["Référence"];
string cmd =
MainwebBrowser.ExecuteJavascriptWithResult(
string.Format("$('{0}').find('.cmd').text();", text));
string existed =
MainwebBrowser.ExecuteJavascriptWithResult(
string.Format("$('{0}').find('#quantity').text();", text));
if (cmd != "" & existed != "")
{
int intcmd;
bool isintcmd = int.TryParse(Regex.Match(cmd, @"\d+").Value, out intcmd);
int intexisted;
bool
Solution
I repeated it in at least 4 places
Seeing the same code multiple times should make you want to refactor it into a separate function, like:
Notice that this function always waits at least 500ms, which is a long time. Consider changing the loop to a regular
You ask whether the while loop "impacts the system a lot".
Other than that, your
Seeing the same code multiple times should make you want to refactor it into a separate function, like:
void async WaitForReady(Browser browser)
{
do
{
await Task.Delay(500);
} while (!browser.IsDocumentReady);
}Notice that this function always waits at least 500ms, which is a long time. Consider changing the loop to a regular
while (to allow the possibility of no delay) and reducing the delay amount. Also, this whole approach assumes the best way to find out when the browser is ready is to keep asking it. Does the browser class raise an event when it becomes ready?You ask whether the while loop "impacts the system a lot".
Task.Delay won't really impact at all, it just stops execution along this path. But a user would definitely notice a 500ms delay in an application's UI, so from that perspective it might impact the system. Presumably testing the IsDocumentReady property is efficient, but again maybe there isn't an alternative anyway.Other than that, your
DeleteProduct function does a lot of different things and would be clearer broken into several functions.Code Snippets
void async WaitForReady(Browser browser)
{
do
{
await Task.Delay(500);
} while (!browser.IsDocumentReady);
}Context
StackExchange Code Review Q#96278, answer score: 2
Revisions (0)
No revisions yet.