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:
- You should never lock on a static variable, you should modify to use parameters and reduce the access level of the variables.
- 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. - I uses static methods for simplycity in the example, you'll have to modify back to instance methods.