473,508 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with async callback

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
2 2059
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7115
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
1
6566
by: Mark Smith | last post by:
Hi , Is it possible in .Net to define a async timer callback. What I am wanting to do is declare a time-out condition that gets executed should the something timeout. The reason I want to do...
4
2943
by: Brett Robichaud | last post by:
I'm using an async delegate to spawn a thread and run some code. I also specify an AsyncCallback to get notified when the thread completes. The problem is that my callback is being called twice...
6
1813
by: TS | last post by:
Im in a web page and call an asynchronous method in business class. the call back method is in the web page. When page processes, it runs thru code begins invoking the method then the page...
11
1744
by: Glen Wolinsky | last post by:
This is my first attempt as asynchronous processing. I have created a small test app as proof of concept, but I am having one proglem. In the example (code listed below), my callback routine has...
2
1231
by: ITALstudio s.r.l. | last post by:
Hy, I make an async call, using delegate and BeginInvoke. When the async call is completed, the callback procedure is executed two times. I debugged the code and I'm sure: I make one async...
2
3399
by: Anand | last post by:
Hi Season Greetings, I'm trying to implement client call backs in my page. i'm using user controls (composite controls) and these are placed in my master page. I'm implementing...
0
2068
by: whizpop | last post by:
Hi, First of all, thanks for a great starter kit, now If I could just get it to work (fully). I am trying to compile and run the solution/services all on a local dev box. I am able to...
1
4433
by: Demi | last post by:
I'm trying to use an Async="true" page to do an async HttpWebRequest. My code is based on the MSDN example: http://msdn2.microsoft.com/en-us/library/21k58ta7.aspx The problem I'm having is...
8
8899
by: Chizl | last post by:
I'm building a web server and having some issues with the TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using MS Web Stress Tool to test against my web server and when I...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.