WebAPI Async
先附上自己練習的Code , 之後再補內容
public class StudyAsyncController : ApiController
{
#region 取數值(如果沒有打停止一秒的話,反而 同步 > 假同步 > 非同步)
[HttpGet]
[Route("StudyAsync/GetDataFalseAsync")]
public async Task<IHttpActionResult> GetFalseAsync()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
var testTimeResult = await TestTimeAsync();
var testTime2Result = await TestTime2Async();
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"{testTimeResult},{testTime2Result},Time:{result}");
}
[HttpGet]
[Route("StudyAsync/GetDataAsync")]
public async Task<IHttpActionResult> GetAsync()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
var testTimeTask = TestTimeAsync();
var testTime2Task = TestTime2Async();
var testTimeResult = await testTimeTask;
var testTime2Result = await testTime2Task;
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"{testTimeResult},{testTime2Result},Time:{result}");
}
[HttpGet]
[Route("StudyAsync/GetData")]
public IHttpActionResult Get()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
var testTimeResult = TestTime();
var testTime2Result = TestTime2();
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"{testTimeResult},{testTime2Result},Time:{result}");
}
public int TestTime()
{
var result = 0;
for (int i = 0; i <= 5000000; i++)
{
result++;
}
Thread.Sleep(1000);
return result;
}
public int TestTime2()
{
var result = 0;
for (int i = 0; i <= 5000000; i++)
{
result++;
}
Thread.Sleep(1000);
return result;
}
public async Task<int> TestTimeAsync()
{
var result = 0;
for (int i =0;i<=5000000;i++)
{
result++;
}
await Task.Delay(1000);
return result;
}
public async Task<int> TestTime2Async()
{
var result = 0;
for (int i = 0; i <= 5000000; i++)
{
result++;
}
await Task.Delay(1000);
return result;
}
#endregion
#region 測試睡眠
[HttpGet]
[Route("StudyAsync/Sleep/GetDataFalseAsync")]
public async Task<IHttpActionResult> GetSleepFalseAsync()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
await TestSleepTimeAsync();
await TestSleepTime2Async();
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"Time:{result}");
}
[HttpGet]
[Route("StudyAsync/Sleep/GetDataAsync")]
public async Task<IHttpActionResult> GetSleepAsync()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
var aa= TestSleepTimeAsync();
var bb= TestSleepTime2Async();
await aa;
await bb;
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"Time:{result}");
}
[HttpGet]
[Route("StudyAsync/Sleep/GetData")]
public IHttpActionResult GetSleep()
{
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();//引用stopwatch物件
sw.Reset();//碼表歸零
sw.Start();//碼表開始計時
/**************/
/**************/
/***目標程式***/
TestSleepTime();
TestSleepTime2();
/**************/
/**************/
sw.Stop();//碼錶停止
//印出所花費的總豪秒數
string result = sw.Elapsed.TotalSeconds.ToString();
return this.Ok($"Time:{result}");
}
public void TestSleepTime()
{
Thread.Sleep(5000);
}
public void TestSleepTime2()
{
Thread.Sleep(5000);
}
public async Task TestSleepTimeAsync()
{
await Task.Delay(5000);
}
public async Task TestSleepTime2Async()
{
await Task.Delay(5000);
}
#endregion
}