Part 89   ParameterizedThreadStart delegate

Use ParameterizedThreadStart delegate to pass data to the thread function

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("input a target number:");
            object target = Console.ReadLine();
            ParameterizedThreadStart pt = new ParameterizedThreadStart(Number.Print);//really need to write that?No,you can just pass the function into Thread class.
            Thread t = new Thread(pt);//new Thread(Number.Print);
            t.Start(target);
            Console.ReadLine();
        }
    }

    public class Number
    {
        public static void Print(object target)
        {
            int number = 0;
            if (int.TryParse(target.ToString(), out number))
            {
                for (int i = 0; i < number; i++)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }
View Code

相关文章:

  • 2021-12-21
  • 2022-01-13
  • 2021-12-08
  • 2021-05-28
  • 2021-11-21
  • 2021-09-05
  • 2021-10-22
猜你喜欢
  • 2021-07-09
  • 2022-01-12
  • 2021-06-17
  • 2022-03-07
相关资源
相似解决方案