Created
January 10, 2017 00:04
-
-
Save hyeomans/cb5c941a3eded49bd702f5775390bfa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
namespace StartNewIsDangerous.Controllers | |
{ | |
public class ValuesController : ApiController | |
{ | |
private static List<Task<ThreadInfo>> _CurrentTasks; | |
// GET api/values/5 | |
public string Get(int id) | |
{ | |
_CurrentTasks = new List<Task<ThreadInfo>>(); | |
for (var i = 0; i < id; i++) | |
{ | |
_CurrentTasks.Add(DoItOnBackground()); | |
} | |
return "Tasks started"; | |
} | |
public List<ThreadInfo> Get() | |
{ | |
Task.WaitAll(_CurrentTasks.ToArray()); | |
return _CurrentTasks.Select(x => x.Result).ToList(); | |
} | |
private static Task<ThreadInfo> DoItOnBackground() | |
{ | |
return Task.Factory.StartNew(() => | |
{ | |
var currentThread = Thread.CurrentThread; | |
Thread.Sleep(TimeSpan.FromSeconds(1)); | |
return new ThreadInfo | |
{ | |
IsThreadPool = currentThread.IsThreadPoolThread, | |
ThreadId = currentThread.ManagedThreadId | |
}; | |
}); | |
} | |
} | |
public class ThreadInfo | |
{ | |
public int ThreadId { get; set; } | |
public bool IsThreadPool { get; set; } | |
public override string ToString() | |
{ | |
return $"ThreadID {ThreadId} IsThreadPool {IsThreadPool}"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment