Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Kill all threads

I have an application that invokes my DLL with InvokeMember() like so:

Assembly OCA = Assembly.LoadFrom("./Modules/ProcessFiles.dll");
Type[] types = OCA.GetTypes();
foreach (var type in types)
{
    //MethodInfo[] methods = type.GetMethods();
    if (type.Name == "Converter")
    {
        var tmpType = type;
        var obj = Activator.CreateInstance(tmpType);
        Thread t = new Thread(
            () =>
            tmpType.InvokeMember("Run", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj,
            null));
        t.Start();
        break;
    }

}

My DLL then creates new a thread and starts processing. In my DLL, I the create new thread like this:

Thread thread = new Thread(
    delegate(){
        while(true)
        {
            GetFilesInFolder();
            Thread.Sleep(120000);
        }
    });
ne.Start();

The goal is to periodically check the folder. The problem is that when I close the application that invokes my DLL, the process is not closed. Is there a way to close all the threads?

NB: I can’t modify the application, I can only modify my DLL.

like image 785
hs2d Avatar asked Jan 03 '12 11:01

hs2d


3 Answers

Set the IsBackground property of the thread to true. This will kill the thread when your app finishes.

Also: Why don't you use one timer (or just one thread), that wakes and eximines the data. That should be more resource friendly.

like image 154
GvS Avatar answered Nov 11 '22 05:11

GvS


Did you try using a System.IO.FileSystemWatcher? this throws events when something changes in a folder. It looks like this will simplify your solution.

like image 38
Bas Avatar answered Nov 11 '22 03:11

Bas


How about implementing Safe Cancellation from here

class RulyCanceler
{
  object _cancelLocker = new object();
  bool _cancelRequest;
  public bool IsCancellationRequested
  {
    get { lock (_cancelLocker) return _cancelRequest; }
  }

  public void Cancel() { lock (_cancelLocker) _cancelRequest = true; } 

  public void ThrowIfCancellationRequested()
  {
    if (IsCancellationRequested) throw new OperationCanceledException();
  }
}

Test

class Test
{
  static void Main()
  {
    var canceler = new RulyCanceler();
    new Thread (() => {
                        try { Work (canceler); }
                        catch (OperationCanceledException)
                        {
                          Console.WriteLine ("Canceled!");
                        }
                      }).Start();
    Thread.Sleep (1000);
    canceler.Cancel();               // Safely cancel worker.
  }

  static void Work (RulyCanceler c)
  {
    while (true)
    {
      c.ThrowIfCancellationRequested();
      // ...
      try      { OtherMethod (c); }
      finally  { /* any required cleanup */ }
    }
  }

  static void OtherMethod (RulyCanceler c)
  {
    // Do stuff...
    c.ThrowIfCancellationRequested();
  }
}
like image 1
Surjit Samra Avatar answered Nov 11 '22 05:11

Surjit Samra