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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Aborting a Thread.

Aborting a Thread.

Scheduled Pinned Locked Moved C#
windows-adminquestionannouncement
6 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.
  • R Offline
    R Offline
    Ronni Marker
    wrote on last edited by
    #1

    Hi I have the following code that is updating an dropdown box with information Active Directory information - this below will show Domain Controllers btw. But I am trying unsuccessfully to cancel an existing running thread if one exist already. this is mainly because sometimes a thread can be running indefinitely and need to abort that, or if another thread is called that need to update the same dropdownbox. Anyway are there any out here who could explain how I could abort an running thread?

        private void ThreadDC()
        {
    
            Thread TRDomainC = new Thread(ThreadingDC);
            if (TRDomainC.IsAlive)
            {
                TRDomainC.Abort();
            }
            TRDomainC.SetApartmentState(ApartmentState.STA);
            TRDomainC.Start();
        }
    
        private void ThreadingDC()
        {
            comboBox1.Items.Clear();
            try
            {
                if (comboBoxDomains.SelectedIndex >= 0 && comboBoxDomains.SelectedIndex != -1)
                {
                    foreach (string dc in domainInfoComponent\[comboBoxDomains.SelectedItem.ToString()\])
                    {
                        comboBox1.Items.Add(dc);
                    }
                    lastIndex = comboBoxDomains.SelectedIndex;
                    comboBox1.SelectedIndex = 0;
                }
            }
            catch
            {
                lastIndex = 0;
                comboBox1.SelectedIndex = -1;
            }
        }
    
    N L 2 Replies Last reply
    0
    • R Ronni Marker

      Hi I have the following code that is updating an dropdown box with information Active Directory information - this below will show Domain Controllers btw. But I am trying unsuccessfully to cancel an existing running thread if one exist already. this is mainly because sometimes a thread can be running indefinitely and need to abort that, or if another thread is called that need to update the same dropdownbox. Anyway are there any out here who could explain how I could abort an running thread?

          private void ThreadDC()
          {
      
              Thread TRDomainC = new Thread(ThreadingDC);
              if (TRDomainC.IsAlive)
              {
                  TRDomainC.Abort();
              }
              TRDomainC.SetApartmentState(ApartmentState.STA);
              TRDomainC.Start();
          }
      
          private void ThreadingDC()
          {
              comboBox1.Items.Clear();
              try
              {
                  if (comboBoxDomains.SelectedIndex >= 0 && comboBoxDomains.SelectedIndex != -1)
                  {
                      foreach (string dc in domainInfoComponent\[comboBoxDomains.SelectedItem.ToString()\])
                      {
                          comboBox1.Items.Add(dc);
                      }
                      lastIndex = comboBoxDomains.SelectedIndex;
                      comboBox1.SelectedIndex = 0;
                  }
              }
              catch
              {
                  lastIndex = 0;
                  comboBox1.SelectedIndex = -1;
              }
          }
      
      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      Don't use Thread.Abort - it's evil! If you want to end a thread, do it cooperatively - make the thread return from it's main method and it will be cleared up properly. Also, you cannot touch UI controls from any thread other than your main UI thread. Control.Invoke is helpful here. Here is a good introduction to threading: http://www.albahari.com/threading/[^] Nick

      ---------------------------------- Be excellent to each other :)

      R 1 Reply Last reply
      0
      • R Ronni Marker

        Hi I have the following code that is updating an dropdown box with information Active Directory information - this below will show Domain Controllers btw. But I am trying unsuccessfully to cancel an existing running thread if one exist already. this is mainly because sometimes a thread can be running indefinitely and need to abort that, or if another thread is called that need to update the same dropdownbox. Anyway are there any out here who could explain how I could abort an running thread?

            private void ThreadDC()
            {
        
                Thread TRDomainC = new Thread(ThreadingDC);
                if (TRDomainC.IsAlive)
                {
                    TRDomainC.Abort();
                }
                TRDomainC.SetApartmentState(ApartmentState.STA);
                TRDomainC.Start();
            }
        
            private void ThreadingDC()
            {
                comboBox1.Items.Clear();
                try
                {
                    if (comboBoxDomains.SelectedIndex >= 0 && comboBoxDomains.SelectedIndex != -1)
                    {
                        foreach (string dc in domainInfoComponent\[comboBoxDomains.SelectedItem.ToString()\])
                        {
                            comboBox1.Items.Add(dc);
                        }
                        lastIndex = comboBoxDomains.SelectedIndex;
                        comboBox1.SelectedIndex = 0;
                    }
                }
                catch
                {
                    lastIndex = 0;
                    comboBox1.SelectedIndex = -1;
                }
            }
        
        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        As Nick said; this[^] may help also. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Merry Christmas and a Happy New Year to all.


        R 1 Reply Last reply
        0
        • N Nicholas Butler

          Don't use Thread.Abort - it's evil! If you want to end a thread, do it cooperatively - make the thread return from it's main method and it will be cleared up properly. Also, you cannot touch UI controls from any thread other than your main UI thread. Control.Invoke is helpful here. Here is a good introduction to threading: http://www.albahari.com/threading/[^] Nick

          ---------------------------------- Be excellent to each other :)

          R Offline
          R Offline
          Ronni Marker
          wrote on last edited by
          #4

          Not really an option waiting. So need to kill the thread one way or another. I know about Control.Invoke, but well, dosn't really help me on ending the thread before it is completed. Ronni

          1 Reply Last reply
          0
          • L Luc Pattyn

            As Nick said; this[^] may help also. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            R Offline
            R Offline
            Ronni Marker
            wrote on last edited by
            #5

            Nope, not really. Well I could move to backgroundworker... But there should(right?) be a way to abort the thread without getting into the deep end.

            L 1 Reply Last reply
            0
            • R Ronni Marker

              Nope, not really. Well I could move to backgroundworker... But there should(right?) be a way to abort the thread without getting into the deep end.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              As Nick and Albahari say, you should not even want to abort a thread, aborting it will leave a lot of objects in an unknown state, putting the app at the risk of a deadlock; how about things that may be locked and never will be unlocked again? You really need the cooperative approach for thread control. And a healthy way to access GUI Controls, hence the reference to my article. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Merry Christmas and a Happy New Year to all.


              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