Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Threading in C# with a Console Application

Threading in C# with a Console Application

Scheduled Pinned Locked Moved C#
csharpjavahelpquestionlearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    agent00zelda
    wrote on last edited by
    #1

    Hello. I'm new to C# (switched from Java) and I am trying to see if there is a way to keep a thread running indefinitely, with no timeout. I have a console application written, but I need it to run more like a windows service in that I want all threads to run until they are told to stop. Is there a special class that will help with this, or does anyone know of a good resource that I can learn more about this specifically??

    D 1 Reply Last reply
    0
    • A agent00zelda

      Hello. I'm new to C# (switched from Java) and I am trying to see if there is a way to keep a thread running indefinitely, with no timeout. I have a console application written, but I need it to run more like a windows service in that I want all threads to run until they are told to stop. Is there a special class that will help with this, or does anyone know of a good resource that I can learn more about this specifically??

      D Offline
      D Offline
      Deresen
      wrote on last edited by
      #2

      You can use the namespace System.Threading for this. The same as you use threading in java, you can use it in C#.

      class ThreadTest
      {
      static void Main()
      {
      Thread t = new Thread (WriteY);
      t.Start(); // Run WriteY on the new thread
      while (true) Console.Write ("x"); // Write 'x' forever }

      static void WriteY()
      {
      while (true) Console.Write ("y"); // Write 'y' forever
      }
      }

      To stop the thread, use t.Stop(); Or you can sleep inside the thread with Thread.Sleep(1000);//one second Good luck

      A 1 Reply Last reply
      0
      • D Deresen

        You can use the namespace System.Threading for this. The same as you use threading in java, you can use it in C#.

        class ThreadTest
        {
        static void Main()
        {
        Thread t = new Thread (WriteY);
        t.Start(); // Run WriteY on the new thread
        while (true) Console.Write ("x"); // Write 'x' forever }

        static void WriteY()
        {
        while (true) Console.Write ("y"); // Write 'y' forever
        }
        }

        To stop the thread, use t.Stop(); Or you can sleep inside the thread with Thread.Sleep(1000);//one second Good luck

        A Offline
        A Offline
        agent00zelda
        wrote on last edited by
        #3

        Thanks SO much... I don't know why it didn't dawn on me to use a while(true) statement... silly me! But thank you, that helps a lot!!

        S 1 Reply Last reply
        0
        • A agent00zelda

          Thanks SO much... I don't know why it didn't dawn on me to use a while(true) statement... silly me! But thank you, that helps a lot!!

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          A better way to wait is to use the Thread.Join[^] method. That way 1. You don't need additional logic to have the main method break out of the loop when the thread(s) exit. 2. Infinite looping uses CPU cycles, whereas Join tells the OS scheduler to not schedule the current thread until the thread passed as the parameter completes.

          Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups