在 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/