Results 1 to 4 of 4

Thread: Syntax for Creating and Terminating C# Threads

  1. #1
    Join Date
    Nov 2009
    Posts
    68

    Syntax for Creating and Terminating C# Threads

    I have started studying Csharp recently and I have to admit it is really interesting learning to it. I have studied C++ but not Java. So sometimes Csharp becomes abit confusing for me. I am covering a topic called Threads in Csharp. I want to know the Syntax for creating and terminating threads in Csharp. I am finding this topic very confusing. I need help on how to create and terminate threads.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Syntax for Creating and Terminating C# Threads

    Threading is a concept of C# wherein, you can do more than one operation at a time by allowing your program to do processing concurrently.


    Code:
    using System;
    using System.Threading;
    
    public class emp
    {
     
        public void AssgnWrk()
        {
            while (!endit)
            {
                Console.WriteLine("Employee Thread: working...");
            }
            Console.WriteLine("Employee thread: Terminating");
        }
        public void Halt()
        {
            endit = true;
        }
    
        private volatile bool endit;
    }
    
    public class EmpThread
    {
        static void Main()
        {
            emp e = new emp();
            Thread EmpThread = new Thread(e.AssgnWrk);
          
            EmpThread.Start();
            Console.WriteLine("The Main Thread: Beginning Employee Thread");
    
            while (!EmpThread.IsAlive);
    
            Thread.Sleep(1);
    
            e.Halt();
    
            EmpThread.Join();
            Console.WriteLine("The Main Thread: Employee Thread Destroyed.");
        }
    }
    Last edited by Praetor; 12-11-2009 at 06:18 PM.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Syntax for Creating and Terminating C# Threads

    When I was learning Threads in Csharp, I was also alot confused. This is a very simple example to illustrate the use of threads in C# by printing 1 - 10 reverse numbers:

    Code:
    using System;
    using System.Threading;
    
    class Number
     {
       public static void Reverse()
       {
        for(int num = 10; num > 0; num--)
        {
          Console.Write(num.ToString() + " ");
        }
       }
    
      public static void Main()
      {
        Thread N = new Thread(new ThreadStart(Reverse));
        N.Start(); // Starts the thread
      }
    }

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Syntax for Creating and Terminating C# Threads

    Thread.Start() is the method that is used to start a Thread. Thread class is used for creating a Thread.
    eg.
    Code:
    Thread t1 = new Thread (new ThreadStart (somedata));  // thread created
    th.Start();   // thread started
    Usually when a thread runs until it finishes when once started, but by calling the Abort() method in C# we can force the thread to stop.

Similar Threads

  1. Rhapsody is terminating with run time errors
    By Vis!Wa in forum Windows Software
    Replies: 4
    Last Post: 27-01-2012, 01:00 PM
  2. What would be a limit for threads per day?
    By Cauvery in forum Technology & Internet
    Replies: 7
    Last Post: 21-07-2011, 11:47 PM
  3. Threads in JavaFX
    By Rily in forum Software Development
    Replies: 3
    Last Post: 15-07-2010, 04:02 PM
  4. Threads in Java
    By Addis in forum Guides & Tutorials
    Replies: 4
    Last Post: 18-02-2010, 12:47 PM
  5. Explorer.exe Terminating Due To Runtime Error.
    By DeborahR in forum Windows XP Support
    Replies: 7
    Last Post: 29-11-2006, 12:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,002,474.27224 seconds with 17 queries