I have a simple ASP.NET applet, and I'm trying to learn basic multithreading to make it run more smoothly.
Let's say I have a button hooked up to the following method:
private NlSearch search;
protected void SearchButtonClick(object sender, EventArgs e)
{
int resultCount = search.MakeRequests(SearchForm.Text);
resultsLabel.Text += SearchForm.Text + ": " + resultCount.ToString() + " occurances";
}
All MakeRequests does is issue a POST request to do a search on a 3rd party website and grabs the number of results from the response. The search term along with the number of results is then added to the page.
However if I were to do something like submit a new search before the first search has responded, it will simply interrupt the original search and only the most recent search will write it's results to the label.
I understand there's a way to do this with C# threading, but I can't figure out how to use tasks correctly to do this. I tried putting the body of the method into a second method with no arguments that I then used to create and run a task, but this did not change the behavior so I think I was using tasks incorrectly.
Any help or advice would be appreciated, thanks.
EDIT: What I've tried so far: both answers seemed to 'work' at retrieving values but for some reason neither actually added them to the label for some reason. For example
protected void NewSearchClick(object sender, EventArgs e)
{
new Thread(() => MakeRequest(SearchForm.Text)).Start();
}
and
protected void MakeRequest(string text)
{
int resultCount = search.MakeRequests(text);
if (resultsLabel != null) resultsLabel.Text += text + ": " + resultCount + " occurances";
}
seemed to retrieve the value but the label does not change.