By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,473 Members | 3,292 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,473 IT Pros & Developers. It's quick & easy.

Need help with async callback

P: n/a
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();

}
}

Nov 2 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 2007-11-01 16:56:46 -0700, bi*********@yahoo.com said:
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

Nov 2 '07 #2

P: n/a
Thanks, Pete. I have everything working now.

On Nov 1, 6:26 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
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

Nov 2 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.