Hi Guys,
I'm trying out multithreading but I cant figure out why the the piece of code below blocks the UI when task.Execute() is called?
public class Task<TRes>
{
private Func<TRes> _func;
public Task(Func<TRes> func)
{
_func = func;
}
public TRes Execute()
{
var iasync = _func.BeginInvoke(null, null);
return _func.EndInvoke(iasync);
}
}
I'm not trying to solve any problem in particular. Just wondering why the UI blocks when it hits Thread.Sleep even though the current thread (the one that was put to "sleep") is a ThreadPool thread (and not the main UI thread). Below is a button click event that uses the Task class.
var task = new Task<string>(() =>
{
var start = DateTime.Now;
var isBackGround = Thread.CurrentThread.IsBackground ;
var isThreadPool = Thread.CurrentThread.IsBackground;
Thread.Sleep(5000);
return String.Format("Start {0} : End {1}", start, DateTime.Now);
}
);
this.label1.Text = task.Execute();