zoukankan      html  css  js  c++  java
  • C#多线程方法 可传参

    //将线程执行的方法和参数都封装到一个类里面。通过实例化该类,方法就可以调用属性来实现间接的类型安全地传递参数。
    using
    System; using System.Threading; //ThreadWithState 类里包含了将要执行的任务以及执行任务的方法 public class ThreadWithState { //要用到的属性,也就是我们要传递的参数 private string boilerplate; private int value; //包含参数的构造函数 public ThreadWithState(string text, int number) { boilerplate = text; value = number; } //要丢给线程执行的方法,本处无返回类型就是为了能让ThreadStart来调用 public void ThreadProc() { //这里就是要执行的任务,本处只显示一下传入的参数 Console.WriteLine(boilerplate, value); } } //用来调用上面方法的类,是本例执行的入口 public class Example { public static void Main() { //实例化ThreadWithState类,为线程提供参数 ThreadWithState tws = new ThreadWithState( "This report displays the number {0}.", 42); // 创建执行任务的线程,并执行 Thread t = new Thread(new ThreadStart(tws.ThreadProc)); t.Start(); Console.WriteLine("Main thread does some work, then waits."); t.Join(); Console.WriteLine( "Independent task has completed; main thread ends."); } }
  • 相关阅读:
    JS---案例:拖曳对话框
    关于选用ccflow你所担心的问题都在这里为您解答
    Elasticsearch(GEO)数据写入和空间检索
    echart的legend不显示问题
    html5+css3的神奇搭配
    h5+css3+Jq
    初探 jQuery
    在vi编辑器中如何向上翻页,向下翻页以及查询“ab”
    webpack4 特性
    webpack4配置
  • 原文地址:https://www.cnblogs.com/tmdsleep/p/5725744.html
Copyright © 2011-2022 走看看