HI,

I want to implemtna a file & filter architecture for reading data from a txt file and then removing the non alpahbatical words in it and display the 20 words in alphabatical order in the Consol window.

how do i do it.

appreciate a reply,
thanks

atleast give it a go. Report the problems you have trying and people will be able to help you.

Hint: BinaryReader

i created as a single thread of control, how do i make it concurrent, i know i have to use threads,
but i don't know how to

main program

 static void Main(string[] args)
        {
            Queue<string> qp1 = new Queue<string>();

            ReadF f1 = new ReadF(qp1);
            f1.run();

            Console.WriteLine("Done");
            Console.ReadLine();            
        }

read from file

 class ReadF
    {

         Queue<string> myQ1 = new Queue<string>();
         StopperF f2 = new StopperF();

         public ReadF(Queue<string> qp1)
        {
            this.myQ1 = qp1;
        }

        public void run( )
        {
            int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("C:\\SE480\\DataFiles\\Text2.txt");
            while ((line = file.ReadLine()) != null)
            {               
                myQ1.Enqueue(line);
                f2.run(myQ1);
                myQ1.Dequeue();
                //Console.WriteLine(line);
                counter++;
            }

            file.Close();

            // Suspend the screen.
            //Console.ReadLine();
        }
    }

is this concurrent or a simgle thread

No, that is a single thread. Do a Google search for thread tutorials, there are plenty out there. You generally use the Thread class, although there are others, such as the BackgroundWorker class. Here's a quick example that executes a Run() method on the main thread (the thread your program starts with) and a newly created thread:

using System;
using System.Threading;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initializes a new thread that uses the Run() method.
            Thread thread2 = new Thread(Run);

            Console.WriteLine("Starting thread...");

            //Starts the new thread.
            thread2.Start("Thread 2");

            //Call Run() under the main thread.
            Run("Thread 1");

            //Waits for the second thread to finish executing.
            thread2.Join();

            Console.WriteLine("Done!");
            Console.ReadKey(true);
        }

        static void Run(object obj)
        {
            Random random = new Random();
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("{0}: {1}", obj, i);
                //Forces the thread to sleep for a random number of milliseconds (less than 500).
                Thread.Sleep(random.Next(500));
            }
        }
    }
}

yes,i could use the split funtion
but if i have number how to i add it to the split function

i created a program but it dosen't work as concurrent, what am i doing wrong.
main.cs

static void Main(string[] args)
        {

            Queue<string> p1 = new Queue<string>();
            Queue<string> p2 = new Queue<string>();
            Queue<string> p3 = new Queue<string>();

            Filter1 f1 = new Filter1();

            Thread t = new Thread(f1.Run);
            t.Start("Thread 1");
           // f1.Run("Thread 2");

            Console.ReadKey(true);

            //Filter2 f2 = new Filter2(p1, p2);
            //Filter3 f3 = new Filter3(p2, p3);
            //Filter4 f4 = new Filter4(p3, null);

        }

Filter1.cs

 class Filter1
    {
        public Queue<string> p1 = new Queue<string>();



        public Filter1()
        {

        }

        public  void Run(Object o)
        {

            Filter2 f2;

            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file = new System.IO.StreamReader("C:\\a.txt");
            while ((line = file.ReadLine()) != null)
            {
                for (int i = 0; i < 3; i++)
                {

                    Console.WriteLine("{0}: {1} ", o, line);
                    p1.Enqueue(line);

                       f2 = new Filter2(p1);
                       f2.Run("Thread 3");
                       p1.Dequeue();
                       Console.WriteLine("dequeue");
                }
            }

            file.Close();
        }

    }

Filter2.cs

 class Filter2
    {
        Queue<string> p2in = new Queue<string>();
        Queue<string> p2out = new Queue<string>();

        public Filter2(Queue<string> pipe2)
        {
            foreach (string l in pipe2)
            {
                p2in.Enqueue(l);
            }
            p2out = null;
        }
        public void Run(Object o)
        {
            foreach (string line in p2in)
            {
                string[] split = line.Split(new Char[] { ' ', ',', '.', ':', '\t' });

                foreach (string s in split)
                {
                    if (s.Trim() != "")
                        Console.WriteLine("{0} {1}", o, s);
                }
            }

        }

    }

apprecite a reply, thanks

hey ddanbe,
the problem with what you suggested is that if a sentence has space and a ' it will only slit by the space,

any idean how to slitt by both

below is the way i used
string[] split = value.Split(new Char[] { ' ', ',', '.', ':', '\t','-' });

You can add an ' to the char array by using '\'', if that's what you're asking (see here).

As for your program, you only created one additional thread, then never really did anything with the main thread. Here's a quick example of how a threaded application could look:

static void Main(string[] args)
{
    //Initialize threads.
    Thread thread1 = new Thread(SomeMethod1);
    Thread thread2 = new Thread(SomeMethod2);
    Thread thread3 = new Thread(SomeMethod3);

    //Start threads.
    thread1.Start();
    thread2.Start();
    thread3.Start();

    //Wait for threads to finish.
    thread1.Join();
    thread2.Join();
    thread3.Join();
}

So, I'm assuming you want Filter2 to run concurrently with Filter1. You simply called the Run() method in Filter2, which doesn't cause a new thread to be created. Generally, with threads you also wouldn't want to do this. It would be very inefficient to create a new thread to process each iteration of your current thread, and could also lead to concurrency issues, since they would be working with the same data source.

Here's a quick example of what you could do in your situation:

class Program
{
    static Queue<string> q1;
    static Queue<string> q2;

    static Thread thread1;
    static Thread thread2;

    static void Main(string[] args)
    {
        q1 = new Queue<string>();
        q2 = new Queue<string>();

        thread1 = new Thread(Filter1);
        thread2 = new Thread(Filter2);

        thread1.Start();
        thread2.Start();

        thread1.Join();
        thread2.Join();

        Console.WriteLine("Done!");
        Console.ReadKey(true);
    }

    static void Filter1()
    {
        for (int i = 0; i < 20; i++)
        {
            string str = i.ToString();
            //Queues are not thread safe, so take a lock.
            lock (q1)
            {
                q1.Enqueue(str);
            }
            Console.WriteLine("Added: {0}", str);
        }
        Console.WriteLine("Thread 1 done...");
    }

    static void Filter2()
    {
        //The return statement will handle stopping the thread.
        while (true)
        {
            string str = null;
            bool read;
            lock (q1)
            {
                //Store bool variable to check later.
                read = q1.Count > 0;
                if (read)
                    str = q1.Dequeue();
                //Check inside the lock so that thread1 cannot add an item to the Queue and stop after our Count check.
                else if (!thread1.IsAlive)
                {
                    Console.WriteLine("Thread 2 done...");
                    return;
                }
            }
            //Only process if read.
            if (read)
            {
                str = int.Parse(str) % 2 == 0 ? "even" : "odd";
                lock (q2)
                {
                    q2.Enqueue(str);
                }
                Console.WriteLine("Added: {0}", str);
            }
            else
                //Optional, but reduces CPU load.
                Thread.Sleep(100);
        }
    }
}

Obviously it is not your program, but it should give you a good starting point. The basic idea is that each thread has a Queue that handles its results, and another thread checks that Queue, processes it and stores it in its own Queue.

A couple things to note here:

  1. You should never lock on a static variable, you should modify to use parameters and reduce the access level of the variables.
  2. Using Thread.Sleep() is an okay method of reducing CPU load. Removing it causes continuous polling, and can be inefficient, as it must take a lock on q1 each time. Using it causes delayed polling, which is better IMO, but can also be inefficient. Using something like AutoResetEvent gives you an interupt style approach, and would be what I recommend, although it would take a bit more work to get working correctly.
  3. I uses static methods for simplycity in the example, you'll have to modify back to instance methods.
This article has been dead for over six months. Start a new discussion instead.