How to update UI from a new thread vb.NET website - vb.net

My application works like this:
Upload Excel file + convert to DataTable
Start new thread
Begin loop through DataTable
Update UI (Label) to show "Processing row [i] of [n]"
Next
End loop
The bold is what I'm not able to do. I've looked around online for updating UI elements from worker threads, but all the results I can seem to find are for Windows Forms, rather than a web project. Is this possible?

yes, you can do it, and actually it is not difficult. you can use ajax toolbox to do it easily. simply use an updatepanel, and update progress.
check http://ajaxcontroltoolkit.codeplex.com/
an example: http://www.asp.net/ajax/documentation/live/overview/updateprogressoverview.aspx

I found a workaround using jQuery AJAX and asp.NET WebMethods and a session variable.
I used a method from one of my previous questions, by having a WebMethod check on a Session variable that was updated by the worker thread.
Worker thread:
Session["progress"] = "{\"current\":" + (i + 1) + ", \"total\":" + dt.Rows.Count + "}"
WebMethod:
[WebMethod]
public static string GetProgress()
if (HttpContext.Current.Session["progress"] == null) {
return "{\"current\":1,\"total\":1}";
} else {
return HttpContext.Current.Session["progress"];
}
}
my jQuery basically looped calling that AJAX WebMethod every second. It would start on page load and if the current = total then it would display "Completed" and clear the loop, otherwise it shows "Processing row [current] of [total]". I even added a jQuery UI Progressbar
This is kind of a manual solution but it solves my problem, with little overhead. An unexpected but nice piece is that since it is utilizing a Session variable, and the WebMethod checks on page load, if the worker thread is active then the progressbar will show even if you navigate away and come back to the page.

Related

Unpredictable behaviour with Selenium and jUnit

I am working on a website and trying to test it with Selenium and jUnit. I'm getting race conditions between the test and the site, despite my best efforts.
The front end of the site is HTML and jQuery. The back end (via AJAX) is PHP.
The site
I have two required text input fields (year and age), plus some others that I'm not changing in the tests that give problems. As soon as both text inputs are non-empty, an AJAX call is made to the back end. This will return 0+ results. If 0 results are returned, a results div on the screen gets some text saying that there were no results. If >0 results are returned, a table is written to the results div showing the results.
I don't want the site to wait until e.g. 4 digits' worth of year is entered before doing the AJAX call as it could be looking at ancient history (yes, really). So, as soon as both are non-empty the call should be made. If you type slowly, this means that entering e.g. 2015 will trigger calls for year=2, year=20, year=201 and year=2015. (This is OK.)
The test
I'm using page objects - one for the inputs and one for the output. At the start of the test, I wait for a prompt to be present on the screen (please enter some data) as that is generated by JavaScript that checks the state of the input fields - so I know that the page has loaded and JavaScript has run.
The wait for a prompt is made immediately after the page object is created for the output. This is the relevant method in the page object:
// Wait until the prompt / help text is displayed. Assumes that the prompt text always contains the word "Please"
public void waitForText() {
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("resultContainer"), "Please"));
}
The method for setting the year is
public void setYear(String year){
WebElement yearField = driver.findElement(By.id(yearInputId));
if (yearField == null) {
// This should never happen
Assert.fail("Can't find year input field using id " + yearInputId);
} else {
yearField.sendKeys(new String [] {year});
driver.findElement(By.id(ageInputId)).click(); // click somewhere else
}
}
and there's a corresponding one for age.
I have a series of methods that wait for things to happen, which don't seem to have prevented the problem (below). These do things like wait for the current result values to be different from a previous snapshot of them, wait for a certain number of results to be returned etc.
I create a driver for Chrome as follows:
import org.openqa.selenium.chrome.ChromeDriver;
// ...
case CHROME: {
System.setProperty("webdriver.chrome.driver", "C:\\path\\chromedriver.exe");
result = new ChromeDriver();
break;
}
The problem
Some of the time, things work OK. Some of the time, both inputs are filled in with sensible values by the test, but the "there are 0 results" message is displayed. Some of the time, the test hangs part-way through filling in the inputs. It seems to be fine when I'm testing with Firefox, but Chrome often fails.
The fact that there is unpredictable behaviour suggests that I'm not controlling all the things I need to (and / or my attempts to control things are wrong). I can't see that I'm doing anything particularly weird, so someone must have hit these kinds of issue before.
Is there a browser issue I'm not addressing?
Is there something I'm doing wrong in setting the values?
Is there something I'm doing wrong in my test choreography?
It could be that when you start typing, the script is still loading or that there's a pending Ajax call when you start handling the next field or validation.
You could try to synchronize the calls with a low level script :
const String JS_WAIT_NO_AJAX =
"var callback = arguments[0]; (function fn(){ " +
" if(window.$ && window.$.active == 0) " +
" return callback(); " +
" setTimeout(fn, 60); " +
"})();";
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
js.executeAsyncScript(JS_WAIT_NO_AJAX);
driver.findElement(By.Id("...")).sendKeys("...");
js.executeAsyncScript(JS_WAIT_NO_AJAX);
driver.findElement(By.Id("...")).click();

How would I implement multithreading in this situation? Is it possible?

I have a form with a listview in it that the user can add tasks too. Then the user can click a button then the application goes through each task in the listview 1 by 1 an executes it.
These tasks are more like instructions that actually complete tasks.I do this by having a class with a loop in it that goes through each item and it then completes a task I set for each item(instruction). In order to start the parsing I have a button on a form that calls that function. IE: RunTask(listview1, 1) - basically all this does it starts the loop I have in my class , with a specified listview and which item to start on.
Everything works perfect except the screen locks up, so I cannot implement a stop feature to stop the application from parsing these listview items. I just don't understand how I can implement this without crossthreading, since the thread that I would like to run seperate will always access this listview. It is not feasable to redesign the program to get rid of the listview. I tried application.doevents although it caused way too man bugs. I have been researching for days on how to fix this but I have NO idea. Hopefully someone cans hed some light.
Also I had already added a background worker to solve the issue, although I had to obviously set checkforillegalcrossthreadcalls = false and I know this isn't smart.
Try doing something like this. Take you list view and turn it into a set of values that aren't UI related. Like this:
string[] values =
this
.listView1
.Items
.Cast<ListViewItem>()
.Select(x => x.Text)
.ToArray();
Then you can use the parallel task library to run your task in the background:
var tokenSource = new System.Threading.CancellationTokenSource();
var token = tokenSource.Token;
var task = System.Threading.Tasks.Task.Factory
.StartNew(() => RunTasks(values, 1), token);
If you need to cancel the task you can do this:
tokenSource.Cancel();
But to handle UI updates when the task is finished do this:
task.ContinueWith(r =>
{
/* When compete code */
});
Make sure that you invoke the UI updates so that they go on the UI thread.
My apologies that I didn't write this in VB.NET. My VB is getting rusty.

How to remember variables with Greasemonkey script when a page reloads

Ive got an problem currently on an mobile site that i'm running directly in my pc's firefox browser. Everytime a button is clicked, the page reloads, thus resetting my variables. I've got this script:
// ==UserScript==
// #name trada.net autoclick 55_1min_mobile
// #namespace airtimeauction auto click
// #include http://www.trada.net/Mobile/
// #version 0.1
// #description Automatically click // ==/UserScript==
var interval = 57000;
var bidClickTimer = setInterval (function() {BidClick (); }, interval);
var numBidClicks = 0;
function BidClick ()
{var bidBtn1=document.getElementById("ctl00_mainContentPlaceholder_AirtimeAuctionItem7_btn_bidNow");
numBidClicks++;
if (numBidClicks > 500)
{
clearInterval (bidClickTimer);
bidClickTimer = "";
}
else
{
bidBtn1.click (1);
}
};
BidClick();
It should click the button every 57 seconds, but the moment it clicks the button, the page reloads, thus resetting the variables. How can i get greasemonkey to "remember" or carry over the variables to the next page/script when it reloads? Will it have something to do with GM_setValue? It will only be this few variables, but the second problem or question wil be, will it subtract the few seconds it takes the page to reload from the "57" seconds? How do i compensate for that?
In addition to GM_setValue...
you also can use the new Javascript "localStorage" object, or a SQL Javascript API.
The advantage of the SQL approach is it is very meager in its resource consumption in a script (think about it; rather than concatenating a humongous string of results, you can tuck away each result and recall it if needed with a precise query. The downside is you have to set up a SQL server, but using something like SQLite that's not a big deal these days. Even postgres or mysql can be quickly spun on a laptop...
Yes, I think you have to use GM_setValue/GM_getValue.
And if you have to do something exactly every 57 seconds, then calculate the time when the next action should take place after the reload, and store it using GM_setValue.
When your script starts, read first if the next action is stored, if it is, use that time to schedule the next action, and calculate the time for the action after that, and so on...
GM.setValue will set a value indefinitely and is scoped to the script, but will work if your script runs across multiple domains.
window.localStorage will set a value indefinitely and is scoped to the domain of the page, so will not work across domains, but will work if you need several GreaseMonkey scripts to access the same value.
window.sessionStorage will set a value only while the window or tab is open and is scoped to only that window or tab for that domain.
document.cookie can set a value indefinitely or only while the browser is open, and can be scoped across subdomains, or a single domain, or a path, or a single page.
Those are the main client-side mechanisms for storing values across page loads that are intended for this purpose. However, there is another method which is sometimes possible (if the page itself is not using it), and can also be quite useful; window.name.
window.name is scoped to the window or tab, but will work across domains too. If you need to store several values, then they can be put into an object and you can store the object's JSON string. E.g. window.name = JSON.stringify(obj)

How can I send different SMS' to multiple recipients in a loop

I'm using Symbian C++ to create my code, I'm using S60 5th Ed SDK
I want to know how to send different messages - Their body text not the same - to multiple recipients in a for-loop ?
I've tried the example below, but when I try to use it in a loop it crashes due to ActiveObjects properties, as I should wait to AO to finish before calling it again.
Sending_SMS_in_S60_3rd_Edition_MTM
Below is example of what I need to do:
SendSMSL(); // **I call this function once to start the process**
// **iRecepients is a CDesCArray contains phone numbers**
// ** iSMSBody is a CDesCArray contains each contact SMS body text**
void CSMS::SendSMSL()
{
if(iRecepients->Count() >= 1)
{
TInt x = iRecepients->Count()-1;
TInt y = iSMSBody->Count()-1;
// **If the sms validating and scheduling succeeded then delete last item from both arrays**
if(iSMSHandler->SendL((*iRecepients)[x],(*iSMSBody)[y])
{
iRecepients->Delete(x);
iSMSBody->Delete(y);
}
}
}
Now, in the code above I call iSMSHandler->SendL() which send sms using AO, and in iSMSHandler object RunL() function, I call back the function above CSMS::SendSMSL() , which in turn checks if there is still anymore iRecepients elements and then call again iSMSHandler->SendL() AO , and keeps this way till no more iRecepients.
Looking forward to hear your feedback on the modification above.
Many thanks in advance.
The link you posted doesn't work for me so I can't see the rest of the code.
Assuming that iSmsHandler is a class that uses active objects to send SMS messages,
I see several issues with your loop.
1) You need to wait for the first asynchronous SendL to complete before you can issue the next SendL
2) The buf variable can not go out of scope until the SendL completes. (This may be the reason for your crash)
I suggest that you keep the textbuffer somewhere else, like together with iSmsHandler, and then code the active object that is called when SendL completes to issue the next SendL.
All of this is guesses since I have no idea what class iSmsHandler is....

How to Implement callback for file downloading?

I wrote a script that downloads file from web using file URL. I have an ActiveXObject of following type.
var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
objHTTP.open("GET", strFileURL, false);
It works perfect for small size file says, file size less than 100MB. But when I try to download file with size greater than 100MB my script hanged. Then I tried,
objHTTP.open("GET", strFileURL, true);
but in this case we have to implement a callback function. I don't know how to implement callback and then use it. Can somebody help me. I am using TestComplete 7. Script that I wrote;
var objHTTP = new ActiveXObject("MSXML2.XMLHTTP");
objHTTP.open("GET", strFileURL, true);
objHTTP.onreadystatechange = Callback;
objHTTP.send();
while((objHTTP.readyState != 4) && (objHTTP.readyState != 'complete'))
{
Delay(100);
}
if(200 != objHTTP.Status)
{
Log.Error("The " + strFileURL + " file was not found." + " The returned status is " + objHTTP.Status);
return;
}
I don't know how to implement Callback function. Can somebody provide me implementation?
Thanks
Probably, the hanging is the result of the while loop waiting for a specific value of the readyState property. If the property never gets one of the expected values, the script will work forever.
I think the MSXML2.XMLHTTP object fails to load the large file, and never sets the readyState to one of the values your script expects. To understand what exactly is happening, I would check what value the property has after very long time, which is enough either for the file to load, or for the attempt to fail (say, 2 hours). If you know what value the readyState property has when the downloading fails, you can handle it in the script to avoid hanging.
That's it about the hanging itself. Now about the cause of the file downloading problem. I have found a page that tells about the problem and suggests setting higher timeouts - take a look:
http://edgylogic.com/blog/downloading-large-files-vbscript/
The example is in VBScript, but it should be easy to implement the same approach with JScript. Please note that the example uses a different COM object - ServerXMLHTTP. You can read about it (including differences from XMLHTTP) here:
http://msdn.microsoft.com/en-us/library/ms762278(v=VS.85).aspx
I hope this helps.