Description
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
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