3

Selection of the number of threads dynamically in C #?

 2 years ago
source link: https://www.codesd.com/item/selection-of-the-number-of-threads-dynamically-in-c.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Selection of the number of threads dynamically in C #?

advertisements

I am trying to create several threads based on the value received by the program. I am using the following code.

int count = 7;
Class1 cl=new Class1();

for (int i = 0; i < count; i++)
{
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs argss)
    {
        BackgroundWorker b = o as BackgroundWorker;
        cl.print("id","password");
    });
}

Console.ReadLine();

In print() method of class1 I print a single line "Just entered print function".

But when I run the program no line is printed.


You need to start the background thread.

class Program
    {
        static void Main()
        {
            int count = 7;

            Class1 cl = new Class1();
            for (int i = 0; i < count; i++)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.DoWork += new DoWorkEventHandler(
                delegate(object o, DoWorkEventArgs argss)
                {
                    BackgroundWorker b = o as BackgroundWorker;

                    cl.Print("id", "password");
                });

                bw.RunWorkerAsync();//Start the background here

            }

            Console.ReadLine();
        }
    }

    class Class1
    {
        public void Print(string id, string password)
        {
            Console.WriteLine("Id:{0},Password:{1}", id, password);
        }
    }


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK