zoukankan      html  css  js  c++  java
  • C# 多线程传参 三种实例

    //using Thread to download files

    //1111111111111111
    foreach (var str in listDownloadPdf)
    {
    //string hello1 = "hello world";
    //这里也可简写成Thread thread = new Thread(ThreadMainWithParameters);
    //但是为了让大家知道这里用的是ParameterizedThreadStart委托,就没有简写了
    Thread thread1 = new Thread(new ParameterizedThreadStart(ThreadMainWithParameters));
    thread1.Start(str);
    }

    //2222222222222222222
    foreach (var str in listDownloadPdf)
    {
    MyThread myThread = new MyThread(str);
    Thread thread2 = new Thread(myThread.ThreadMain);
    thread2.Start();
    }

    //333333333333333
    foreach (var str in listDownloadPdf)
    {
    //string hello3 = "hello world";
    //如果写成Thread thread = new Thread(ThreadMainWithParameters(hello));这种形式,编译时就会报错
    Thread thread3 = new Thread(() => ThreadMainWithParameters(str));
    thread3.Start();
    }

    //333333333333333333333
    static void ThreadMainWithParameters(string str)
    {
    Console.WriteLine("33333333333333333,received: {0}", str);
    }
    //111111111111111111111
    static void ThreadMainWithParameters(object obj)
    {
    string str = obj as string;
    if (!string.IsNullOrEmpty(str))
    Console.WriteLine("1111111111111111,received: {0}", str);
    }

    //222222222222222222
    public class MyThread
    {
    private string data;

    public MyThread(string data)
    {
    this.data = data;
    }

    public void ThreadMain()
    {
    Console.WriteLine("222222222222222222: {0}", data);
    }
    }

  • 相关阅读:
    P1016 旅行家的预算
    导航菜单全部解释调用外部样式
    css调用外部样式和css样式说明剧中显示
    两种调用外部样式的方法
    css行内样式
    css选择器集体声明
    CSS用类选择器在本页写样式
    CSS用Id选择器在本页写样式
    css用标签选择器在本页写样式
    CSS本页写样式
  • 原文地址:https://www.cnblogs.com/HaifengCai/p/3625632.html
Copyright © 2011-2022 走看看