silencereformer 2015-11-22 08:49 采纳率: 50%
浏览 1757
已采纳

C#创建线程调用方法参数出错

我想创建几个线程来熟悉多线程的一些知识于是写了一个小程序

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace 多线程试验
{
    class Program
    {
        static void Main(string[] args)
        {
            int 线程计数器 = 1;
            函数库 H = new 函数库();
            Thread th1 = new Thread(new ThreadStart(H.显示(线程计数器)));
            线程计数器++;
            Thread th2 = new Thread(new ThreadStart(H.显示(线程计数器)));
            线程计数器++;
            Thread th3 = new Thread(new ThreadStart(H.显示(线程计数器)));
            线程计数器++;

        }
    }
}

上面是主方法,下面是我要用到的方法

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;

namespace 多线程试验
{
    class 函数库
    {
        public void 显示(int i)
        {
            Console.WriteLine("这是第{0}个线程",i);
            Thread.Sleep(2000);
            Console.WriteLine("第{0}个线程结束",i);
        }
    }
}

但是在主函数中直接调用就没问题,但是如果在创建线程的时候作为参数调用方法就会出问题,错误提示是“应输入方法名称”。求各位大神解惑,先谢过!
错误代码是
Thread th1 = new Thread(new ThreadStart(H.显示(线程计数器)));
Thread th2 = new Thread(new ThreadStart(H.显示(线程计数器)));
Thread th3 = new Thread(new ThreadStart(H.显示(线程计数器)));
这三句

  • 写回答

2条回答 默认 最新

  • 微wx笑 Java领域优质创作者 2015-11-22 10:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?