By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,398 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

Thread-Safe Calls to Windows Forms Controls

P: n/a
Hi

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

Dion

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
query.</param>

/// <param name="callback">

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

/// </param>

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

/// <returns>

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

/// </returns>

/// <remarks>

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

/// 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
information.</param>

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

/// <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 =
(LookupDelegate)((AsyncResult)asyncResult).AsyncDe legate;
//AsyncResult aResult = (AsyncResult)asyncResult;

//LookupDelegate dlgt = (LookupDelegate)aResult.AsyncDelegate;
// Retrieve the results of the asynchronous call

return dlgt.EndInvoke(asyncResult);

}




Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Dion,

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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dion Heskett" <no****@here.thankyou> wrote in message
news:uY**************@TK2MSFTNGP12.phx.gbl...
Hi

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

Dion

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
query.</param>

/// <param name="callback">

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

/// </param>

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

/// <returns>

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

/// </returns>

/// <remarks>

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

/// 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
information.</param>

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

/// <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 =
(LookupDelegate)((AsyncResult)asyncResult).AsyncDe legate;
//AsyncResult aResult = (AsyncResult)asyncResult;

//LookupDelegate dlgt = (LookupDelegate)aResult.AsyncDelegate;
// Retrieve the results of the asynchronous call

return dlgt.EndInvoke(asyncResult);

}



Nov 17 '05 #2

P: n/a
You could use BackgroundWorker to keep the UI responsive. Just be sure
to use Control.Invoke or Control.BeginInvoke to display the results on
the form.

Regards,
Thi - http://thith.blogspot.com

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.