Click here to Skip to main content
15,895,142 members
Home / Discussions / C#
   

C#

 
GeneralRe: move a file from one directory to another [modified] Pin
Yustme28-Feb-10 22:35
Yustme28-Feb-10 22:35 
GeneralRe: move a file from one directory to another Pin
Yustme1-Mar-10 0:09
Yustme1-Mar-10 0:09 
GeneralRe: move a file from one directory to another Pin
Luc Pattyn1-Mar-10 0:56
sitebuilderLuc Pattyn1-Mar-10 0:56 
GeneralRe: move a file from one directory to another Pin
Yustme1-Mar-10 3:05
Yustme1-Mar-10 3:05 
GeneralRe: move a file from one directory to another Pin
Yustme1-Mar-10 12:09
Yustme1-Mar-10 12:09 
Questionhelp with threads and locks [modified] Pin
codeguruk28-Feb-10 9:23
codeguruk28-Feb-10 9:23 
AnswerRe: help with threads and locks Pin
Saksida Bojan28-Feb-10 19:50
Saksida Bojan28-Feb-10 19:50 
Generalthis is the code i use to check it: Pin
codeguruk28-Feb-10 21:09
codeguruk28-Feb-10 21:09 
input is: 1,2,3,4,5,6,7,8,9,10 (several times)

namespace Test
{
    public class SyncQueue<T>
    {
        private Queue<T> queue;
        private object readerLock;
        private object queueLock;
        private AutoResetEvent dataAvailable;

        public SyncQueue()
        {
            this.queue = new Queue<T>();
            this.readerLock = new object();
            this.queueLock = new object();
            this.dataAvailable = new AutoResetEvent(false);
        }

        public void Enqueue(T item)
        {
            lock (queueLock)
            {
                queue.Enqueue(item);
                Console.WriteLine("Enqueued Item: " + item);
            }
            dataAvailable.Set();
        }

        public T Dequeue()
        {
            lock (readerLock)
            {
                if (queue.Count == 0)
                {
                    dataAvailable.WaitOne();
                }
                lock (queueLock)
                {
                    return queue.Dequeue();
                }
            }
        }
    }

    public class Tester
    {
        SyncQueue<int> myQ = null;

        public Tester(SyncQueue<int> queue)
        {
            myQ = queue;
        }

        public void ProcessMessages(object obj)
        {
            while (true)
            {
                Console.WriteLine(myQ.Dequeue());
            }
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            SyncQueue<int> myQ = new SyncQueue<int>();
            Tester t1 = new Tester(myQ);
            Tester t2 = new Tester(myQ);
            Tester t3 = new Tester(myQ);
            ThreadPool.QueueUserWorkItem(new WaitCallback(t1.ProcessMessages));
            ThreadPool.QueueUserWorkItem(new WaitCallback(t2.ProcessMessages));
            ThreadPool.QueueUserWorkItem(new WaitCallback(t3.ProcessMessages));

            while (true)
            {
                string[] snumbs = Console.ReadLine().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string snumb in snumbs)
                {
                    myQ.Enqueue(int.Parse(snumb));
                }
            }
        }
    }
}



the exception is: Invalid Operation Exception - queue is empty.

any idea?

thanks,
GeneralRe: this is the code i use to check it: Pin
Saksida Bojan28-Feb-10 22:42
Saksida Bojan28-Feb-10 22:42 
GeneralRe: this is the code i use to check it: Pin
codeguruk1-Mar-10 2:08
codeguruk1-Mar-10 2:08 
GeneralRe: this is the code i use to check it: Pin
Saksida Bojan1-Mar-10 3:21
Saksida Bojan1-Mar-10 3:21 
GeneralRe: this is the code i use to check it: Pin
codeguruk1-Mar-10 5:41
codeguruk1-Mar-10 5:41 
Questionhow could I print search result in a form with a data grid view? Pin
ronakT28-Feb-10 8:48
ronakT28-Feb-10 8:48 
AnswerRe: how could I print search result in a form with a data grid view? Pin
Dan Mos28-Feb-10 9:28
Dan Mos28-Feb-10 9:28 
GeneralRe: how could I print search result in a form with a data grid view? Pin
ronakT1-Mar-10 0:20
ronakT1-Mar-10 0:20 
QuestionDiscovering what a registry value is stored as Pin
Carl Mailey28-Feb-10 8:13
Carl Mailey28-Feb-10 8:13 
AnswerRe: Discovering what a registry value is stored as Pin
Richard MacCutchan28-Feb-10 9:00
mveRichard MacCutchan28-Feb-10 9:00 
AnswerRe: Discovering what a registry value is stored as Pin
Luc Pattyn28-Feb-10 11:15
sitebuilderLuc Pattyn28-Feb-10 11:15 
AnswerRe: Discovering what a registry value is stored as Pin
Saksida Bojan28-Feb-10 20:19
Saksida Bojan28-Feb-10 20:19 
Questioncross thread Pin
Yustme28-Feb-10 6:28
Yustme28-Feb-10 6:28 
AnswerRe: cross thread Pin
Dan Mos28-Feb-10 6:33
Dan Mos28-Feb-10 6:33 
GeneralRe: cross thread Pin
Yustme28-Feb-10 6:35
Yustme28-Feb-10 6:35 
GeneralRe: cross thread [modified] Pin
Dan Mos28-Feb-10 6:44
Dan Mos28-Feb-10 6:44 
GeneralRe: cross thread Pin
Alex Manolescu28-Feb-10 7:10
Alex Manolescu28-Feb-10 7:10 
GeneralRe: cross thread [modified] Pin
Dan Mos28-Feb-10 7:27
Dan Mos28-Feb-10 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.