task

Keeping UI responsive during a Thread.Sleep() [duplicate]

丶灬走出姿态 提交于 2019-12-10 15:55:54
问题 This question already has answers here : update UI in Task using TaskScheduler.FromCurrentSynchronizationContext (4 answers) Closed 4 years ago . Background: I am writing an application that iterates through a list of servers and obtains various details about each machine. During the iteration, I need to update various controls on the form (to display a summary of the obtained information). Because it is iterating through a list, I have called a Thread.Sleep() to allow the user time to read

What is equivalent to C# AsyncTask in Android? And how to force stop it?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:34:31
问题 the below routine on C# starts/stops/restarts a function on a different thread: using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { var CancelationToken = new CancellationTokenSource(); // declare and initialize a token for the cancelaion while (SomeCondition) { CancelationToken = new CancellationTokenSource(); // re initialize if wanted to restart Task.Run(() = > Process(), CancelationToken.Token); //start a

task IsCanceled is false, while I canceled

房东的猫 提交于 2019-12-10 15:16:27
问题 When I cancel a Task, the await result still returns true for the IsCanceled Property. Seems something is going wrong. Please advise. This is the code: CancellationTokenSource _cancelLationToken = new CancellationTokenSource(); private async void Button_Click(object sender, EventArgs e) { _cancelLationToken = new CancellationTokenSource(); _cancelLationToken.Token.Register(theCallBack); var myTaskToWaitFor = Task.Factory.StartNew(() => WorkHard(_cancelLationToken.Token), _cancelLationToken

How To create a foreground task?

只愿长相守 提交于 2019-12-10 13:59:05
问题 I seem to fail to create a foreground task. my main thread is supppose to call another thread and then exit. the other thread suppose to run forever void MainThreadMain() { task_main = Task.Factory.StartNew(() => OtherThread()) ; return; } void OtherThread() { while(true) { TellChuckNorrisJoke(); } } how can I ensure task_main will continue running even that Main Thread is dead? I assumed il do: task_main.IsBackgorund = false; but no such option :\ I can make my main thread to wait a signal

Conflict between Log4Net's ThreadContext and Task

柔情痞子 提交于 2019-12-10 13:58:01
问题 Has anyone tried to stack contexts and use Tasks at the same time? I'm trying something like this: using (log4net.ThreadContext.Stacks["contextLog"].Push("Saving Data")) { log.Info("Starting transaction"); var taskList = new List<Task>(); taskList.Add(Task.Factory.StartNew(() => { log.Info("Inside Transaction"); })); Task.WaitAll(taskList.ToArray()); } and I'm getting that result: 2015/42/26 13:42:10,841 INFO [Saving Data] Starting transaction 2015/42/26 13:42:10,870 INFO [(null)] Inside

C++ equivalent of .NET's Task.Delay?

醉酒当歌 提交于 2019-12-10 13:54:42
问题 I'm writing a C++/CX component to be consumed by Window's store Apps. I'm looking for a way to accomplish what Task.Delay(1000) does in C#. 回答1: Old Question, but still unanswered. You can use #include <chrono> #include <thread> std::this_thread::sleep_for(std::chrono::milliseconds(1000)); This will need C++11, which shouldn't be a problem when using C++/CX. 回答2: I'm not going to claim to be a wizard - I'm still fairly new to UWP and C++/CX., but what I'm using is the following: public ref

TaskCanceledException with ContinueWith

て烟熏妆下的殇ゞ 提交于 2019-12-10 13:42:26
问题 I've been trying to figure out why I'm getting a TaskCanceledException for a bit of async code that has recently started misbehaving. I've reduced my issue down to a small code snippet that has me scratching my head: static void Main(string[] args) { RunTest(); } private static void RunTest() { Task.Delay(1000).ContinueWith(t => Console.WriteLine("{0}", t.Exception), TaskContinuationOptions.OnlyOnFaulted).Wait(); } As far as I'm aware, this should simply pause for a second and then close. The

What is the point of .NET 4.6's Task.CompletedTask?

偶尔善良 提交于 2019-12-10 12:38:20
问题 This blog post mentions the new Task APIs, including a new Task.CompletedTask property introduced in .NET 4.6. Why was this added? How is this better than, say, Task.FromResult(whatever) ? 回答1: Task.FromResult(whatever) works for Task<TResult> , but until 4.6 there was nothing for the nongeneric task. You could use FromResult with a dummy value and implicitly cast it to Task , but that somewhat obfuscates the intent (you're not really returning any asynchronous value ) and allocates objects

clearing stack of activities with just one press

夙愿已清 提交于 2019-12-10 12:20:07
问题 I have a launching Activity A1 which has a start button which starts a Service S1: startButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.i(TAG1, "Starting Update Service"); startService(serviceIntentS1); } }); S1 depending on some condition starts Activity A2: if (giveninteger>=2) { Intent intentA2= new Intent(this, A2.class); // following line to avoid exception intentA2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //to avoid exception startActivity

What's the diference between Task.WhenAll() and foreach(var task in tasks)

…衆ロ難τιáo~ 提交于 2019-12-10 11:19:41
问题 After a few hours of struggle I found a bug in my app. I considered the 2 functions below to have identical behavior, but it turned out they don't. Can anyone tell me what's really going on under the hood, and why they behave in a different way? public async Task MyFunction1(IEnumerable<Task> tasks){ await Task.WhenAll(tasks); Console.WriteLine("all done"); // happens AFTER all tasks are finished } public async Task MyFunction2(IEnumerable<Task> tasks){ foreach(var task in tasks){ await task;