task

Task.IsCancelled doesn't work

大城市里の小女人 提交于 2019-12-24 01:39:09
问题 I've got the following sample code: static class Program { static void Main() { var cts = new CancellationTokenSource(); var task = Task.Factory.StartNew( () => { try { Console.WriteLine("Task: Running"); Thread.Sleep(5000); Console.WriteLine("Task: ThrowIfCancellationRequested"); cts.Token.ThrowIfCancellationRequested(); Thread.Sleep(2000); Console.WriteLine("Task: Completed"); } catch (Exception exception) { Console.WriteLine("Task: " + exception.GetType().Name); throw; } }).ContinueWith(t

Query FireStore against a List of Documents

隐身守侯 提交于 2019-12-24 00:46:13
问题 I have a List<String> of names referring to Documents that I want to retrieve from FireStore. I want to access the contents after they are finished loading so I have implemented an OnCompleteListener in the Fragment that uses the data. However, I am not sure how to run a loop within a Task to query FireStore for each Document. I am querying FireStore in a Repository class that returns a Task object back through my ViewModel and finally to my Fragment . I want the Repository to return a Task

When executing an array of tasks asynchronously, shouldn't it take as long as the longest running task?

自古美人都是妖i 提交于 2019-12-24 00:36:51
问题 I have a list of 10 Tasks, each task takes 15 seconds. All the tasks are in an array and are executed asynchronously. Shouldn't the entire set take about 15 seconds? From the code below, notice that in the output the entire set takes 21 seconds. When I change it to 100 tasks, it takes over a minute. It's almost as if simply creating the task takes a second. What am I missing? Thanks! static void Main(string[] args) { var mainStart = DateTime.Now; var tasks = new List<Task>(); for (int i = 0;

Problems with show dialog on Background task in windows phone 8.1

淺唱寂寞╮ 提交于 2019-12-24 00:18:39
问题 I have this code for execute httpwebrequest and response in background method and i just want show dialog for information when download zip crashed and my code enter in this catch... private void DoSincroFit() { HttpWebRequest request = HttpWebRequest.CreateHttp(url); request.BeginGetResponse(new AsyncCallback(playResponseAsync), request); } public async void playResponseAsync(IAsyncResult asyncResult) { //Declaration of variables HttpWebRequest webRequest = (HttpWebRequest)asyncResult

Using Task.Wait instead of await for async programming

﹥>﹥吖頭↗ 提交于 2019-12-23 23:11:58
问题 The .Net article on Tasks shows two following two code snippets, one using await and the other using Task.Wait and says both are "functionally equivalent". Isn't that technically incorrect then? Can someone please clarify? Also if Tasks are supposed to be asynchronous and form the basis for Asynchronous Programming (TPL), why would ASP.Net allow a synchronous Wait on them anyway? Doesn't that kind of violate their main utility? using System; using System.Threading.Tasks; public class Example

Why C# Parallel.Invoke is slow?

て烟熏妆下的殇ゞ 提交于 2019-12-23 20:14:40
问题 I am doing this: private static void Main(string[] args) { var dict1 = new Dictionary<int, string>(); var dict2 = new Dictionary<int, string>(); DateTime t1 = DateTime.Now; for (int i = 1; i < 1000000; i++) { Parallel.Invoke( () => dict1.Add(i, "Test" + i), () => dict2.Add(i, "Test" + i) ); } TimeSpan t2 = DateTime.Now.Subtract(t1); Console.WriteLine(t2.TotalMilliseconds); Console.ReadLine(); } So doing a for loop 1 million time and adding items to two different dictionaries. The problem is

async task progress dialog show too late

人盡茶涼 提交于 2019-12-23 19:04:34
问题 progress dialog appear to, late probably after async task is finished,in doInBackground it calls a web service and parse an xml,the activity have to wait for some seconds if in xml is a bigger file @Override protected void onPreExecute(){ super.onPreExecute(); completed=false; this.progressDialog.show(); } @Override protected Boolean doInBackground(Integer... params) { t=HttpHelper.callWebService( url, soapAction,xml); if (t.equals("")){ return false; } else { try { SAXParserFactory spf =

Detect errors with NetworkStream.WriteAsync

拥有回忆 提交于 2019-12-23 19:03:14
问题 If I kill my server after the call to Login is complete, no exception is thrown when the call to stream.WriteAsync(data, 0, data.Count()); is made and there is no indication of error in the returned Task. How then, am I supposed to detect errors? Surely, there should be some indication that I tried to send data over a connection that had been hung up on. Here is my latest code attempt: using System; using System.Collections.Generic; using System.Linq; using System.Net.Sockets; using System

Spring Task Async and Delayed

↘锁芯ラ 提交于 2019-12-23 16:28:19
问题 I need to do onething that I don't know wich is the best practice to this. After I send one request to an especific service, this one returns OK and queue my request. I have a callback service that is used to notify when it ends. The problem is that the whole process can take a long time and over without notify anything and after that I need to consider a timeout. The application is SpringBoot APP and I was considering to use the annotations @EnableAsync and @Async on a service method with a

Why does TaskFactory.StartNew receive a CancellationToken [duplicate]

怎甘沉沦 提交于 2019-12-23 13:08:20
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Cancellation token in Task constructor: why? This method receives a CancellationToken: CancellationTokenSource cts = new CancellationTokenSource(4); var t = Task.Factory.StartNew(() => { // code }, cts.Token); Since cancellation is cooperative (the actual working code needs to observe the cancellation token), What is the purpose of passing this to the StartNew method as an argument? 回答1: It allows the task