我在新开一个线程时,线程进行的函数是个带参数的函数,这时候该怎么处理这个参数?
具体如下:
{
...
           da.Fill(ds);
            Thread show_curve = new Thread(GenerateCure(ds,0,1));//这里系统提示错误
            show_curve.Start();
}
private void GenerateCure(DataSet ds, int x_num, int y_num)
{
......}
----------
提示的错误:
错误 CS1502: 与“System.Threading.Thread.Thread(System.Threading.ThreadStart)”最匹配的重载方法具有一些无效参数
错误 CS1503: 参数“1”: 无法从“void”转换为“System.Threading.ThreadStart”
请高手给分析一下,应该怎么改

解决方案 »

  1.   

    改用 ParameterizedThreadStart委托ParameterizedThreadStart s = new ParameterizedThreadStart(Hello);
    Thread thd = new Thread(s);
    thd.Start("hello");........
    void UpdateBook(object str)
    {
        Console.WriteLine(str.ToString());
    }
      

  2.   

    使用一个调用一个类的方法,参数在类的构造函数中初始化赋值。
    public class CellProd
    {
      Cell cell; // 被操作的Cell对象
      int quantity = 1; // 生产者生产次数,初始化为1   public CellProd(Cell box, int request)
      {
      //构造函数
      cell = box; 
      quantity = request; 
      }
      public void ThreadRun( )
      {
      for(int looper=1; looper<=quantity; looper++)
        cell.WriteToCell(looper); file://生产者向操作对象写入信息
      }
    } 
    public class MonitorSample
    {
      public static void Main(String[] args)
      {
      int result = 0; file://一个标志位,如果是0表示程序没有出错,如果是1表明有错误发生
      CellProd prod = new CellProd(cell, 20); 
        Thread producer = new Thread(new ThreadStart(prod.ThreadRun));
        }
    }
      

  3.   

    一楼 和 四楼的都是正解,个人喜欢四楼的解多一些,因为可以在四楼的类里面加上 event(一楼的也可以,就是比较麻烦),就可以在创建出来的线程中回调了。楼主接下来要注意跨线程调用的问题了。
      

  4.   

    public class MonitorSample 
    { 
    这两句应该删掉的
      

  5.   


    ParameterizedThreadStart s = new ParameterizedThreadStart(Hello);
    Thread thd = new Thread(s);
    thd.Start(
    new object[]{"hello",5,"world"}//数组中三个参数
    );........
    void UpdateBook(object str)
    {
        object[] values=str as object[];
        //拆出来三个参数,比较猥琐的做法,如果要OO,还是四楼的思路,不过要自己定义类就是了
        string val1=values[0].ToString();
        int count=Convert.ToInt32(values[1]);
        string val2=values[2].ToString();
        for(int i=0;i<count;i++)
        {
             Console.WriteLine(val1 + " " + val2);
        }
    }