Get combobox values during threading

S

Shirim9

Hi All,

I have a question about getting the combobox values
(combobox.selecteditem) during threading.

There is a Sub that does alot of calculations as below and i know if i
put maxnum = combobox.selecteditem, there will be cross-thread
problem. I don't quite know how to get the selected values in the
combobox. I have tried to search around for some clues and found it
may have to use BeginInvoke and EndInvoke? However i'm not exactly
sure whether this is right way and how to use it. If anyone can kindly
show an actual implementation of how to get the values.

Thanks a lot


Btw, below is just a illustration that i was trying to do.

Private Sub backgroundSub()
Dim i, maxnum as integer
maxnum = Cint(combobox.selecteditem) ' This will cause cross-thread
problem and i don't know how to solve
for i = 1 to maxnum
'Do calculations here
Next
End Sub

'Sub runs when user click this button
Private Sub Button1_Click()
Dim u As New System.Threading.Thread(AddressOf backgroundSub)
u.Start()
'Do other things
End Sub
 
J

Josip Medved

There is a Sub that does alot of calculations as below and i know if i
put maxnum = combobox.selecteditem, there will be cross-thread
problem. I don't quite know how to get the selected values in the

You need to perform that call before you get into thread. This may be
easiest using BackgroundWorker for your computation instead.

Something like:

Private Sub Button1_Click
BackgroundWorker1.RunWorkerAsync(ComboBox1.SelectedItem)
'Do other things
End Sub

Private Sub BackgroundWorker1_DoWork
Dim i, maxnum As Integer
maxnum = CInt(e.Argument)
For i = 1 To maxnum
'Do calculations here
Next
End Sub
 
S

Shirim9

Hi Josip,

Thanks a lot for your help, i just tried it out and it worked. Just
wondering what happens if there are more than 1 combobox (eg:
CmoboBox1.selecteditem, ComboBox2.selecteditem, ...). I know i can
create an array and put all these combobox values into this array (eg:
BackgroundWorker1.RunWorkerAsyn(ComboBoxArray) Is this what you'll do
too Or ...?

Thanks again
 
J

Josip Medved

Thanks a lot for your help, i just tried it out and it worked. Just
wondering what happens if there are more than 1 combobox (eg:
CmoboBox1.selecteditem, ComboBox2.selecteditem, ...). I know i can
create an array and put all these combobox values into this array (eg:
BackgroundWorker1.RunWorkerAsyn(ComboBoxArray) Is this what you'll do
too Or ...?

Usually I do just that if I need multiples of same object type.
If I need few different ones, I often create new class/structure for
passing that kind of parameter.
 

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