get function return from asynchronous background worker

  • Thread starter TheVillageCodingIdiot
  • Start date
T

TheVillageCodingIdiot

on my form I have a function that returns a SQL connection depending
on what is selected in a combo box. Once a button is clicked it starts
a backgroundworker thread and Im trying to call the function on the
form to get the SQL connection but getting error that its being
accessed outside the original thread. How do I go about getting the
returned SQL connection into the background worker thread?

example
///////////////Form1.VB//////////////////////////////////
Sub Button1_Click(sender as object, e as eventargs) handles
Button1.Click
Worker = new backgroundworker
Worker.RunWorkerAsync
End Sub

Sub Worker_DoWork(sender as object, e as DoWorkEventArgs) handles
worker.DoWork
Dim class1 as new myClass

class1.Start()
End Sub

Function GetSQLConn() as SQLConnection
Select case combo1.selectedindex
case 0
return sql1
case 1
return sql2
end case
End Function
////////////MyClass.VB////////////////////////////////////////////
Class myClass
Sub Start()
dim comm as SQLConnection = form1.GetSQLConn()
End Sub
End Class
 
T

TheVillageCodingIdiot

well I kinda have a work around but would like to find a better way.
Right now I have the SQL Connection as a gobal variable on the form
and created a sub in the class that I give it the object. I would like
to find a way of this being a little more better. Dont like the idea
of having a gobal variable. Would much rather use the connection,
close it, and dispose of the object.
 

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