I have created a function as follows
public int ManFunction()
{
Task task = Task.Factory.StartNew(RunTask, cts_token.Token);
return task.Id;
}
Is there any way to get running task by task-Id in C#?
basically, when do I call function a second time I want to check whether the task is running or not? based on that I want to pass a notification to the user that the task is already running.
Using Task.Id to as identifiers bad idea. Task.Id is not unique see remarks here
Your function instead of id can return Task:
public Task ManFunction()
{
Task task = Task.Factory.StartNew(RunTask, cts_token.Token);
return task;
}
Basically you can work with Task object like with id, no difference to you. Than you can use task.IsCompleted to check is task done or not, like:
var t = this.ManFunction();
//.....
if (!t.IsCompleted)
{
//.....
}