Saturday, February 23, 2013

Threading: 04- Thread with multiple parameters without class or struct

Instead of creating a class to pass multiple parameters to a thread, we can use Object arrays to be passed to the method.

 

Example:

using System;
using System.Threading;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("Main Started");
        var th = new Thread(Greet); // Create new thread
        th.Start(new object[] { "Bob", "Good Morning" });  // Execute Greet method in the created thread
        Console.WriteLine("Main Completed");
        Console.ReadKey();
    }

    private static void Greet(object info) {
        Console.WriteLine("Hi {0}, {1}!", (info as object[])[0], (info as object[])[1]);
    }
}

No comments:

Post a Comment