true

Find the best tutors and institutes for C Sharp

Find Best C Sharp Classes

Please select a Category.

Please select a Locality.

No matching category found.

No matching Locality found.

Outside India?

Search for topics

How I use threads?

Asked by Last Modified  

IT Courses/Programming Languages/C Sharp

Follow 0
Answer

7 Answers

Please enter your answer

DBA Trainer

This section discusses two aspects of thread usage. We first discuss threads as it applies to every Swing application namely, why all code that deals with components needs to execute on a specific thread. We also discuss the invokeLater and invokeAndWait methods, which are used to ensure that code... read more
This section discusses two aspects of thread usage. We first discuss threads as it applies to every Swing application — namely, why all code that deals with components needs to execute on a specific thread. We also discuss the invokeLater and invokeAndWait methods, which are used to ensure that code executes on this special thread. The rest of the section applies only to those who want to use additional threads to improve performance in their application. We also include a discussion of SwingWorker — a handy class used by many of our demos to offload time-consuming tasks. read less
Comments

SharePoint Administration And Development, Office 365,Advanced .Net C#

Last couple of years processors had a rapid development especially on its speed. Nowadays a processor in the market is with a speed around 3GHz - 4GHz.when processor comes up to this level processor vendors such as Intel, AMD used to notified that processor can't be speeds up more and more with the environment... read more
Last couple of years processors had a rapid development especially on its speed. Nowadays a processor in the market is with a speed around 3GHz - 4GHz.when processor comes up to this level processor vendors such as Intel, AMD used to notified that processor can't be speeds up more and more with the environment they should use. Then they used to think about parallel processing, here's the point Hyper-threading technology comes in to action. Later they improved it to Dual Core then in to Core 2 Duo. the answer is to use the maximum performance and of the above processors (advantage of parallel processing) we have to use the threads with our C# applications. class Program { static void Main(string[] args) { // create thread start delegate instance - contains the method to execute by the thread ThreadStart ts=new ThreadStart(run); // create new thread Thread thrd=new Thread(ts); // start thread thrd.Start(); // makes the main thread sleep - let sub thread to run Thread.Sleep(1000); for (int t=10; t > 0; t--) { Console.WriteLine("Main Thread value is :" + t); Thread.Sleep(1000); } Console.WriteLine("Good Bye!!!I'm main Thread"); Console.ReadLine(); } // this method executed by a separate thread static void run() { for (int i = 0; i < 10; i++) { Console.WriteLine("Sub Thread value is : " + i); Thread.Sleep(1000); } Console.WriteLine("Good Bye!!!I'm Sub Thread"); } } Periodic operations from threads If you want to perform any method call in a perodic order then you can use the Timer class. Note : There are three timer classes availbale with .net framework class library one is in System namespace next is in System.Windows.Forms namespace and the last is in System. Threading namespace all of them are providing similar functionalities so please don't confuse them together, I'm talking here is about the System.Threading.Timer class Example: Using Timer class class Program { static void Main(string[] args) { // assign thread timer to do the job System.Threading.Timer thrdTimer = new Timer(run, 10, 0, 1000); // makes the main thread sleep - let sub thread to run Thread.Sleep(1000); Console.WriteLine("Good Bye!!!I'm main Thread"); Console.ReadLine(); } /* this method executed by a separate thread * this sholud be match with the TimerCallback * (parameters must be passed as an object) */ static void run(object args) { // cast our parameter int j = (int)args; Console.WriteLine("Hi I'm executing by timer you passed " + j); } } Here is the line we are assiging new timer to do the job,last parameter is the period to stay(in miliseconds) within the callbacks (here I have passed the 1000). // assign thread timer to do the job System.Threading.Timer thrdTimer = new System.Threading.Timer(run, 10, 0, 1000); See and how much .net framework class library had made the developer life easier. read less
Comments

Have 9+ years in teaching with 5 + years software development industries exp

public void DoWork() { while (!_shouldStop) { Console.WriteLine("worker thread: working..."); } Console.WriteLine("worker thread: terminating gracefully."); } MyThread.Suspend() // causes suspend the Thread Execution. MyThread.Resume() // causes the suspended Thread... read more
public void DoWork() { while (!_shouldStop) { Console.WriteLine("worker thread: working..."); } Console.WriteLine("worker thread: terminating gracefully."); } MyThread.Suspend() // causes suspend the Thread Execution. MyThread.Resume() // causes the suspended Thread to resume its execution. read less
Comments

Computer Guru

Use System.Threading for Create threads.
Comments

IT Trainer,Manager @ HLT

This example demonstrates how to create and start a thread, and shows the interaction between two threads running simultaneously within the same process. Note that you don't have to stop or free the thread. This is done automatically by the .NET Framework common language runtime. The program begins... read more
This example demonstrates how to create and start a thread, and shows the interaction between two threads running simultaneously within the same process. Note that you don't have to stop or free the thread. This is done automatically by the .NET Framework common language runtime. The program begins by creating an object of type Alpha (oAlpha) and a thread (oThread) that references the Beta method of the Alpha class. The thread is then started. The IsAlive property of the thread allows the program to wait until the thread is initialized (created, allocated, and so on). The main thread is accessed through Thread, and the Sleep method tells the thread to give up its time slice and stop executing for a certain amount of milliseconds. The oThread is then stopped and joined. Joining a thread makes the main thread wait for it to die or for a specified time to expire (for more details, see Thread.Join Method). Finally, the program attempts to restart oThread, but fails because a thread cannot be restarted after it is stopped (aborted). For information on temporary cessation of execution, see Suspending Thread Execution. public class Alpha { // This method that will be called when the thread is started public void Beta() { while (true) { Console.WriteLine("Alpha.Beta is running in its own thread."); } } }; public class Simple { public static int Main() { Console.WriteLine("Thread Start/Stop/Join Sample"); Alpha oAlpha = new Alpha(); // Create the thread object, passing in the Alpha.Beta method // via a ThreadStart delegate. This does not start the thread. Thread oThread = new Thread(new ThreadStart(oAlpha.Beta)); // Start the thread oThread.Start(); // Spin for a while waiting for the started thread to become // alive: while (!oThread.IsAlive); // Put the Main thread to sleep for 1 millisecond to allow oThread // to do some work: Thread.Sleep(1); // Request that oThread be stopped oThread.Abort(); // Wait until oThread finishes. Join also has overloads // that take a millisecond interval or a TimeSpan object. oThread.Join(); Console.WriteLine(); Console.WriteLine("Alpha.Beta has finished"); try { Console.WriteLine("Try to restart the Alpha.Beta thread"); oThread.Start(); } catch (ThreadStateException) { Console.Write("ThreadStateException trying to restart Alpha.Beta. "); Console.WriteLine("Expected since aborted threads cannot be restarted."); } return 0; } } Example Output Thread Start/Stop/Join Sample Alpha.Beta is running in its own thread. Alpha.Beta is running in its own thread. Alpha.Beta is running in its own thread. ... ... Alpha.Beta has finished Try to restart the Alpha.Beta thread ThreadStateException trying to restart Alpha.Beta. Expected since aborted threads cannot be restarted. read less
Comments

Create a new ThreadStart delegate. delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.
Comments

Computer Teacher / Corporate Trainer

do u need full program ?
Comments

View 5 more Answers

Related Questions

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Why a function in C# requires "Return type"??
- Basically , a Method is a piece of code used for the re-usability purpose. - Method is of 2 types Function and Procedure - Function is a method which returns a value to the calling place Function...

Raga Deepthi G. | 25/09/2018

0 0
0

"foreach" loop in C#
foreach is a looping statement : repeats a group of statements for each element in an array or an object collection. (or) used to iterate through the collection/ an array to get the required information. Advantages: Easy...

Raga Deepthi G. | 30/03/2018

3 0
1

Inversion of Control
Problem You have classes that have dependencies on services or components whose concrete type is specified at design time. In this example, ClassA has dependencies on ServiceA and ServiceB. Figure 1 illustrates...

Ranjan P. | 25/09/2017

1 0
0

Recommended Articles

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a significant" increase in demand for IT professionals in 2014 as compared to 2013, a steady demand for IT professionals is rest assured...

Read full article >

Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system s storage capacity and you can t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...

Read full article >

Applications engineering is a hot trend in the current IT market. An applications engineer is responsible for designing and application of technology products relating to various aspects of computing. To accomplish this, he/she has to work collaboratively with the company s manufacturing, marketing, sales, and customer...

Read full article >

Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...

Read full article >

Looking for C Sharp Classes?

Find best C Sharp Classes in your locality on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Sharp Classes?

Find best tutors for C Sharp Classes by posting a requirement.

  • Post a learning requirement
  • Get customized responses
  • Compare and select the best

Looking for C Sharp Classes?

Find best C Sharp Classes in your locality on UrbanPro

Post your learning requirement

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 25 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 6.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more