Question on Delegate

  • Thread starter Thread starter Soul
  • Start date Start date
S

Soul

Hi,

I am learning how to use delegate and invoke a method asynchronously, below
are code snippet from my simple "Hello World" example program with one Form
and one Class. I test it on Pocket PC 2002 emulator, but I keep receive
NotSupportedException. Can anyone teach me where did I do wrong?

public class HelloWorld
{
public HelloWorld()
{}

public string SayName( string name )
{
for (int i = 0; i < 10; i++)
Thread.Sleep(100);

return "Hello " + name + ", how are you?";
}
}

public class Form1 : System.Windows.Forms.Form
{
private delegate string DelSayName(string name);
private bool callComplete;
private HelloWorld helloWorld;
private DelSayName d1;
private AsyncCallback asyncCallBack;

public Form1()
{
InitializeComponent();

this.helloWorld = new HelloWorld();
this.d1 = new DelSayName(this.helloWorld.SayName);
this.callComplete = false;
}

private void btnSay_Click(object sender, System.EventArgs e)
{
if (this.textBoxName.Text != String.Empty)
{
this.asyncCallBack = new AsyncCallback(this.onCompleteInvoke);
IAsyncResult result = this.d1.BeginInvoke(this.tbxName.Text, new
AsyncCallback(this.asyncCallBack), null);

EventHandler evtHdlDisplayProgressBar = new
EventHandler(this.DisplayProgressBar);
while (!this.callComplete);
this.Invoke(new EventHandler(this.DisplayProgressBar));

this.callComplete = false;
}
}

private void DisplayProgressBar(object sender, EventArgs e)
{
if (this.progressBarStatus.Value == this.progressBarStatus.Maximum)
this.progressBarStatus.Value = 0;
else
this.progressBarStatus.Value = this.progressBarStatus.Value + 1;
}

private void onCompleteInvoke(IAsyncResult result)
{
this.callComplete = true;
MessageBox.Show(this.d1.EndInvoke(result));
}
}

thank you.
 
BeginInvoke and EndInvoke are not supported in the Compact Framework. Check out this link for differences between full and compact frameworks http://msdn.microsoft.com/library/d...tuv/html/etcondifferenceswithnetframework.asp

I have modified your code to be functional. Pretty much what has to be done is create two threads, one for getting the name and the other for updating the progress bar. When ever you attempt to update the UI you should use Invoke (which you where doing in your old code)

HT
Mark Arteag

private bool callComplete = false
private string retname = ""
private HelloWorld helloWorld
public Form1(

InitializeComponent()

this.helloWorld = new HelloWorld()
this.callComplete = false


private void btnSay_Click(object sender, System.EventArgs e

if (this.textBoxName.Text.Length>0

//Start the SayName Threa
Thread thread = new Thread(new ThreadStart(StartSayName))
thread.Start()

//Start the update progres
Thread updateProgress = new Thread(new ThreadStart(UpdateProgress))
updateProgress.Start()



private void StartSayName(

retname = this.helloWorld.SayName(this.textBoxName.Text)
this.Invoke(new EventHandler(DisplayName))
this.callComplete = true


private void UpdateProgress(

while(!callComplete
this.Invoke(new EventHandler(DisplayProgressBar))


private void DisplayName(object sender, EventArgs e

this.textBoxName.Text = retname
MessageBox.Show(retname)


private void DisplayProgressBar(object sender, EventArgs e

if (this.progressBarStatus.Value == this.progressBarStatus.Maximum
this.progressBarStatus.Value = 0
els
this.progressBarStatus.Value = this.progressBarStatus.Value + 1



public class HelloWorl

public HelloWorld(
{

public string SayName( string name

for (int i = 0; i < 100; i++
Thread.Sleep(100)

return "Hello " + name + ", how are you?"

}
}
 
Back
Top