Translate

Tuesday, June 10, 2014

How to pass data into a thread in C# (Call a thread with input parameters)

Using a Lambda expression, you can pass any number of parameters into a method when calling it thru a thread.

using System;
using System.Threading;

namespace ThreadInputParam
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var t = new Thread(() => Print("my input string",3));
            t.Start();
        }

        public static void Print(string s,int i)
        {
            Console.WriteLine(s);
            Console.WriteLine(i);
        }
    }
}


No comments:

Post a Comment

Comments will appear once they have been approved by the moderator