[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

get function return from asynchronous background worker

TheVillageCodingIdiot

7/2/2008 7:37:00 PM

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







2 Answers

TheVillageCodingIdiot

7/2/2008 8:53:00 PM

0

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.

Peter Duniho

7/3/2008 3:31:00 AM

0

On Wed, 02 Jul 2008 12:36:34 -0700, TheVillageCodingIdiot
<whosyodaddy1019@hotmail.com> wrote:

> 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?

See Control.Invoke(). Alternatively, you could handle the
RunWorkerCompleted event for the BackgroundWorker, allowing you to return
the connection instance from your DoWork handler and retrieve it from the
RunWorkerCompleted handler.

Pete