Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Task.CurrentId work?

I am currently learning how to use Tasks, async and await in Windows Store ("Metro") apps. I stumbled upon the property Task.CurrentId and try to understand how it works.

According to MSDN it returns "An integer that was assigned by the system to the currently-executing task". So I added logging this value to my custom logger, but to my surprise none of my test apps ever logged anything but null.

Look at this example:

private async void TestButton_Click(object sender, RoutedEventArgs e)
{
    int? id1 = Task.CurrentId;

    await Task.Delay(100);
    int? id2 = Task.CurrentId;

    StorageFolder folder = ApplicationData.Current.LocalFolder;
    StorageFile file = await folder.CreateFileAsync("test.txt", 
        CreationCollisionOption.OpenIfExists);
    int? id3 = Task.CurrentId;

    await FileIO.AppendTextAsync(file, "test");
    int? id4 = Task.CurrentId;

    await DoMoreAsync();
    int? id7 = Task.CurrentId;
}

private async Task DoMoreAsync()
{
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    StorageFile file = await folder.CreateFileAsync("test.txt",
        CreationCollisionOption.OpenIfExists);
    int? id5 = Task.CurrentId;

    await FileIO.AppendTextAsync(file, "test");
    int? id6 = Task.CurrentId;
}

All these ids are null. Why? This code does create tasks, doesn't it? Shouldn't they have an id?

like image 389
Sebastian Negraszus Avatar asked Jan 18 '13 12:01

Sebastian Negraszus


People also ask

How does Task run work?

Inside DoComplexCalculusAsync(), Task. Run uses another new thread from thread pool to do the heavy calculations in the background. Thus the thread pool has to deal with unexpectedly loosing one thread from the pool. When the thread completes the computations it is returned back to the thread pool thread.

How do C# tasks work?

What Is A Task In C#? A task in C# is used to implement Task-based Asynchronous Programming and was introduced with the . NET Framework 4. The Task object is typically executed asynchronously on a thread pool thread rather than synchronously on the main thread of the application.

Is Task run good?

Run is completely unnecessary. If you do still need Task. Run , then it doesn't make much difference to the runtime in this case whether you call it once or many times, so just do what's most natural for your code.

What is a Task ID?

Description The Task ID field contains the number that Project assigns to each task as you add it to the project. The Task ID indicates the position of the task with respect to the other tasks. How Calculated As you create tasks, Project automatically assigns the next number in the sequence of tasks as listed.


1 Answers

Because you're using async / await, all your Task.CurrentId calls are happening on the UI thread.

The UI thread is not a Task, so it has a null CurrentId


If you create a Task, its CurrentId will be set:

int? idTask = await Task.Run( () => Task.CurrentId );
like image 157
Nick Butler Avatar answered Oct 10 '22 17:10

Nick Butler