views:

211

answers:

1

I am new to programing and can not find or know what to search for to debug the thread that is started with the SendAsync Method. The code works good using the Send Method but when using SendAsync it goes to waiter.WaitOne() but i never get the callback (I think thats what its called) to myPing_PingCompleted. So two questions how do I debug the code when it starts a new thread. I am using C# Express so it may not have all the debuging tools as VS. and any idea where I am going wrong in my code. Thanks

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using System.Net;

private void btnPingAsync_Click(object sender, EventArgs e)
    {
        string bIP = txtStartIP.Text;
        string eIP = txtEndIP.Text;
        int timeOut;
        int cnt = 0;
        if (eIP == null) eIP = bIP;
        Ping myPing = new Ping();
        PingOptions parmPing = new PingOptions();
        AutoResetEvent waiter = new AutoResetEvent(false);
        myPing.PingCompleted +=new PingCompletedEventHandler(myPing_PingCompleted);
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] dataBuffer = Encoding.ASCII.GetBytes(data);
        if (!int.TryParse(txtTimeOut.Text, out timeOut)) timeOut = 120;
        parmPing.DontFragment = true;
        parmPing.Ttl = 32;
        pbQueueStatus.Minimum = 0;
        pbQueueStatus.Step = 10;
        pbQueueStatus.Value = 0;
        pbQueueStatus.Style = ProgressBarStyle.Continuous;




        if (verify.ValidIPAddress(bIP) && verify.ValidIPAddress(eIP))
        {
            IPQueue = build.IPAddressQueue(bIP, eIP);
            pbQueueStatus.Maximum = IPQueue.Count;
            pbQueueStatus.TopLevelControl.UseWaitCursor= true;
            pbQueueStatus.Visible = true;
            while (IPQueue.Count > 0)
            {
                myPing.SendAsync(IPQueue.Dequeue(), timeOut, dataBuffer, parmPing, waiter);
                waiter.WaitOne();
                if (++cnt > 10)
                {
                    pbQueueStatus.PerformStep();
                    cnt = 0;
                }
            }
        }
    }

    private void myPing_PingCompleted(Object sender, PingCompletedEventArgs e)
    {

        PingReply reply = e.Reply;
        ((AutoResetEvent)e.UserState).Set();
        if (reply .Status == IPStatus .Success )
        {
            dosomething;
        }
A: 

I'm assuming that you are putting a breakpoinit in the myPing_PingCompleted method but that when in debug mode its just not going there. Is that right?

Is the code throwing some sort of error? If you step through the code, does it call the myPing.SendAsync with the right parameters?

I just tried your code (without the IPQueue because that looks like your custom class). It works fine on my end. I used one valid IP and what IP that doesn't exist. It worked good in both cases.

Edit for new information

Ok i just tried it in a windows form application and IT DOES NOT WORK. When i tried it before it was in a unit test. Basically it would seem that the thread used to render the windows form and handle the events isn't capable of being used to create async requests (maybe because it is a foreground thread). But, you can get arround it pretty easily by creating another thread to do the ping.

Actually, ideally that's how you would have to do it anyways. In order for the windows form app not to lock up when the thread is busy, a good principle is to do all backround work on a seperate thread. This would keep the windows form responsive. Be careful though, when trying to access Controls using backround threads an exception will be thrown. It is best to read all the values you want into private variables and then kick of a thread that will do all the work, have that thread update another set of variables and then have the forground thread read the variables and update the controls.

Mark
Yes I have a breakpoint in the first statement in the myPing_Ping Completed method. When stepping thru the program the last statement to execute is the waiter.WaitOne() method the form will comeback up but is not being completely redrawn and then nothing. This is happening on two different computers.Could I be missing a reference? I have added the references that I have to the code sample in the question.
DicksGarage
Also I tried putting just a IP address in in place of IPQueue and the same thing still happens.
DicksGarage
Thanks that worked good.
DicksGarage