c# - 为什么我收到错误 No overload for ... matches delegate?

标签 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
            {
            }
        }

错误是:new ThreadStart(DoRequest)

错误 1 ​​'DoRequest' 的重载不匹配委托(delegate) 'System.Threading.ThreadStart'

我该如何解决错误?

最佳答案

ThreadStart 构造函数需要一个返回 void 且不带任何参数的委托(delegate)。错误 Error 1 No overload for 'DoRequest' matches delegate 'System.Threading.ThreadStart' 表示 DoRequest 的方法签名与 定义的签名不匹配>ThreadStart 委托(delegate)。这就像您将一个字符串传递给一个需要 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);
            }
        })
    );
}

关于c# - 为什么我收到错误 No overload for ... matches delegate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831599/

相关文章:

C#动态编译字符串和.cs文件

c# - 如何禁用 Listview 自动查找您键入的内容

.net - 无法在 Controller 'System.Web.Mvc.PartialViewResult Foo[T](T)' 上调用操作方法 'Controller',因为操作方法是通用方法

c# - 为什么我的验证事件没有在 C# 中触发?

c# - 检测数据集/数据表中的行何时更改

c# - 如何创建没有碎片的 Uri(将 # 转换为 %23)

c# - 记录 WCF soap 消息参数和方法名称

c# - 获取 EXIF 方向标签,旋转到正确的方向,处理图像并以正确的方向保存图像

c# - 反序列化其中包含引号和撇号的 JSON 字符串

c# - 标签的位置在 xp 和 7 中不同