[2.0] Asynchronous Programming
I am trying to write a function that will be contained in a DLL that will report back to the main program window once something is done. I do not want to loop or use a timer to constanly call a function because it will use way too much resources.
I think that i need a call back function inside the dll but im unsure how exactly it will work. Can someone paste some code showing how I go about doing this please?
Re: [2.0] Asynchronous Programming
Take a look at the help topic for the IAsyncResult Interface for some more information. It includes links to related topics and some code examples.
Re: [2.0] Asynchronous Programming
Thanks for the reply. Im trying to write a ping program but the program locks up when it is awating a response from the server. Is the IAsyncResult the way to solve this from locking up?
I have been reading for a while but I still do not understand how this particular code would be intergrated into my program
This is the simplest breakdown of the code in my program
Code:
IPHostEntry ipEntry = Dns.GetHostEntry("www.anywhere.com");
IPAddress[] ip = ipEntry.AddressList;
PingReply reply = ping.Send(ip[0].ToString());
I found the msdn documentation on the iasync but I still dont see how it would work in my particular case.
http://msdn2.microsoft.com/en-us/lib...yncresult.aspx
Re: [2.0] Asynchronous Programming
so far this is what I have but it does not work
Code:
public delegate int PingResultsDelegate(int i);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void cmdPing_Click(object sender, EventArgs e)
{
int callResult = 0;
Ping ping = new Ping();
IPHostEntry ipEntry = Dns.GetHostEntry(www.anywhere.com);
IPAddress[] ip = ipEntry.AddressList;
PingReply reply = ping.Send(ip[0].ToString());
if (reply.Status != IPStatus.Success)
{
}
PingResultsDelegate pingDelegate = new PingResultsDelegate(MyCallback);
IAsyncResult aResult = pingDelegate.BeginInvoke(2, MyCallback, null);
aResult.AsyncWaitHandle.WaitOne();
callResult = pingDelegate.EndInvoke(aResult);
}
public static void MyCallback(IAsyncResult ar)
{
int value = Convert.ToInt32(ar.AsyncState);
AsyncResult aResult = (AsyncResult)ar;
}
}
Re: [2.0] Asynchronous Programming
Re: [2.0] Asynchronous Programming
Isn't that call to WaitOne defeating the purpose of calling an asynchronous method? If you call an asynchronous method and then block until it completes then why call the ansynchronous method in the first place? That's merely an intuitive analysis, given that I've never done any coding using asynchronous methods explicitly myself. Try the link below for more information.
http://msdn2.microsoft.com/en-us/library/ms228969.aspx