Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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 9-May-12 21:52pm
Edited 9-May-12 22:18pm
v3
Comments
E.F. Nijboer at 10-May-12 4:20am
   
Just some keywords that, with addition of some google, can help you out: "producer consumer" and "thread invoke".
JimRaj at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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 => {
  // 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[^].
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Comments
Pete O'Hanlon at 30-May-12 7:06am
   
Let me correct that 1 vote.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Comments
Pete O'Hanlon at 30-May-12 7:07am
   
Let me correct that 1 vote.
Laxmikant_Yadav at 3-Jun-12 14:20pm
   
Thank You Pete :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Pete O'Hanlon at 30-May-12 7:07am
   
Let me correct that 1 vote.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Check my multi-threading article
 
Simple mutlithreaded application[^]
  Permalink  
Comments
Pete O'Hanlon at 30-May-12 7:07am
   
Let me correct that 1 vote.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 566
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 30 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100