I think you get error because inside of USP_Select_Registration
you trying to read values from form control's. Which was created by another thread.
RunWorkComleted
executes in the same thread where Backgroundworker
was created, thats why code P_Panel.Visible = False
will executes normally.
But DoWork
executes on the another thread.
And when you tried to access some forms controls to read values
TXT_Search.Text
- it raise error
You can pass you searching parameters to BackgroundWorker
, but you need to add parameters to USP_Select_Registration
function.
For example:
Private Sub USP_Select_Registration(searchText as String)
'Your code here
End Sub
Then where you start BackgroundWorker
:
B_Worker.RunWorkerAsync(TXT_Search.Text)
And
Private Sub B_Worker_DoWork(ByVal sender As Object,
ByVal e As DoWorkEventArgs) Handles B_Worker.DoWork
Threading.Thread.Sleep(25)
USP_Select_Registration(e.Argument)
End Sub
In your case you need to pass more then one parameter.
So you can create some structure/class or whatever object where you can keep all needed values. And pass that object to RunWorkerAsync