Q.2 How to retrieve data from table using another thread. I mean to say after retrieving while providing it to datagridview should i check Datagridview's Invokerequired property.
Rather than muck about like this, take advantage of the Task Parallel Library. Suppose you have a class called MyData which you will populate with the data that you are going to display, do something like this:
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken = tokenSource.Token;
Task<MyData> dataTask = Task.Factory.StartNew<MyData>(
(data) => return FillMyData();}, token, TaskCreationOptions.LongRunning);
dataTask.ContinueWith(ant => {
dgvMyGrid.DataSource = ant.Result;
dgvMyGrid.DataBind();
}, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
This creates a task that retrieves the data, and then uses a continuation to update the grid. The clever part here is the TaskScheduler bit because that tells the code to put the data back onto the calling thread.
Q4 Tell me how to call these threads one by one. If I initialize thread1 and start it and i initialize second thread below it and start it then it execute second thread before completing first work as shown below
Thread thr1 = new Thread (new ThreadStart(this.insertingValues));
thr1.start();
Thread thr2 = new Thread (new ThreadStart(this.Retreving));
thr2.start();
This defeats the purpose of having a second thread. If you need that second piece of work to start only after the first has finished, then make that part of the first thread.
For more information about the Task Parallel Library, Sacha Barber wrote an excellent series
here[
^].