Please help: C# PING issue

Trying to call following in but sit on AutoEventReset doesnt move however if I call message box before waiter.WaitOne(); it works. Can anyone please tell me why?


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?


// 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)
// 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)


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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!