© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
FirstGearGames

❔ 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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Passing back to the main thread
C#CC# / help
13mo ago
✅ Infinite Bandwidth Work-around?
C#CC# / help
2y ago
✅ how does passing byref work
C#CC# / help
11mo ago
OnAppendCookie callback doesn't work
C#CC# / help
4y ago