UrbanPro

Learn C Sharp from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

How I use threads?

Asked by Last Modified  

7 Answers

Learn C Sharp

Follow 0
Answer

Please enter your answer

MS SQL SERVER 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

Tips of learning Java Language/Other Programming Languages
1.You should know the basic concept: If we talk about programming languages so basic concept are same in all the high level languages. So you should know the basic concept firstly then you can easily understand...
I

ICreative Solution

0 0
0

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...

C#-Program Rectangle and Circle
using System;using System.Collections.Generic;using System.Linq;using System.Text; //Namespace declarationnamespace figures{ //Rectangle Class declaration class rectangle { // member variables...

Extension Methods in C#
Extension methods enables you to add methods to existing types without creating a new derived type, recompiling or otherwise modifying the original type. Extension methods are special type of static methods,...

"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...

Recommended Articles

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 >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Software Development has been one of the most popular career trends since years. The reason behind this is the fact that software are being used almost everywhere today.  In all of our lives, from the morning’s alarm clock to the coffee maker, car, mobile phone, computer, ATM and in almost everything we use in our daily...

Read full article >

Looking for C Sharp Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C Sharp Classes?

The best tutors for C Sharp Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C Sharp with the Best Tutors

The best Tutors for C Sharp Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 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 7.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