C# threads array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ToS1
    New Member
    • Aug 2008
    • 1

    #1

    C# threads array

    Hi,

    I'm traying to make a thread array to a function with one variable, but i get everytime an error. I trayed to put the variable by the call, but the compiler doen't like that too.



    Code:
    static void Main(string[] args)
    {
    Thread[] threads = new Thread[5];
    
    for(int i = 0; i < threads.Length; i++)
    {
    threads[i] = new Thread(new ThreadStart(DoWork));
    }
    
    for(int i = 0; i < threads.Length; i++)
    {
    threads[i].Start();
    }
    
    for(int i = 0; i < threads.Length; i++)
    {
    threads[i].Join();
    }
    
    Console.WriteLine("Complete");
    }
    
    public void DoWork(String myStr)
    {
    Console.WriteLine("DoWork"+myStr);
    }
    Last edited by kenobewan; Aug 24 '08, 12:25 PM. Reason: Please use code tags
  • DonBytes
    New Member
    • Aug 2008
    • 25

    #2
    Code:
    static void Main(string[] args)
    		{
    			Thread[] threads = new Thread[5];
    
    			for (int i = 0; i < threads.Length; i++)
    			{
    				threads[i] = new Thread(new ParameterizedThreadStart(DoWork));
    			}
    
    			for (int i = 0; i < threads.Length; i++)
    			{
    				threads[i].Start(i.ToString()); // or whatever you want to send to your thread
    			}
    
    			for (int i = 0; i < threads.Length; i++)
    			{
    				threads[i].Join();
    			}
    
    			Console.WriteLine("Complete");
    			Console.ReadKey();
    		}
    
    		public static void DoWork(Object StringObject)
    		{
    			String myStr = (StringObject as String);
    			Console.WriteLine(String.Format("{0} DoWork", myStr));
    		}

    Comment

    Working...