Jenny Mchammer
Jenny Mchammer

Reputation: 95

Why i'm getting error No overload for ... matches delegate?

In form1 i have a method 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);
                    }
                })
            );
        }

Then i call this method in two places in form1 both in buttons click events:

DoRequest(ScreenshotRequest.DannysCommands.Displayoverlays);

The error i get is in this method in 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
            {
            }
        }

The error is on : new ThreadStart(DoRequest)

Error 1 No overload for 'DoRequest' matches delegate 'System.Threading.ThreadStart'

How can i solve the error ?

Upvotes: 2

Views: 13732

Answers (2)

matthewrdev
matthewrdev

Reputation: 12190

The ThreadStart constructor expects a delegate that returns void and takes no arguments. The error Error 1 No overload for 'DoRequest' matches delegate 'System.Threading.ThreadStart' indictates that the method signature for DoRequest does not match the signature defined by a ThreadStart delegate. It'd be like you passing a string into a method that needed a double.

Consider using a ParameterizedThreadStart instead:

Thread t = new Thread(new ParameterizedThreadStart(DoRequest));
t.Start(ScreenshotRequest.DannysCommands.Displayoverlays);

And then edit your DoRequest method to expect an object that you can then cast:

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

Upvotes: 5

Joy Hyuk Lee
Joy Hyuk Lee

Reputation: 776

You have a parameter in your DoRequest.

so you need ParameterizedThreadStart

http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx

http://www.dotnetperls.com/parameterizedthreadstart

and you need to set parameter type to Object

void DoRequest(object param)
{
YourType variable = (YourType)param;
// Something...

}

Upvotes: 1

Related Questions