Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi... Friends

can any one help me how to use Threads in My condition .

Q.1 I have to insert values in table from .net on some condition like where Name = 'John'
The name will be type by user in textbox. Now i want to know how to get text from textbox while using thread. I want to use one thread for inserting because if there is large amount of rows, then sql take 30 seconds.

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.

Q.3 After retrieving data i want to show it in datagridview. but i want to arrange the numeric values towards right side of datagridview and amount which is less than 10 should be highlited by red colour. This would be done in seperate thread
, because it also take lot of time if data is in large amount.

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();


after starting thr1 it initialize thr2 thread and starts thr2 before completing work assigned to thr1.

Q5 i have started Progress bar on button Click. Now How to set Progress bar marquespeed =0. should i have to again check Progress bar's invokerequired. if yes then how i will stop it using Invoke of Form.

Please help me i have stuck to it from last 3 days..
Please help.....

If any one want my code then i can mail my code. Please help me.
Posted
Updated 9-May-12 22:18pm
v3
Comments
E.F. Nijboer 10-May-12 4:20am    
Just some keywords that, with addition of some google, can help you out: "producer consumer" and "thread invoke".
Jim Jos 16-May-12 3:17am    
Please let me know if you need a solution still? We could work this together.. One question I have is Insertion and retreival should take place at the same time? Or Retreival could take place only after insertion..

Check my multi-threading article

Simple mutlithreaded application[^]
 
Share this answer
 
Comments
Pete O'Hanlon 30-May-12 7:07am    
Let me correct that 1 vote.
 
Share this answer
 
Comments
Pete O'Hanlon 30-May-12 7:07am    
Let me correct that 1 vote.
 
Share this answer
 
Comments
Pete O'Hanlon 30-May-12 7:07am    
Let me correct that 1 vote.
LaxmikantYadav 3-Jun-12 14:20pm    
Thank You Pete :)
 
Share this answer
 
Comments
Pete O'Hanlon 30-May-12 7:06am    
Let me correct that 1 vote.
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:
C#
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken = tokenSource.Token;

Task<MyData> dataTask = Task.Factory.StartNew<MyData>(
  (data) => return FillMyData();}, token, TaskCreationOptions.LongRunning);

dataTask.ContinueWith(ant => {
  // Bind the data that's in ant.Result to your grid here..
  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[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900