1

I wonder how to abort my Thread after my function ends Thread.Abort();
My application running files and each file is opened is different thread

int _counter;
int _parallelThreads
_queue = new Queue();

public void transmit()
{
    while (_counter < _parallelThreads)
    {
        lock (_queue)
        {
            string file = (string)_queue.Dequeue();
            ThreadStart ts = delegate { processFile(file); };
            Thread thread = new Thread(ts);
            thread.IsBackground = true;
            thread.Start();
            _counter++;
        }
    }
}

private void processFile(string file)
{
    WiresharkFile wf = new WiresharkFile(file, _selectedOutputDevice, 1);
    wf.OnFinishPlayEvent += wf_OnFinishPlayEvent;
    wf.sendBuffer();
}

and this is the event that my file finished

private void wf_OnFinishPlayEvent(MyClass class)
{
   // here i want to abort my thread
}

The reason i want to abort my thread when it finished is because i think this is my memory lack reason in case i open a lot of parallels thread and run it over ond over (my application memory usage read more than 1 giga)

user2214609
  • 4,713
  • 9
  • 34
  • 41

2 Answers2

1

when you abort a thread, a lot of unexpected things can go wrong. particularly when you work with files. when i had to do that (for example, a "cancel" button) i used a litlle trick.

i had a flag IsCanceled on a scope both threads can see be set to true, and on the worker thread, every few statement, will check that flag and close all open files and end itself.

this might not work well for your situation, depending on wf.sendBuffer(); logic. let me know

Example:

private void processFile(string file)
{
    WiresharkFile wf = new WiresharkFile(file, _selectedOutputDevice, 1);
    wf.OnFinishPlayEvent += wf_OnFinishPlayEvent;

    if(IsCanceled == false)
    {
       wf.sendBuffer();
    }
}

and if the sendBuffer() method logic is too long, then

public void sendBuffer()
{
    // some logic

    if(IsCanceled)
    {
       // close open streams
       return;
    }

    // some logic
}

as for the flag itself, a singleton class could do just fine for that, or a class all the other classes know

public class Singleton
{
   private static Singleton instance;
   private bool isCanceled;
   private Singleton() 
   {
       isCanceled = false;
   }

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }

   public bool IsCanceled
   {
      get 
      {
         return isCanceled;
      }
      set
      {
         isCanceled = value;
      }
   }
}

notice that the singleton class is open to everyone, and you might want to use a class only known by the threads that needs to check it. that is something that depend on your security needs.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
-1

You should not abort the threads, threads will quit automatically when the code in it finishes. Maybe you just want to wait the thread to finish, after that do something else.
You can use an array to store the thread, and use Thread.Join() to wait all the threads end.

List<Thread> threadList = new List<Thread>();

public void transmit()
{
    while (_counter < _parallelThreads)
    {
        lock (_queue)
        {
            string file = (string)_queue.Dequeue();
            ThreadStart ts = delegate { processFile(file); };
            Thread thread = new Thread(ts);
            thread.IsBackground = true;        
            threadList.Add(thread);       //add thread to list
            thread.Start();
            _counter++;
        }
    }
    //wait threads to end
    foreach(Thread t in threadList)
          t.Join();
}

private void processFile(string file)
{
    WiresharkFile wf = new WiresharkFile(file, _selectedOutputDevice, 1);
    wf.OnFinishPlayEvent += wf_OnFinishPlayEvent;
    wf.sendBuffer();
}
Jerry
  • 435
  • 4
  • 12
  • Because the t.Join(); it even not started to play – user2214609 Aug 03 '13 at 13:26
  • In the for statement, the following line code will start the thread.
    thread.Start(); Whether or not you write the t.Join() has no effect on the execution of the thread. If only you write the thread.Start(), the thread will start. No matter you write thread.Joion or not.
    – Jerry Aug 04 '13 at 04:17