Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking TPL Async calls from mvc controller on click of submit button
    text
    copied!<p>Basically I want to implement simple search functionality, whenever user enters some keyword in the text box on view and clicks submit button I want to make ASYNC calls to predefined website urls using TPL Async mechanism. When I do the same with console application it works like a charm but not with ASP.NET MVC3.</p> <p>I couldn't find the reason</p> <pre><code> public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); } [HttpPost] public ActionResult Index(string text) { string[] url = { "http://www.msnbc.com", "http://www.yahoo.com", "http://www.nytimes.com", "http://www.washingtonpost.com", "http://www.latimes.com", "http://www.newsday.com" }; Task&lt;string[]&gt; webTask = this.GetWordCounts(url, text); string[] results = null; try { results = webTask.Result; } catch (AggregateException e) { } return View("Index", results); } //Taken from MSDN Task&lt;string[]&gt; GetWordCounts(string[] urls, string name) { TaskCompletionSource&lt;string[]&gt; tcs = new TaskCompletionSource&lt;string[]&gt;(); WebClient[] webClients = new WebClient[urls.Length]; object m_lock = new object(); int count = 0; List&lt;string&gt; results = new List&lt;string&gt;(); for (int i = 0; i &lt; urls.Length; i++) { webClients[i] = new WebClient(); #region callback // Specify the callback for the DownloadStringCompleted // event that will be raised by this WebClient instance. webClients[i].DownloadStringCompleted += (obj, args) =&gt; { if (args.Cancelled == true) { tcs.TrySetCanceled(); return; } else if (args.Error != null) { // Pass through to the underlying Task // any exceptions thrown by the WebClient // during the asynchronous operation. tcs.TrySetException(args.Error); return; } else { // Split the string into an array of words, // then count the number of elements that match // the search term. string[] words = null; words = args.Result.Split(' '); string NAME = name.ToUpper(); int nameCount = (from word in words.AsParallel() where word.ToUpper().Contains(NAME) select word) .Count(); // Associate the results with the url, and add new string to the array that // the underlying Task object will return in its Result property. results.Add(String.Format("{0} has {1} instances of {2}", args.UserState, nameCount, name)); } // If this is the last async operation to complete, // then set the Result property on the underlying Task. lock (m_lock) { count++; if (count == urls.Length) { tcs.TrySetResult(results.ToArray()); } } }; #endregion // Call DownloadStringAsync for each URL. Uri address = null; try { address = new Uri(urls[i]); // Pass the address, and also use it for the userToken // to identify the page when the delegate is invoked. webClients[i].DownloadStringAsync(address, address); } catch (UriFormatException ex) { // Abandon the entire operation if one url is malformed. // Other actions are possible here. tcs.TrySetException(ex); return tcs.Task; } } // Return the underlying Task. The client code // waits on the Result property, and handles exceptions // in the try-catch block there. return tcs.Task; } </code></pre> <p>this is my view - for now I have hard coded keyword as microsoft</p> <pre><code>@using (Html.BeginForm("Index", "Home", new { text = "Microsoft" })) { &lt;input type="submit" /&gt; } </code></pre> <p>Update: It stays forever and inside the try block of Index Post method</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