Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7683695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:51:13+00:00 2026-05-31T18:51:13+00:00

I created a background thread that get’s data and returns it to the main

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:51:14+00:00Added an answer on May 31, 2026 at 6:51 pm

    DataAdapter.Fill method is synchronous, meaning it blocks current thread until it is completed. Basically, that means you can’t add polling/checking in the background thread since everything it can do is execute Fill method (which may be time-consuming).

    If your problem is stopping the operation itself (please note that no data may be returned), you should just call thread.Abort() from your main thread. For that you’d need to save thrGetData as class-level variable. More about Thread.Abort() may be found here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a background thread that's owned by an object. When that
I'm trying to have a class that when created, starts a background thread, similar
I have a function on a background thread that looks like this: NSMutableArray *descriptions
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I have created a button CSS class that uses background images with different positions
I have created a table in my application, I want to select (change background
I have a process I run periodically on a background thread that receives changes
I am running a background worker thread that takes a long time to run.
I have a method that is being executed on a background thread. From that
My application has a message-only window that is launched from a newly created thread.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.