Thread-Safe Calls to Windows Forms Controls


Dion Heskett


I get a InvalidOperationException message when I click button2 at this line
of code textBox1.Text = whoIs1.EndLookup( asyncResult );

Should I now be implementing the BackgroundWorker Class now?

Many thanks


private void button2_Click(object sender, System.EventArgs e)


whoIs1.WhoIsServer = comboBox1.Text;

whoIs1.Query = textBox3.Text;

whoIs1.Port = Convert.ToInt32(textBox4.Text);

// using callback

whoIs1.BeginLookup(textBox3.Text, new AsyncCallback( OurCallback ), null);

MessageBox.Show("Doing other stuff." + "\r\n" ,"Viascape WhoIs",
MessageBoxButtons.OK, MessageBoxIcon.Error);


private void OurCallback ( IAsyncResult asyncResult )


textBox1.Text = whoIs1.EndLookup( asyncResult );



Whois Class

/// <summary>

/// Begins an asynchronous lookup operation for WhoIs information.

/// </summary>

/// <param name="query">A string containing the WhoIs host name to

/// <param name="callback">

/// The callback method to be called when the asynchronous operation is

/// </param>

/// <param name="state">The state object.</param>

/// <returns>

/// An <see cref="IAsyncResult"/> instance that references the asynchronous

/// </returns>

/// <remarks>

/// The BeginLookup method starts an asynchronous request for WhoIs host

/// The asynchronous callback method uses the <see cref="EndLookup"/> method
to return the

/// actual WhoIs information.

/// </remarks>

public IAsyncResult BeginLookup(string query, System.AsyncCallback callback,
object state )


// Create an instance of a delegate that wraps Lookup method

LookupDelegate dlgt = new LookupDelegate (this.Lookup);

// Initiate the Asynchronous call passing in the callback delegate

// and the delegate object used to initiate the call

return dlgt.BeginInvoke(query, callback, state);

//IAsyncResult asyncResult = dlgt.BeginInvoke(query, callback, state);

//return asyncResult;


/// <summary>

/// Ends an asynchronous request for WhoIs information.

/// </summary>

/// <param name="asyncResult">The pending request for WhoIs

/// <returns>The results of the asynchronous lookup containing the WhoIs

/// <remarks>

/// The <b>EndLookup</b> method completes an asynchronous request for WhoIs
information that was

/// started with a call to <see cref="BeginLookup"/>.

/// </remarks>

public string EndLookup(IAsyncResult asyncResult)


// Extract the delegate from the AsyncResult

LookupDelegate dlgt =

//AsyncResult aResult = (AsyncResult)asyncResult;

//LookupDelegate dlgt = (LookupDelegate)aResult.AsyncDelegate;

// Retrieve the results of the asynchronous call

return dlgt.EndInvoke(asyncResult);




Nicholas Paldino [.NET/C# MVP]


You could use the BackgroundWorker class, but it would be easier to just
use the Invoke method on the control to make the call on the right thread.
The Invoke method takes a delegate, and makes sure that it is called on the
thread that created the control Invoke was called on.

Hope this helps.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question