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))
{ }
}