c# task run in background
2017-02-23 15:36
507 查看
1. Task.Run will start to run in background
2. Don't use breakpoint to debug, or all tasks will be interupted.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SystemTasks
{
class TestTask
{
public static async Task<int> A(int upper)
{
for (int i = 0; i < upper; ++i)
{
await Task.Delay(1000);
Console.WriteLine("A: " + i);
}
return upper;
}
public static async Task<int> B(int upper)
{
for (int i = 0; i < upper; ++i)
{
await Task.Delay(1000);
Console.WriteLine("B: " + i);
}
return upper;
}
public static async Task<int> C()
{
int xx = 0;
var taskb = Task.Run(async () =>
{
Console.WriteLine("Start B:");
int b = await B(10);
Console.WriteLine("End B:");
return b;
});
int a = await A(5);
if (xx == 0)
{// xx == 0, taskb will run in background even if the function has returned.
return a;
}
else
{
await taskb;
return taskb.GetAwaiter().GetResult();
}
}
static void Main()
{
C().GetAwaiter().GetResult();
Thread.Sleep(200000); //If using breakpoint here, all threads will be interupted
return;
}
}
}
2. Don't use breakpoint to debug, or all tasks will be interupted.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace SystemTasks
{
class TestTask
{
public static async Task<int> A(int upper)
{
for (int i = 0; i < upper; ++i)
{
await Task.Delay(1000);
Console.WriteLine("A: " + i);
}
return upper;
}
public static async Task<int> B(int upper)
{
for (int i = 0; i < upper; ++i)
{
await Task.Delay(1000);
Console.WriteLine("B: " + i);
}
return upper;
}
public static async Task<int> C()
{
int xx = 0;
var taskb = Task.Run(async () =>
{
Console.WriteLine("Start B:");
int b = await B(10);
Console.WriteLine("End B:");
return b;
});
int a = await A(5);
if (xx == 0)
{// xx == 0, taskb will run in background even if the function has returned.
return a;
}
else
{
await taskb;
return taskb.GetAwaiter().GetResult();
}
}
static void Main()
{
C().GetAwaiter().GetResult();
Thread.Sleep(200000); //If using breakpoint here, all threads will be interupted
return;
}
}
}
相关文章推荐
- Let's program run at background in linux/unix
- How can I run Perl system commands in the background
- Run matlab in server background
- Run dos command IN C#
- 110.Which task would you perform before you run Oracle Universal Installer (OUI) in silent or suppre
- 热血军团-runInBackground
- Run exe in background on .NET platform
- Task Scheduler in C#.Net
- selenium2 run in Jenkins GUI testing not visible or browser not open but run in background浏览器后台运行不可见
- DEBUG in background task
- 【Multitasking】Executing a Finite-Length Task in the Background
- Run shell in background
- Myeclips如果勾选了always run in background 该如何把进度条对话框调出
- Completing a Long-Running Task in the Background(在后台完成长时间运行的任务)
- run a bat file or script file in background
- APP Run In Background(App 的后台运行)
- Task cancellation in C# and things you should know about it
- Parallel task in C# 4.0
- Executing a Finite-Length Task in the Background
- Coloring the Console Application (AddingText and Background color in C#)