Results 1 to 6 of 6

Thread: [2.0] Asynchronous Programming

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    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
    Last edited by 2MuchRiceMakesMeSick; Oct 22nd, 2006 at 12:40 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: [2.0] Asynchronous Programming

    anyone?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width