473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Thread-Safe Calls to Windows Forms Controls

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
2 2136
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
3
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.