Please help me how to use threading
-
2012年5月10日 上午 07:55
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 it. Please help me guys..........
Er. Varinder Rai
- 已編輯 Vari0007 2012年5月10日 上午 08:17
所有回覆
-
2012年5月10日 上午 08:32
Hi,
I understood that you wrote a client application (with a propgresbar) and that you want to implement thereading.
The first thing you should check out is the BackgroundWorker. That is an easy way to implement a background activity and you get easy to use events. So it supports reporting the progres and you can get an event when it is done.
If you have 2 things that should happen after each other: Just put it together. There is no need to have 2 threads if one thread should run when the first thread is done.
But now to oyur questions:
Q1: I see no question in here. Just be aware that there are multiple ways to get another thread. One is the Thread class but there are other possibilites e.g. a BackgroundWorker.
Q2: You always have to check the data structures and make sure, that you do not generate race conditions. The data classes of ADO.Net normaly take care of a lot of things already. So you should use these. The DataGridView should be databound to the data itself and it will update itself when required (if the required interfaces are implemented and the data is correctly bound. But that depends on the technology, too) That way you do not have to do any changes on the DataGridView yourself.
If you want to change a UI element yourself, you have to do it inside the UI Thread (That is a common thing between the current technologies: Windows Forms, WPF, Silverlight). So if you want to do everything yourself, you could simply load the data from the database in a thread but then you have to use an Invoke call to apply the changes to the UI elements.Q3: As I wrote in Q2 already: Split the change to the underlying data away from the modification of the UI elements. The last must be done inside the UI Thread!
Q4: Just create only one Thread which is calling a method which invokes the first and then the second method.
Q5: If you do not want to use the backgroundworker, you could create your own ReportProgres event which your thread will fire from time to time to report the progres. Your UI could subscribe to this event and update the ProgresBar (using Invoke!).
With kind regards,
Konrad
- 已標示為解答 Alexander SunModerator 2012年5月18日 上午 06:03
-
2012年5月11日 上午 06:23
HI Vari,
1. I dont find any question.
2. If you are going to access UI controls apart from the thread it was created, InvokeRequired should be checked. Or else you will be getting Cross thread exception.
4. I hope the problem is thread 2 starts before thread 1 is completed. What you can do is, Initialize thread 2. but start the thread 2 (thread2.Start()) inside function insertingValues
void InsertingValues()
{
// Do your inserting stuff here.
thread2.Start();
}
can u explain your 5th question a bit further..
Thanks,
Sanjay.
-
2012年5月11日 下午 06:23版主
I didn't see a question in Q1, either, but I wrote something that invokes a delegate asynchronously, and returns a datatable.
class Questions { delegate DataTable Q1(string searchText); public event EventHandler<Q1_EventArgs> Q1_Completed; public void Start_Q1(string searchText) { Q1 thread = new Q1(Q1_AsyncMethod); thread.BeginInvoke(searchText, Q1_Callback, thread); } private DataTable Q1_AsyncMethod(string searchText) { DataTable result = new DataTable(); // fiil your table using searchText; return result; } private void Q1_Callback(IAsyncResult result) { Exception exception = null; DataTable table = null; Q1 method = result.AsyncState as Q1; try { table = method.EndInvoke(result); } catch ( Exception ex ) { exception = ex; // handle the exception } finally { Q1_EventArgs e = new Q1_EventArgs(table, exception); this.On_Q1_Completed(e); } } private void On_Q1_Completed(Q1_EventArgs e) { EventHandler<Q1_EventArgs> handler = this.Q1_Completed; if ( handler != null ) { handler.Invoke(this, e); } } } public class Q1_EventArgs : EventArgs { public DataTable Table { get; set; } public Exception Exception { get; set; } public Q1_EventArgs(DataTable table, Exception exception) { this.Table = table; this.Exception = exception; } }
Notice that there is only one public method and one public event. I suppose you could enhance it by creating a constructor that instantiates the delegate with a constructor parameter, which would make it infinitely more re-usable.Rudy
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
- 已編輯 Rudedog2MVP, Moderator 2012年5月11日 下午 06:32
-
2012年5月11日 下午 08:26
My advice would be to avoid threads.
They can cause all sorts of problems if you arent very careful.
I wrote a 100,000 line program and looked into threading and there was very little done at the same time.
n.Wright

