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.
Set the
IsBackgroundproperty of the thread totrue. 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.