c# - 为什么我收到错误...匹配委托(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
            {
            }
        }

错误出现:新 ThreadStart(DoRequest)

错误 1“DoRequest”没有重载匹配委托(delegate)“System.Threading.ThreadStart”

我该如何解决这个错误?

最佳答案

ThreadStart构造函数需要一个返回 void 且不带参数的委托(delegate)。错误 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);
            }
        })
    );
}

关于c# - 为什么我收到错误...匹配委托(delegate)没有重载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831599/

相关文章:

c# - 仅返回类型不同的 C++ 多个接口(interface)?

c# - 在以x86为目标的docker中运行.NET Core项目会抛出System.BadImageFormatException

c# - HTML敏捷无法获得结果

c# - 以编程方式单击按钮 C#

c# - Excel 数据到 TreeView C#

c# - Convert.ToInt16和(Int16)有什么区别

c# - 如何在UWP中按PathGeometry进行裁剪?

c# - 以编程方式更改某些 Windows 用户名(重命名 Windows 用户)

c# - SerialPort.Write(byte [],int,int)的文档是否会引起误解?

c# - 在 .Net Core 类库中使用 Windows 窗体 - .NET Core 控件库