【问题标题】:c# multithreading method name expectedc# 多线程方法名期望
【发布时间】:2012-04-16 23:11:54
【问题描述】:

我正在尝试创建一个循环,该循环为列表中的每个程序创建一个线程,但在下面的代码中传递边界时出现“预期方法名称”错误;

for (i = 0; i <= programs.Count; i++)
{
    checkProcess check = new checkProcess();
    // check.isRunning();

    string filename = programs[i].Filename;
    string filepath = programs[i].Filepath;

    mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));

    mWorkerThread.Start();
}

我读了一些关于代表的文章,但似乎无法让他们在我的问题的上下文中工作。对于我应该前进的方向,任何帮助将不胜感激。

【问题讨论】:

  • 说实话,我不知道怎么做……让我快速看一下。编辑:明白了!

标签: c# multithreading methods


【解决方案1】:

线程目标应该是可执行的,而不是你的方法的结果。

mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath)));

在上述情况下,您尝试使用check.IsRunning(...)返回值 创建ThreadStart 的新实例。你想要的是类似的东西

mWorkerThread = new Thread( () => check.isRunning(filename, filepath) );

【讨论】:

【解决方案2】:

在您的声明中mWorkerThread = new Thread(new ThreadStart(check.isRunning(filename, filepath))); check.isRunning 是在线程开始时调用的方法名称。

Thread t = new Thread(new ThreadStart(ThreadMethod));
t.Start("My Parameter");

// method that will be called
    private void ThreadMethod(object parameter)
    {
        // parameter equals to "My Parameter"
    }

另一个期望是匿名委托方法,使您的方法内联..使用 lambda 表达式:

   Thread t = new Thread(new ThreadStart(()=>ThreadMethod(parmaValue) ));
    t.Start("My Parameter");

参考: ThreadStart with parameters

【讨论】:

    猜你喜欢
    • 2013-10-21
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多