c# - Threading in Producer-Consumer pattern -


first sorry bad english. 've code reading .csv file, 1 one , after read, 'll removed. .csv file format this:

id     name     parentid ------------------------ 0      [root]     1             0 2      b        0 3      c        1 4      d        2 

and result be

[root] _a __c _b __d 

just tree. can execute on 1 file , after first time, consumer never wakes again, please me fix or explain how call reason , way fix.

thanks lot.

class program {     static producerconsumer prdcos;     static string[] file;      static void main(string[] args)     {         string directory = "";         prdcos = new producerconsumer();          console.cancelkeypress += new consolecanceleventhandler(console_cancelkeypress);         directory = input_directory();         file = null;          while (true)         {             if (check_exist(directory) == true)                 break;             else              {                 console.writeline("please reinput \n");                 directory = input_directory();             }         }          file = directory.getfiles(directory, "*.csv");          console.writeline("\n-------------------------program start------------------------------");          thread thread =  new thread(new threadstart(consumejob));         thread thread2 = new thread(new threadstart(procedurejob));         thread.isbackground = true;         thread2.isbackground = true;         thread.start();         thread2.start();          //delete file          //for (int = 0; < file.length; i++)         //    file.delete(file[i]);         console.readline();          while(console.readkey(true).key == consolekey.enter)         {             console.writeline("press ctr + c exit");         }     }      static void console_cancelkeypress(object sender, consolecanceleventargs e){}      static void procedurejob()     {         if (file.length > 0)         {             foreach (string str in file)             {                 prdcos.produce(str);                 thread.sleep(1000);             }         }     }      static void consumejob()     {         list<string> = new list<string>();         = file.tolist();         //for(int = 0; i<a.count;i++)         //{             prdcos.consume();             thread.sleep(100);         //}     }      static string input_directory()     {         string str = "";          console.write("input file directory:  ");         str = console.readline();         return str;     }      static bool check_exist(string path)     {         int count;         string[] file;         //string filename = "";         string filepath;         filepath = path;          if (directory.exists(filepath) != true)                 // check file path exist         {             console.writeline("{0} not valid directory!!!", path);             return false;         }         else         {             count = directory.getfiles(filepath, "*").length;             file = directory.getfiles(filepath, "*.csv");         }          if (count == 0)         {             console.writeline("folder empty!!!");             return false;         }         if (count > 0 && file.length == 0)         {             console.writeline("input file not valid!!!");             return false;         }         return true;     }      }      public class producerconsumer     {     queue queue = new queue();     object locker = new object();     int node = 0;     int node = 0;      public void produce(string path)     {         console.writeline("processing produce");              lock (locker)             {                 try                 {                     //foreach (string mpath in path)                     //{                     var reader = new streamreader(file.openread(path));                     system.collections.generic.list<string> str1 = new system.collections.generic.list<string>();                      while (!reader.endofstream)                     {                         string line = reader.readline();                         if (line.split(',').length > 3)                         {                             console.writeline("file's data wrong format!!!");                             return;                         }                         str1.add(line);                     }                     reader.close();                     queue.enqueue(str1);                     thread.sleep(2000);                     //}                 }                 catch (exception ex)                 {                     console.writeline(ex);                 }             }         console.writeline("finished processing produce");     }      public void consume()     {         console.writeline("processing consume");         list<string> parentid = new list<string>();         datarow[] row = { };         lock (locker)         {             try             {                 if (queue.count > 0)                 {                     foreach (system.collections.generic.list<string> str in queue)                     {                         try                         {                             node = 0;                             node = 0;                             node = str.count() - 1;                             printdata(str, 0, str);                             console.writeline("\n");                         }                         catch (exception ex)                         {                             console.write(ex);                         }                      }                     console.writeline("finished processing consume");                     thread.sleep(1000);                 }             }                             catch (exception ex)             {                 console.writeline(ex);             }             //while (queue.count == 0)             //{             //    monitor.wait(locker);             //    queue.dequeue();             //}             //if (queue == null)             //    return;             //thread.sleep(100);         }     }      private void printwithindent(string value, int indent)     {         node++;         console.writeline("{0}{1}", new string(' ', indent), value);// prtvalue[1]);     }      public void printdata(list<string> str, int indent, list<string> strmain)     {           list<string> child = new list<string>();         string[] value = null;         string[] valuemain = null;         (int = 0; < str.count; i++)         {             if (node >= node)                 return;             child = new list<string>();             valuemain = str[i].split(',');             if (str[i].tostring() == strmain[0].tostring())                 continue;             else                 printwithindent(valuemain[1], indent);             foreach (string mstr in strmain)             {                 value = mstr.split(',');                 if (value[2] == valuemain[0])                     child.add(mstr);             }              child = child.orderby(x => (x.split(',')[1])).tolist();    //sort alphabet              if (child.count > 0)             {                 printdata(child, indent + 1, strmain);             }         }     } } 

the first problem notice consumer ending. there more problems though - suggest throw away , start again.

first need make sure understand pattern. need relate classic pattern threading in .net.

once understand conceptual level stuff, should investigate parallel linq (plinq) , task parallel library (tpl) - using these producer/consumer save lot of time , bugs (no more locking, can have threads block while wait, etc.).

here other references may help:


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -