Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,026 questions

51,982 answers

573 users

How to use thread array in C#

2 Answers

0 votes
using System;
using System.Threading;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread[] arr = new Thread[3];

            for (int i = 0; i < arr.Length; i++)
            {
                ThreadStart ts = new ThreadStart(Method);
                arr[i] = new Thread(ts);
                arr[i].Start();
            }
            Console.WriteLine("END");
        }
        static void Method()
        {
            Thread.Sleep(500);
            Console.WriteLine("Method()");
        }
    }
}

/*
run:

END
Method()
Method()
Method()

*/

 



answered Jan 20, 2017 by avibootz
0 votes
using System;
using System.Threading;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Thread[] arr = new Thread[3];

            for (int i = 0; i < arr.Length; i++)
            {
                ThreadStart ts = new ThreadStart(Method);
                arr[i] = new Thread(ts);
                arr[i].Start();
            }
            // joins the threads together in sequential order
            for (int i = 0; i < arr.Length; i++)
                arr[i].Join();

            Console.WriteLine("END");
        }
        static void Method()
        {
            Thread.Sleep(500);
            Console.WriteLine("Method()");
        }
    }
}

/*
run:

Method()
Method()
Method()
END

*/

 



answered Jan 20, 2017 by avibootz

Related questions

2 answers 105 views
1 answer 156 views
156 views asked Sep 8, 2015 by avibootz
1 answer 103 views
103 views asked Sep 1, 2024 by avibootz
1 answer 90 views
90 views asked Sep 1, 2024 by avibootz
1 answer 98 views
98 views asked Sep 1, 2024 by avibootz
...