Skip to content

C# PING issue

chathura123

Asked Jan 31, 2007, 3:25 PM EST1 answer
Share
LinkedInX
Trying to call following in but sit on AutoEventReset doesn’t move however if I call message box before waiter.WaitOne(); it works. Can anyone please tell me why?


Code************************


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Net.NetworkInformation;
using System.Threading;


void Button1Click(object sender, EventArgs e)
{
textBox1.Text =this.PINGaComputer(button1.Text);

}


public string PINGaComputer(string hostname)
{
WhatISmyHostName = hostname;
WhatISmyIPstatus = "NO PING";

AutoResetEvent waiter = new AutoResetEvent (false);

Ping pingSender = new Ping ();

// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 12 seconds for a reply.
int timeout = 5000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);


// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(WhatISmyHostName, timeout, buffer, options, waiter);

//********If I undo the MessageBox.Show comment this works?

//MessageBox.Show(waiter.ToString());

// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
return WhatISmyIPstatus;
// Console.WriteLine(myip);


}

private void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
//MessageBox.Show(e.Reply.Status.ToString());
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
((AutoResetEvent)e.UserState).Set ();
}

// If an error occurred, display the exception to the user.
if (e.Error != null)
{

((AutoResetEvent)e.UserState).Set();
}

PingReply reply = e.Reply;

// DisplayReply (reply);
if (reply.Status == IPStatus.Success)
{
// Console.WriteLine ("Address: {0}", reply.Address.ToString ());
// Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
// Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
// Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
// Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
WhatISmyIPstatus = "CJ "+reply.Address.ToString ();
}

// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I'm having the same problem. I suspect that WaitOne somehow blocks the PingCompletedCallback. The MessageBox kinda does it to me too ... that's because when the message box is active PingCompletedCallback get called before WaitOne method is invoked. I suspect PingCompletedCallback gets blocked because it has something to do with the UI but I can't manage to find out what it is because inside PingCompletedCallback I just update some static variables and I don't handle the UI. So I'd appreciate some pointers.
    0