5

As a simple example I have a WPF application with a single button on the Main Window and code behind thus:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    async void Button_Click(object sender, RoutedEventArgs e)
    {
        await Task<bool>.Run(() => this.DoOnThread());
    }

    async Task<bool> DoOnThread()
    {
        Thread.CurrentThread.Name = "MyTestThread";
        Thread.Sleep(1000);
        return true;
    }
}

If I break at "return true" via VisualStudio threads window I can get the ThreadID, if I continue and let the code run to completion and wait a little till the thread exits, I get "The thread 0x9ad34 has exited with code 259 (0x103)" displayed in the VS output window.

What am I doing wrong and how do I ensure I get a thread exit code of 0?

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
John
  • 1,714
  • 21
  • 41
  • 1
    Exit Code 259 is "STILL_ACTIVE". I'm not sure what you're trying to accomplish here. – Adam Sears Feb 07 '14 at 16:16
  • 3
    Agreed with Adam... what are you trying to do? Tasks run on the threadpool, those threads stay alive and wait for work. – J... Feb 07 '14 at 16:18
  • It's just that I see these exit coded is Visual Studio output and was curious as to what was causing it. my above simple, crude example was my attempt at getting an answer, nothing more. Thank you. – John Feb 07 '14 at 16:32
  • 2
    [Tip](http://damieng.com/blog/2014/02/05/8-visual-studio-debugging-tips-debug-like-a-boss): right-click and uncheck module/thread/process messages. They're almost always just noise. – Stephen Cleary Feb 07 '14 at 16:34

2 Answers2

15

Task.Run does not create a thread. It schedules a delegate to run on a ThreadPool thread. The threads in a threadpool are created or destroyed according to the CPU load.

The exit code you see has nothing really to do with your code: it may simply be a Visual Studio debug message, or a ThreadPool thread that exited.

Additionally, async doesn't mean that a method will run asynchronously. It is syntactic sugar that allows the compiler to create code to wait asynchronously for any asynchronous methods marked with await. In your case, DoOnThread has no asynchronous calls or await so it will run syncrhonously.

In fact, the compiler will even emit a warning that DoOnThread doesn't contain await so it will run synchronously

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 1
    It schedules a *delegate*, which may or may not be lambda. – Servy Feb 07 '14 at 16:19
  • 1
    Thank you, Task.Run causes the action to execute on a thread different to the UI thread. I have verified this by checking the Managed Thread ID of both the button click and the DoOnthread methods. I was just concerned that exit code 259 was a problem, but it seems I should just ignore it. – John Feb 07 '14 at 16:30
9

Thread pool threads do not belong to you. You should not set their Name, nor should you be concerned about their exit codes.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Yes thanks, I only named it so I could identify it easily for this simple test. However point taken. – John Feb 07 '14 at 16:30