I want to avoid using fields when sharing data between 2 threads.
Consider 2 classes – user and handler.
User constructs handler and tells it to change a user.flag variable after 1000ms.
There could be many users with various flags. I can of course use an event or action or callback but wont it be a lot nicer if I could pass flag by ref and simply change its value
(NOTE – user does not need to know when exactly flag value changed! It only needs to have flag == true at any arbitrary point of time after 1000ms user checks the flag)
I would write something like this (without using unsafe):
bool userFlag;
void UserInitHandler()
{
var handler = new Handler(1000)
handler.Start(ref userFlag);
}
class Handler
{
...
void Start(ref bool flag)
{
ThreadPool.QueueUserWorkItem((o) =>
{
Thread.Sleep(delay);
flag = true;
}
}
}