Click here to Skip to main content
15,850,214 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

I have a windows form with 2 buttons. One button would call the Get method and the other button will call update method. So at the end both will point to the same data table. Do i really need to use Multi-threading here?

I have an other scenario too:

I have a text box which shows how many threads to start. If the value is 10 then, then Get method will be executed on 10 threads and i would determine which thread is taking the longest time and which is taking the shortest time...Please help me how can i do this.Thanks


Please clarify...

Thanks.
Posted
Updated 9-May-13 4:29am
v2
Comments
Sergey Alexandrovich Kryukov 9-May-13 9:05am    
Why? This is too vague question; all depends on what Get and Update do. If they don't block UI thread, why multithreading.
—SA
vidkaat 9-May-13 9:31am    
Thanks ..I should use Multi threading as my requirement is changed ....I have a textbox which shows how many threads to start when a Get button is clicked once. So if it is 10 ,then on one click it should execute the Get method in 10 threads...I am actually measuring the execution time of each thread ...Please give me some idea how can i achieve this...Thanks.

Probably multithreading is useless in your case. It would be usefulif (at least) one task takes a lot of time (that is user noticeable) to complete: often threads are used to prevent the GUI freeze while a long time operation is occurring.
 
Share this answer
 
Comments
vidkaat 9-May-13 9:03am    
Thanks a lot.
Sergey Alexandrovich Kryukov 9-May-13 10:29am    
My 5.
—SA
Please see my comment to the question. The problem is not formulated to the detail allowing to decide finally.

Multithreading may have many different justification. In particular, you should call all methods which blocks or simply considerably delay the execution, in a separate (non-UI) thread.

More essentially, it can be required by logic. If you have some sequential processing, more or less independent in its logic, it should be a separate thread. The problem should be considered in a holistic way. Some algorithm working the best in a console-only application, will require a separate thread in a windowed UI, and so on.

[EDIT]

Please see some of my past answers on related topics:

One of the best ways to create a thread:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^];

throttling threads properly (Resume/Pause is deprecated as unsafe):
ManualResetEvent and AutoResetEvent in Thread[^],
pause running thread[^];

invocation to UI thread:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

The topics I listed are practically the most important, when the UI is involved.

—SA
 
Share this answer
 
v2
Comments
vidkaat 9-May-13 9:25am    
Thanks ..I also have one more question..I have an other scenario .I have a textbox which shows how many threads to start when a Get button is clicked once. So if it is 10 ,then on one click it should execute the Get method in 10 threads...I am actually measuring the execution time of each thread ...Please give me some idea how can i achieve this...Thanks.
Sergey Alexandrovich Kryukov 9-May-13 10:33am    
This text box should better be NumericUpDown, not a TextBox. You measure time with great precision using System.Diagnostics.Stopwatch, but the real results will depend on other threads on the system and may vary. I've added some references to my threading answers, please see the update answer, after [EDIT].
—SA
While CPallini is generally right, I always try to keep anything that is potentially reusable, or could possibly someday exhibit unexpected or unwanted behavior out of the UI. If there's a possibility that you can build your database calls into a class you can reuse, then do that. Also, I think making database calls off the UI thread is not a bad idea. There could always be network slowness/trouble someday, or database server slowness or problems - and if your making those calls in the UI thread then your UI will freeze.

I would set up a delegate and do that stuff off thread, and then pass your callback the results to update your UI.
 
Share this answer
 
v2
Comments
vidkaat 9-May-13 9:32am    
I have updated my question...Thanks for ur answer
Hey,

Here if there are two buttons then I think only 1 button event will be firing at a single call.

You cant hit both buttons at a single moment so there will be only 1 event triggering (either GET or Update) in a single postback.

May be here multithreading is not reqd.

Hope it helps...


Cheers!!
Sam
 
Share this answer
 
Comments
vidkaat 9-May-13 9:03am    
Thanks a lot.
Sergey Alexandrovich Kryukov 9-May-13 10:29am    
Not applicable at all. This is not a Web application. What "postback"? Come on...
—SA

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