❔ Passing around thread to perform work on?

I'd like to call a method that runs on the main thread but offloads a portion of it's work onto a passed in thread. Here's a stripped down version of what I'm looking to accomplish...
private BackgroundWorker _worker = new BackgroundWorker();
private ConcurrentBag<AResult> _results = new ConcurrentBag<AResult>();
public enum AResult
{
Passed,
Failed,
Error
}
public void DoThing(BackgroundWorker worker, ConcurrentBag<AResult> bag)
{
//Nevermind this could all be done on worker thread, pretend some must be done on main.
//Main thread...
float a = 10f;
float b = 20f;

//Do this in worker....
if (a == b)
bag.Add(AResult.Passed);
else if (a > b)
bag.Add(AResult.Failed);
else
bag.Add(AResult.Error);
}

//This is called on the main thread.
private void DoCalculations()
{
int count = _collection.Count;
for (int i = 0; i < count; i++)
_collection[i].DoThing(_worker, _results);
}
//Called regularly on the main thread.
private void HandleResults()
{
while (_results.TryTake(out AResult result))
{ }
}
private BackgroundWorker _worker = new BackgroundWorker();
private ConcurrentBag<AResult> _results = new ConcurrentBag<AResult>();
public enum AResult
{
Passed,
Failed,
Error
}
public void DoThing(BackgroundWorker worker, ConcurrentBag<AResult> bag)
{
//Nevermind this could all be done on worker thread, pretend some must be done on main.
//Main thread...
float a = 10f;
float b = 20f;

//Do this in worker....
if (a == b)
bag.Add(AResult.Passed);
else if (a > b)
bag.Add(AResult.Failed);
else
bag.Add(AResult.Error);
}

//This is called on the main thread.
private void DoCalculations()
{
int count = _collection.Count;
for (int i = 0; i < count; i++)
_collection[i].DoThing(_worker, _results);
}
//Called regularly on the main thread.
private void HandleResults()
{
while (_results.TryTake(out AResult result))
{ }
}
I'm not 100% sure on the best way to go about this.
2 Replies
FirstGearGames
FirstGearGamesOP2y ago
I'm aware you could run the second half of DoThing in a Task but that method could be called hundreds of times at once and starting new Tasks that frequently seems like a bad idea. Although reading more about this, it would suggest Tasks might be okay? I'd still like to be able to control the thread used if possible.
Tasks on the other hand are a higher-level .NET abstraction that basically represents a promise of a separate work, that’ll be completed in future. For example, a Task<T> is nothing but a task that comes with the promise of returning a value of type T when the task completes. They are compositional in nature, capable of returning values and being chained (at any amount) by using task continuations. They are also capable of using the Thread Pool and extremely handy for being used for I/O bound operations.
Tasks on the other hand are a higher-level .NET abstraction that basically represents a promise of a separate work, that’ll be completed in future. For example, a Task<T> is nothing but a task that comes with the promise of returning a value of type T when the task completes. They are compositional in nature, capable of returning values and being chained (at any amount) by using task continuations. They are also capable of using the Thread Pool and extremely handy for being used for I/O bound operations.
Disregard. I'll just use the ThreadPool methods to control utilization better.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server