Need help with async callback

B

billsahiker

I wrote a test program to help understand asynchronous calls in c#. I
have a working VB.NET app that uses a similar technique but I cannot
get it to work in c#. The errors I get and the code below. I
understand the need for threading when updating UI and the general
concept of what is going on but do not understand the two errors.:

Error 1 'testwinform.Form1.WorkDoneDelegate()' must declare a body
because it is not marked abstract or extern C:\Development\testwinform
\testwinform\Form1.cs 20 26 testwinform
Error 2 'testwinform.Worker.AsyncMethodCaller()' must declare a body
because it is not marked abstract or extern C:\Development\testwinform
\testwinform\Worker.cs 21 26 testwinform



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace testwinform
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

public Delegate WorkDoneDelegate();

private void btnStart_Click(object sender, EventArgs e)
{
Worker ad = new Worker();
AsyncMethodCaller caller = new
AsyncMethodCaller(ad.WorkerThread);
IAsyncResult result = caller.BeginInvoke(CallBackMethod);

}

private void CallBackMethod(IAsyncResult result)
{
AsyncMethodCaller caller =
AsyncMethodCaller(result.AsyncState);
string returnvalue = caller.EndInvoke(result);
if (this.lblMessage.InvokeRequired)
{
WorkDoneDelegate wd = new
WorkDoneDelegate(CallBackMethod);
this.Invoke(wd, new object()(returnvalue));
}
else
lblMessage.Text = "Done";
}
}
}

//here is the class with the worker thread
using System;
using System.Collections.Generic;
using System.Text;

namespace testwinform
{
class Worker
{
public Worker()
{
}

public string WorkerThread()
{
Thread.Sleep(10);
return ("Done");
}
//delegate used in asynchronous callback
public Delegate AsyncMethodCaller();

}
}
 
P

Peter Duniho

I wrote a test program to help understand asynchronous calls in c#. I
have a working VB.NET app that uses a similar technique but I cannot
get it to work in c#. The errors I get and the code below. I
understand the need for threading when updating UI and the general
concept of what is going on but do not understand the two errors.:

Error 1 'testwinform.Form1.WorkDoneDelegate()' must declare a body
because it is not marked abstract or extern C:\Development\testwinform
\testwinform\Form1.cs 20 26 testwinform
Error 2 'testwinform.Worker.AsyncMethodCaller()' must declare a body
because it is not marked abstract or extern C:\Development\testwinform
\testwinform\Worker.cs 21 26 testwinform

The error gives you a very strong hint as to what the problem is. The
compiler is interpreting your attempts to define a delegate as though
they are method declarations. So, you should look at the declarations
with that in mind. Why would the compiler think they are method
declarations? Because they are more like a method declaration than a
delegate declaration.

Two problems:

1) There is no return type specified in your declaration for the delegate
2) You are using "Delegate" instead of "delegate" to declare the delegate

All delegate declarations must include a complete method signature, and
no method signature is complete without a return type.

"Delegate" is a type. "delegate" is a C# keyword used to declare a
type that inherits "Delegate".

Because of these issues, the compiler looks at the declaration and
sees: "return type, method name, empty parameter list" and says "ah, a
method declaration...but, there's no body!" Thus the error.

Hope that helps.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top