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. thread.interrupt only working once

thread.interrupt only working once

Scheduled Pinned Locked Moved C#
helptutorialquestioncareer
3 Posts 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using Thread.Sleep(Timeout.Infinite); to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpreted

    private static Thread TimeoutThread;
    private void Form1Load(object sender, EventArgs e)
    {
    var Job = new ThreadStart(TestTimeout);
    TimeoutThread = new Thread(Job) { IsBackground = true };
    TimeoutThread.Start();
    }

         private static void TestTimeout() 
         { 
             MessageBox.Show("Run"); 
             try 
             { 
                 Thread.Sleep(Timeout.Infinite); 
             } 
             catch (ThreadInterruptedException) 
             { 
                 TestTimeout(); 
             } 
         } 
    
         private void button1\_Click(object sender, EventArgs e) 
         { 
             TimeoutThread.Interrupt(); 
         }
    

    Can anyone tell me how to resolve this issue

    L G 2 Replies Last reply
    0
    • L Lost User

      nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using Thread.Sleep(Timeout.Infinite); to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpreted

      private static Thread TimeoutThread;
      private void Form1Load(object sender, EventArgs e)
      {
      var Job = new ThreadStart(TestTimeout);
      TimeoutThread = new Thread(Job) { IsBackground = true };
      TimeoutThread.Start();
      }

           private static void TestTimeout() 
           { 
               MessageBox.Show("Run"); 
               try 
               { 
                   Thread.Sleep(Timeout.Infinite); 
               } 
               catch (ThreadInterruptedException) 
               { 
                   TestTimeout(); 
               } 
           } 
      
           private void button1\_Click(object sender, EventArgs e) 
           { 
               TimeoutThread.Interrupt(); 
           }
      

      Can anyone tell me how to resolve this issue

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I think that recursive call to TestTimeout is causing some problems. Does something like this do what you want (I have added a spinwait to simulate some work being done).

          private static Thread TimeoutThread; 
          private void Form1\_Load(object sender, EventArgs e)
          {
              ThreadStart Job = new ThreadStart(TestTimeout);
              TimeoutThread = new Thread(Job) { IsBackground = true };
              TimeoutThread.Start(); 
          }
      
          private static void TestTimeout() 
          {
              while (true)
              {
                  MessageBox.Show("Starting Work");
                  try
                  {
                      Thread.SpinWait(100000000);
                      MessageBox.Show("Finished Work");
                      Thread.Sleep(Timeout.Infinite);
                  }
                  catch (ThreadInterruptedException)
                  {
      
                  }                 
              }
      
          }
      
          private void button1\_Click(object sender, EventArgs e)
          {
              TimeoutThread.Interrupt();
          }
      
      1 Reply Last reply
      0
      • L Lost User

        nd am having a problem in my application that ive been able to replicate using the code below. I want the function TestTimeout to run only at certain times so im using Thread.Sleep(Timeout.Infinite); to stop it. I'm interrupting the thread using the button below it works once however has no effect afterwards ? What should happen is it should display the message then sleep again until interpreted

        private static Thread TimeoutThread;
        private void Form1Load(object sender, EventArgs e)
        {
        var Job = new ThreadStart(TestTimeout);
        TimeoutThread = new Thread(Job) { IsBackground = true };
        TimeoutThread.Start();
        }

             private static void TestTimeout() 
             { 
                 MessageBox.Show("Run"); 
                 try 
                 { 
                     Thread.Sleep(Timeout.Infinite); 
                 } 
                 catch (ThreadInterruptedException) 
                 { 
                     TestTimeout(); 
                 } 
             } 
        
             private void button1\_Click(object sender, EventArgs e) 
             { 
                 TimeoutThread.Interrupt(); 
             }
        

        Can anyone tell me how to resolve this issue

        G Offline
        G Offline
        Ganesh Kumar Kaki
        wrote on last edited by
        #3

        check out once that may be your form_load event called many times..

        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