Running SQL in a Thread
-
I have a Windows Forms app in which I execute a SQL statement using SQLDataAdapter. I want the user to be able to kill the SQL statement if it seems to take to long. My thought is to execute the SQL in its own thread. Then when the user clicks a Cancel button it just stops the Thread. Is this the best way to do this? Will it work? Is is inefficient? Thanks, Mark Mokris
-
I have a Windows Forms app in which I execute a SQL statement using SQLDataAdapter. I want the user to be able to kill the SQL statement if it seems to take to long. My thought is to execute the SQL in its own thread. Then when the user clicks a Cancel button it just stops the Thread. Is this the best way to do this? Will it work? Is is inefficient? Thanks, Mark Mokris
Use the SqlDataAdapter in a thread, as you suggested, but I would add a twist. Instead of calling Thread.Abort() to terminate the thread, have your thread read small chunks of data at a time, and check for a flag telling it to stop between reads. Doing it this way, you could even have a progress bar in your main window showing the progress to the user. -------- "I say no to drugs, but they don't listen." - Marilyn Manson
-
I have a Windows Forms app in which I execute a SQL statement using SQLDataAdapter. I want the user to be able to kill the SQL statement if it seems to take to long. My thought is to execute the SQL in its own thread. Then when the user clicks a Cancel button it just stops the Thread. Is this the best way to do this? Will it work? Is is inefficient? Thanks, Mark Mokris
As someone suggested to me on here yesterday, you might want to look into System.ComponentModel.BackgroundWorker as an alternative. I haven't used it myself, I stuck to threads because it's what I know much more of anyway but it might be worth looking into yourself.