HRESULT.ToString() relies on a string cache that is not thread-safe, so when calling HRESULT.ToString() from different threads, an exception is triggered when updating this cache.
What code is involved
This can be easily triggered with the following snippet:
using System.Diagnostics;
using Vanara.PInvoke;
var a1 = Task.Run(() => { for (int i = 0; i < 100; ++i) { Debug.Print(new HRESULT().ToString()); } });
var a2 = Task.Run(() => { for (int i = 0; i < 100; ++i) { Debug.Print(new HRESULT().ToString()); } });
Task.WaitAll(a1, a2);
Expected behavior
Two separate HRESULT objects, that exist separately on different threads, should not interfere with one another.
Describe the bug and how to reproduce
HRESULT.ToString()
relies on a string cache that is not thread-safe, so when callingHRESULT.ToString()
from different threads, an exception is triggered when updating this cache.What code is involved
This can be easily triggered with the following snippet:
Expected behavior
Two separate
HRESULT
objects, that exist separately on different threads, should not interfere with one another.Screenshots
Code that triggers exception:![image](https://github.com/dahall/Vanara/assets/129744627/a6c69b03-5cc2-422f-a414-76117257a79a)
Exception source: