在c#中发送参数错误

时间:2018-03-06 17:35:11

标签: c# multithreading c#-4.0 parameters thread-safety

我在c#中编写了以下代码,经过多次尝试后,我仍然在注释行中遇到同样的错误

  

错误消息:"预期的方法名称"

所以请您告诉我发送" tmp"的合适方式。我的LoadSpeceficSontrol函数的变量。 任何帮助都会提前感激。

 private void AddBox()
    {
        Thread thread= new Thread(() => LoadControls());
        thread.Start();
    }

    delegate void LoadControlsCallback();

    private void LoadControls()
    {
        int Nu = int.Parse(PageNumber.Text);
        for (int i = (Nu - 1) * 100; i < Nu * 200 && i < MoviesList.Count; i++)
        {
            MovieControl tmp = new MovieControl(MoviesList[i]);
            if (tmp.InvokeRequired || MoviesFlowPanel.InvokeRequired)
            {
                LoadControlsCallback d = new LoadControlsCallback(LoadSpeceficControl, new object[] {tmp}); // error here
                this.Invoke(d);
            }
            else
            MoviesFlowPanel.Controls.Add(tmp);
        }
    }

    private void LoadSpeceficControl(MovieControl tmp)
    {
        MoviesFlowPanel.Controls.Add(tmp);   
    }

代码版本2.0

private void AddBox()
{
    Thread thread= new Thread(() => LoadControls());
    thread.Start();
}

private void LoadControls()
{
    int Nu = int.Parse(PageNumber.Text);
    for (int i = (Nu - 1) * 100; i < Nu * 200 && i < MoviesList.Count; i++)
    {
        MovieControl tmp = new MovieControl(MoviesList[i]);
        if (tmp.InvokeRequired || MoviesFlowPanel.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { MoviesFlowPanel.Controls.Add(tmp); });
        }
        else 
        MoviesFlowPanel.Controls.Add(tmp);
        tmp = null;
    }
}

2 个答案:

答案 0 :(得分:0)

delegate void LoadControlsCallback(Action<MovieControl> action, object[] objarray); 的委托定义应与签名匹配。与您的使用方式类似,它应该如下所示:

 <textarea name="reporterRemarks" rows="5" 
onchange="javascript:document.itemActionForm.remarksUpdateFlag.value = 
'Y';document.itemActionForm.updateFlag.value = 'Y';" id="reporterRemarks" 
class="form-control fixed-width-font"></textarea>

答案 1 :(得分:0)

应该是这样的:

delegate void LoadControlsCallback(MovieControl tmp);

LoadControlsCallback d = LoadSpeceficControl;
d(tmp);