multithreading - c# Task class and memory leak -
multithreading - c# Task class and memory leak -
i have application handles info text file - reads line file handles , puts result in file. after handling 1 row handles next 1 until whole file done. rows file time-consuming handling. decided set handling-logic in separate thread , if handling takes longer 10 sec. kill thread. code this:
public class handler { public void handle(string row) { // perform handling } } public class programme { private static bool handlerow(string row) { task task = new task(() => new handler().handle(row)); task.start(); // updated var waitresult = task.wait(timeout); // timeout 10 sec. if(waitresult == false || task.isfaulted) homecoming false; homecoming true; } public static void main() { foreach(var row in getrowstohandle()) handlerow(row); } } but somehow when running programme out of memory exception. seems memory not released properly. know why memory leaks might happen? updated forgot include task.start() in code sniffer. set there
your 10s timeout times out task. doesn't stop handle() executing (if indeed ever starts - can't see start there). means locally see timeout on task.
also, depends in part on getrowstohandle() - homecoming non-buffered sequence, or list, etc.
while task support cancellation, requires co-operation implementation. honest, since aren't doing async might improve off handling own "have taken long" basic timeout in handle(). thread-abort (the other option) not recommended.
c# multithreading memory-leaks
Comments
Post a Comment