Skip to content

When using ThreadPool.QueueUserWorkItem with a method that throws an exception, the entire application crashes and exits unexpectedly. This behavior is not documented and can lead to unintended application terminations. #11084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1257960069 opened this issue Mar 13, 2025 · 1 comment
Labels
area-System.Threading untriaged New issue has not been triaged by the area owner

Comments

@1257960069
Copy link

1257960069 commented Mar 13, 2025

Type of issue

Typo

Description

[在此处输入反馈]

using Microsoft.AspNetCore.Mvc;

namespace WebApplic.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            ThreadPool.QueueUserWorkItem(_ =>
            {
                throw new Exception("An error occurred.");
            });
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

Expected behavior
The exception should be caught and handled appropriately, or at the very least, the documentation should clearly state that unhandled exceptions in ThreadPool.QueueUserWorkItem will cause the application to crash.

Screenshots
N/A

Additional context
This issue can lead to unexpected application crashes and loss of data. It is important to either handle the exception within the QueueUserWorkItem method or ensure that the documentation clearly states this behavior to prevent such issues.

Environment:

  • OS: [e.g., Windows 11]
  • .NET version: [e.g., .NET 8.0]
  • IDE: [e.g., Visual Studio 2022]

Please update the documentation to reflect this behavior or provide a solution to handle exceptions in ThreadPool.QueueUserWorkItem without crashing the application.

Thank you.

Page URL

https://learn.microsoft.com/zh-cn/dotnet/api/system.threading.threadpool.queueuserworkitem?view=net-8.0&devlangs=csharp&f1url=%3FappId%3DDev17IDEF1%26l%3DZH-CN%26k%3Dk(System.Threading.ThreadPool.QueueUserWorkItem)%3Bk(TargetFrameworkMoniker-.NETFramework%2CVersion%253Dv4.7.2)%3Bk(DevLang-csharp)%26rd%3Dtrue

Content source URL

https://github.com/dotnet/dotnet-api-docs/blob/main/xml/System.Threading/ThreadPool.xml

Document Version Independent Id

e13ed79b-e212-1d7a-f939-6086b300a399

Platform Id

aa4d1ae9-7ae8-c1d8-6d31-f91535257631

Article author

@dotnet-bot

@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Mar 13, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 13, 2025
@1257960069
Copy link
Author

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    internal class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.QueueUserWorkItem(state =>
            {
                throw new Exception("An error occurred.");
            });
            Console.WriteLine("Hello, World!");
            Console.ReadLine();
        }
    }
}

Maybe should recommand developer use Task.Run

@gewarren gewarren added area-System.Threading and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Mar 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Threading untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants