-3

I have a windows form application which uses a local SQL server on LAN. Some parts of this windows application includes running some SELECT queries on SQL, but every time i run a query through my c# windows application, it will freeze and wait for DB answer. what i want to know is how can i have a system to handle my DB job on a different CPU or etc... so my application won't freeze and wait for DB to reply, something like the google search engine where you type and get results simultaneously.

I'm really new to C# so please answer in a way I can understand.

jazb
  • 5,498
  • 6
  • 37
  • 44
Vahid.M
  • 13
  • 4
  • 1
    there might be some benefit in showing us the code that connects to the db and the way you are querying for records. without this we can only guess what you are doing wrong – jazb Oct 17 '18 at 06:33
  • What kind of select query are you doing that makes a computer to freeze. You either have issues with your code or your hardware performance – Maytham Fahmi Oct 17 '18 at 06:36
  • when i say freeze, i only mean that i can no longer press buttons. – Vahid.M Oct 17 '18 at 06:42
  • @Vahid.M post your code. Although I suspect you should look for eg `ExecuteScalarAsync` and async/await, not parallel execution. There are a lot of duplicate questions. If you want help with your code, post it here – Panagiotis Kanavos Oct 17 '18 at 07:01

1 Answers1

1

Application get freeze since you are executing your DB calls from UI thread. You should run time-consuming code (DB calls, API calls etc) in a separate thread.

You can use Asynchronous Programming with Async and Await to achieve this.

Please read more on Asynchronous Programming and this tutorial will explain how to update UI when using Async-Await.

private async void ButtonClickHandlerAsync(object sender, EventArgs e)
{
    await Task.Run(() =>
    {
        you can do your time-consuming tasks here;
    });
}

If you can post a sample code segment with your question, it will help to give a more accurate answer.

Sampath
  • 1,173
  • 18
  • 29