Threading

  • Thread starter Thread starter Brian P. Hammer
  • Start date Start date
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
 
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
 
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
 
Back
Top