为什么我收到错误没有超载...匹配委托?

时间:2014-09-14 08:48:09

标签: c# .net winforms

在form1中我有一个方法DoRequest:

void DoRequest(ScreenshotRequest.DannysCommands cmd)
        {
            progressBar1.Invoke(new MethodInvoker(delegate()
                {
                    if (progressBar1.Value < progressBar1.Maximum)
                    {
                        progressBar1.PerformStep();

                        _captureProcess.BringProcessWindowToFront();
                        // Initiate the screenshot of the CaptureInterface, the appropriate event handler within the target process will take care of the rest
                        _captureProcess.CaptureInterface.BeginGetScreenshot(new Rectangle(int.Parse(txtCaptureX.Text), int.Parse(txtCaptureY.Text), int.Parse(txtCaptureWidth.Text), int.Parse(txtCaptureHeight.Text)), new TimeSpan(0, 0, 2), Callback,cmd);
                    }
                    else
                    {
                        end = DateTime.Now;
                        txtDebugLog.Text = String.Format("Debug: {0}\r\n{1}", "Total Time: " + (end-start).ToString(), txtDebugLog.Text);
                    }
                })
            );
        }

然后我在form1中的两个地方调用此方法,只需按下按钮点击事件:

DoRequest(ScreenshotRequest.DannysCommands.Displayoverlays);

我得到的错误是在form1中的这个方法:

void Callback(IAsyncResult result)
        {
            using (Screenshot screenshot = _captureProcess.CaptureInterface.EndGetScreenshot(result))
            try
            {
                _captureProcess.CaptureInterface.DisplayInGameText("Screenshot captured...");
                if (screenshot != null && screenshot.CapturedBitmap != null)
                {
                    pictureBox1.Invoke(new MethodInvoker(delegate()
                    {
                        if (pictureBox1.Image != null)
                        {
                            pictureBox1.Image.Dispose();
                        }
                        pictureBox1.Image = screenshot.CapturedBitmap.ToBitmap();
                    })
                    );
                }

                Thread t = new Thread(new ThreadStart(DoRequest));
                t.Start();
            }
            catch
            {
            }
        }

错误已开启:新的ThreadStart(DoRequest)

错误1&#39; DoRequest&#39;匹配委托&#39; System.Threading.ThreadStart&#39;

我该如何解决错误?

2 个答案:

答案 0 :(得分:3)

ThreadStart构造函数需要一个返回void并且不带参数的委托。错误Error 1 No overload for 'DoRequest' matches delegate 'System.Threading.ThreadStart'表明DoRequest的方法签名与ThreadStart委托定义的签名不匹配。这就像是将字符串传递给需要双精度的方法。

请考虑使用ParameterizedThreadStart代替:

Thread t = new Thread(new ParameterizedThreadStart(DoRequest));
t.Start(ScreenshotRequest.DannysCommands.Displayoverlays);

然后编辑您的DoRequest方法以期望您可以投射的对象:

void DoRequest(object data)
{
    // Get your command information from the input object.
    ScreenshotRequest.DannysCommands cmd = (ScreenshotRequest.DannysCommands)data;

    progressBar1.Invoke(new MethodInvoker(delegate()
        {
            if (progressBar1.Value < progressBar1.Maximum)
            {
                progressBar1.PerformStep();

                _captureProcess.BringProcessWindowToFront();
                // Initiate the screenshot of the CaptureInterface, the appropriate event handler within the target process will take care of the rest
                _captureProcess.CaptureInterface.BeginGetScreenshot(new Rectangle(int.Parse(txtCaptureX.Text), int.Parse(txtCaptureY.Text), int.Parse(txtCaptureWidth.Text), int.Parse(txtCaptureHeight.Text)), new TimeSpan(0, 0, 2), Callback,cmd);
            }
            else
            {
                end = DateTime.Now;
                txtDebugLog.Text = String.Format("Debug: {0}\r\n{1}", "Total Time: " + (end-start).ToString(), txtDebugLog.Text);
            }
        })
    );
}

答案 1 :(得分:1)

您的DoRequest中有一个参数。

所以你需要ParameterizedThreadStart

http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx

http://www.dotnetperls.com/parameterizedthreadstart

您需要将参数类型设置为Object

void DoRequest(object param)
{
YourType variable = (YourType)param;
// Something...

}