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