在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'没有重载匹配代理'System.Threading.ThreadStart'
我该如何解决错误?
最佳答案
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);
}
})
);
}