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

I'm not 100% sure on the best way to go about this.
Was this page helpful?