Threading

B

Brian P. Hammer

All - I have the need to make my app multi threaded for those long running tasks. Some of them are not a problem but the question I have is with SQL and populating items on a form. If I create a new thread, connect to my SQL, run a long query and then populate a combobox within this new thread; I am guessing I should not do this. Somewhere I read that items created on one thread should not be manipulated on another.

How does one go about doing this? I have read through the help and examples but didn't see anything that jumped out as an answer.

Thanks,
Brian P. Hammer
 
C

Christopher Kimbell

You are correct. GUI elements can only be accessed on the GUI thread. What you have to do is to execute code on the UI thread from some other thread. Each control has a InvokeRequired property and Invoke method, the former indicating if the latter must be called.

Here is an article that describes the problem with code:

http://weblogs.asp.net/justin_rogers/articles/126345.aspx


Chris


All - I have the need to make my app multi threaded for those long running tasks. Some of them are not a problem but the question I have is with SQL and populating items on a form. If I create a new thread, connect to my SQL, run a long query and then populate a combobox within this new thread; I am guessing I should not do this. Somewhere I read that items created on one thread should not be manipulated on another.

How does one go about doing this? I have read through the help and examples but didn't see anything that jumped out as an answer.

Thanks,
Brian P. Hammer
 
B

Brian P. Hammer

Chris - Thanks for the pointer. Sorry, but I am kind of new to this whole
concept. If I call from my main thread:

Dim dlgt As New AsyncDelegate(AddressOf ManufacturerData)
Dim ar As IAsyncResult = dlgt.BeginInvoke(Nothing, Nothing)


Private Function ManufacturerData() as String 'The new thread
'Do my SQL connection here
'....
'....
'....
Do While drSQL.Read()
objManufacturerItem = New
ListItem(drSQL.Item("ManufacturerName").ToString(), _
CInt(drSQL.Item("IDManufacturer")))
'Cross thread issue here
Me.cboManufacturer.Items.Add(objManufacturerItem)
Loop
'....
'....
End Sub


Is this doing what I need?


Thanks,
Brian P. Hammer


You are correct. GUI elements can only be accessed on the GUI thread. What
you have to do is to execute code on the UI thread from some other thread.
Each control has a InvokeRequired property and Invoke method, the former
indicating if the latter must be called.

Here is an article that describes the problem with code:

http://weblogs.asp.net/justin_rogers/articles/126345.aspx


Chris


All - I have the need to make my app multi threaded for those long running
tasks. Some of them are not a problem but the question I have is with SQL
and populating items on a form. If I create a new thread, connect to my SQL,
run a long query and then populate a combobox within this new thread; I am
guessing I should not do this. Somewhere I read that items created on one
thread should not be manipulated on another.

How does one go about doing this? I have read through the help and examples
but didn't see anything that jumped out as an answer.

Thanks,
Brian P. Hammer
 

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

Top