为什么我';m正在获取错误…没有过载..匹配委托

本文关键字:没有过 取错误 获取 为什么 | 更新日期: 2023-09-27 18:21:52

在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);

我得到的错误是在这个方法中的形式1:

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"DoRequest"没有与委托"System.Threading.ThreadStart"匹配的重载

我该如何解决这个错误?

为什么我';m正在获取错误…没有过载..匹配委托

ThreadStart构造函数需要一个返回void且不接受参数的委托。错误Error 1 No overload for 'DoRequest' matches delegate 'System.Threading.ThreadStart'表示DoRequest的方法签名与ThreadStart委托定义的签名不匹配。这就像你把一个字符串传递给一个需要double的方法。

考虑使用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);
            }
        })
    );
}

您的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...

}