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.
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.