Link to home
Start Free TrialLog in
Avatar of MichaelCohoon
MichaelCohoon

asked on

Question on using C# IsDestinationReachable function

Hi, I am an SMS administrator for our Company. It seems on a daily basis I have to connect to computers to fix something related to SMS. Well we have a large remote user base and frequently those machines are not connected when I need to do something to them. So I suppose I could try to remember to ping them throughout the day or set up a ping -t command, but that seemed wasteful. So as a beginning c# hobbyist (won't say programmer just yet), I broke out my Visual C# Express 2005 and thought how could I automate this task of finding out when a computer comes online? So I found out about the function IsDestinationReachable using a DLL Import within c#.

I set up a window to ask for a computer name, then pass that computer name to the next window that does the actual checking. If it does not reach the given machine, it sleeps for 30 seconds then tries again. Once the machine is reached, the window pops up on the display and an audible tone is also generated.

Well this seems to work for most machines. However, I have seen this app tell me a machine is now online when it is sitting right next to me, turned off, and unplugged from the network. If I try to ping the machine, I get no reply, as I would expect.

I have listed my code below. Can anyone offer some thoughts, hints, etc.?
-------------------------------------------------------------------------------------------------------
        private void frmWaitForActive_Load(object sender, EventArgs e)
        {
            //set window title and hide window.
            Text = "Is " + strCompName + " online?";
            this.Hide();

            /*This section of code will perform the actual test of
             * determining whether the indicated computer is reachable.
             *
             * A boolean variable is initially set to false, then the
             * function called IsDestinationReachable is run using the
             * supplied computer name as the host.
             *
             * If the host is reachable, then a window will display
             * for the user indicating such, as well as a tone will sound
             * as an audible indicator.
             *
             * If the host is not found, then the program sleeps
             * for 30 seconds (30000 milliseconds) and the process
             * is then repeated to check if the host is reachable.
             */
            bool blnFlag = false;
            while (blnFlag == false)
            {      
                if (IsDestinationReachable(strCompName, IntPtr.Zero))
                {
                    //the host is reachable. Now notify the administrator
                    WindowState = FormWindowState.Normal;
                    txtCompName.Text = strCompName + " is now on-line!";
                    this.Show();
                    blnFlag = true;
                    Beep(500, 550);
                }
                else
                {
                    //Sleep 30 seconds (30000 milliseconds)
                    Thread.Sleep(30000);
                }
            }
        }


Thanks for any ideas.
Michael
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelCohoon
MichaelCohoon

ASKER

Bob, I think I can do the code to start a ping command, using the Process.Start, but I really don't have a clue how I would be able to determine if I receive a response or not via code. Would that check be something easily done?

Michael
Bob, you gave me an idea to go the way of Ping. So a search on google for "C# ping status" turned up code for me. .Net 2.0 now includes network functions that are relatively easy to use, including a ping. So I will go that route with my program.

I thank you for the idea.
Michael