I created a background thread that get's data and returns it to the main thread; this works. Now I want to be able to stop the SQL request from the main thread (probably on a button click).
This is my test code for creating the thread (working):
Private Sub GetSql()
If (Me.InvokeRequired) Then
Me.Invoke(New GetSqlDelegate(AddressOf GetSql))
Else
Dim da As SqlDataAdapter = New SqlDataAdapter("select * from database", _
New SqlConnection(connectionString))
dt = New DataTable
da.Fill(dt)
Me.BeginInvoke(New BindDataToGridDelegate(AddressOf BindDataToGrid))
End If
End Sub
Private Delegate Sub BindDataToGridDelegate()
Private Sub BindDataToGrid()
DataGridView1.DataSource = dt
dt = Nothing
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
thrGetData = New Thread(AddressOf ThreadBackgroundData)
thrGetData.IsBackground = True
thrGetData.Start()
End Sub
How can I access the background thread to stop the query on demand?
Do I need to set a flag on the main thread telling the background thread to stop running then have the background thread poll the main thread at intervals?
Any help would be appreciated. I was trying to look for an example but I wasn't able to find a good one. Even pseudo code would help
Thanks.