Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you update ASP.NET web forms from different threads?
    text
    copied!<p>In the last few days I've been trying to learn how to use ASP.NET Web Forms together with multithreading the hard way by building a simple applet using both and I've been struggling with aspects of interactions between different threads and the UI.</p> <p>I've resolved some multithreading issues in some other questions (and also learned after waaaaaay too long that web forms and WPF are not the same thing) but now I'm running into trouble finding the best way to update UI elements based on data acquired in multiple threads.</p> <p>Here's my code:</p> <p><strong>Default.aspx</strong></p> <pre><code>public partial class _Default : System.Web.UI.Page { private NlSearch _search; private static int _counter = 0; private static SortedList&lt;long, SearchResult&gt; resultsList = new SortedList&lt;long, SearchResult&gt;(); protected void Page_Load(object sender, EventArgs e) { _search = new NlSearch(); } protected void AddSearchMethod(object sender, EventArgs e) { var text = SearchForm.Text; new Task(() =&gt; MakeRequest(text)); } protected void UpdateMethod(object sender, EventArgs e) { resultsLabel.Text = ""; foreach (var v in resultsList.Values) { resultsLabel.Text += v.SearchTerm + ": " + v.Count + " occurances&lt;br/&gt;"; } } protected void ClearSearchMethod(object sender, EventArgs e) { resultsLabel.Text = ""; resultsList.Clear(); } protected void MakeRequest(string text) { _counter++; SearchResult s = new SearchResult { SearchTerm = text, Count = _search.MakeRequests(text) }; resultsList.Add(_counter, s); } } </code></pre> <p>I've tried quite a few versions of the same basic thing. NlSearch.MakeRequest (called by MakeRequests) sends an HTTP POST request to an outside web site imitating a search bar input, and then extracts an integer from the markup indicating how many results came back.</p> <p>The current simple UI revolves around a SearchForm textfield, an "Add Search" button, an "Update Label" button a "Clear Search" method, and a ResultsLabel that displays results. The AddSearch button creates a new task that calls MakeRequest, which calls the method to send the HTTP request and then stores the results in the order they were sent in a static sorted list.</p> <p>So now ideally in a good UI I would like to just update the label every time a thread returns, however I've tried using ContinueWhenAll and a few other task functions and the problem seems to be that other threads do not have the ability to change the UI. </p> <p>I have also tried running a new thread on page load that updates the label every few seconds, but this likewise failed. </p> <p>Because I haven't been able to implement this correctly, I've had to use the "Update Label" button which literally just tells the label to display what's currently in the static list. I would really like to get rid of this button but I can't figuer out how to get my threads to make UI changes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload