-1

first sorry about my bad english. I 've code this for reading .csv file, one by one and after read, it 'll be removed. The .csv file format is like this:

ID     Name     ParentID
------------------------
0      [Root]    
1      A        0
2      B        0
3      C        1
4      D        2

and the result will be

[Root]
_A
__C
_B
__D

just like the tree. But I just can execute on 1 file and after the first time, the consumer never wakes up again, so please help me fix this or explain how call this reason and the way to fix.

Thanks a 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 i = 0; i < file.Length; i++)
        //    File.Delete(file[i]);
        Console.ReadLine();

        while(Console.ReadKey(true).Key == ConsoleKey.Enter)
        {
            Console.WriteLine("press ctr + c to 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> a = new List<string>();
        a = file.ToList();
        //for(int i = 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} is not valid Directory!!!", path);
            return false;
        }
        else
        {
            Count = Directory.GetFiles(filePath, "*").Length;
            file = Directory.GetFiles(filePath, "*.csv");
        }

        if (Count == 0)
        {
            Console.WriteLine("Folder is 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 Is 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;
        for (int i = 0; i < 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 by Alphabet

            if (child.Count > 0)
            {
                PrintData(child, indent + 1, StrMain);
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vincent
  • 21
  • 5
  • 2
    Please don't just dump your code. Narrow the problem down to a specific piece of the program and ask about that. You can use a debugger to help you narrow down where the problem is occurring (and possibly even fix it!) – clcto May 29 '14 at 23:10
  • Oh thanks so much, I will notice that. I just think it will more clear if I post all here. I'm so sorry and thanks for your advice. Btw can you help me? – Vincent May 30 '14 at 00:39

1 Answers1

0

The first problem I notice is that your consumer is ending. There are more problems than that though - I would suggest you throw it away and start again.

First you need to make sure you properly understand the pattern. Then you need to relate the classic pattern to threading in .Net.

Once you understand the conceptual level stuff, you should also investigate Parallel LINQ (PLINQ) and the Task Parallel Library (TPL) - using these for your producer/consumer will save a lot of time and bugs (no more locking, you can have threads that block while they wait, etc.).

Here are some other references that may help:

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • First, thanks for your advice, I will try to start again but it's no more time. Is this can be fix for run??? I spend 3 days to research this pattern and threading, now I just have 12hour to finish it. Is there any solution? I very thanks for your reply. – Vincent May 30 '14 at 00:53
  • A quick and nasty fix is to look at putting a `while(true){ ... }` loop inside the consumer thread. – slugster May 30 '14 at 02:24
  • Thanks for your reply, your attention in this post is a favor to me. I've tried but my program never stop @@ this so terrible. – Vincent May 30 '14 at 03:16