Hi,

I wonder if the below code I have take up "2 threads" on the processor.
In the Form1_Load event I call "startProcess". That process starts a backgroundworker
in a separate class. The process also monitor callbacks from the backgroundworker.
This is what makes me wonder if this takes up "2 threads" on the processor.

The big concern with my question is that I will launch the application 20 times which would
mean that 40 threads are running where perheps 20 of those are not nessecary.
To add is that is a big task to move the backgroundworker to the Form1.cs as the Instance.cs
contain 20,000 lines and alot of code to report back to "progress =>".

So I wonder if it takes up 2 threads. If so can there be a solution to make it only run the backgroundworker
as it is setup now and still monitor "progress =>" ?

        //Form1.cs
        private void Form1_Load(object sender, EventArgs e)
        {
            startProcess();
        }

        //Start thread
        void startProcess()
        {

            //Start the bgWorker with below code
            Project.Instance ts = new Project.Instance();

            ts.doSomething(
                //Here we will update the progress after different scenarios depending on the string that is received(message):
                        progress =>
                        {
                            //Get the returning string here:
                            String returnString = progress;
                            MessageBox.Show(returnString);
                        },
                        result =>
                        {
                            //bgWorker is finished
                        }
                    );
        }

        //Instance.cs
        public void doSomething(Action<String> progressCallBack, Action<String> completionCallBack)
        {
            BackgroundWorker bgWorker = new BackgroundWorker();
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.WorkerReportsProgress = true;
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.ProgressChanged += (sender, args) => { progressCallBack(args.UserState.ToString()); };
            bgWorker.RunWorkerCompleted += (sender, args) => { completionCallBack(args.Result.ToString()); };

            //Start worker
            if (bgWorker.IsBusy == false)
            {
                bgWorker.RunWorkerAsync();
            }
        }
        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = (BackgroundWorker)sender;

            //This will report back to 'progress'
            worker.ReportProgress(10, "Report back something to 'progress'");

            //Finished
            e.Result = "Process is finished";
        }

This seems to be an extension on your last question. I'm going to share and be inexact here.

Rather than dive into your code you may want to look at the machine behind C# where it's CLR (https://en.wikipedia.org/wiki/Common_Language_Runtime) is just one instance on one core and threads appear to be in that CPU instance.

I quickly saw how similar this was to Java and when I needed to get all CPUs up and working would not thread but launch the app again which revs up another CLR and that CLR runs our byte code C# app.

Hope this removes the mystery for you.

is just one instance on one core and threads appear to be in that CPU instance.
not thread but launch the app again which revs up another CLR

Thanks for your answer. Yes it is an extension of my last question. Sometimes its good to divide up the problem in two to not get to confusing.
I beleive the above that you mentioned are positive where threads are assigned to that CPU in which the instance was launched.
I have implemented the logic to launch instances/apps of the thread to be working in parallell and it actually so far is going well.
My test was with 10 apps in parallell and they executed the work in 1.30 minutes. Launching just one app, the work takes 13 minutes.
So it is about 10 times faster actually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.