.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Events Sample in C#

Posted By:Karthikeyan Anbarasan       Posted Date: January 02, 2011    Points: 15    Category: OOPS    URL: http://www.dotnetspark.com  

Events Sample in C#
 

using System;
using System.Threading;
using System.Threading.Tasks;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            const int taskCount = 4;

            var manEvnts = new ManualResetEventSlim[taskCount];
            var waitHndls = new WaitHandle[taskCount];
            var calcs = new Calculator[taskCount];

            TaskFactory taskFactory = new TaskFactory();
            for (int i = 0; i < taskCount; i++)
            {
                manEvnts[i] = new ManualResetEventSlim(false);
                waitHndls[i] = manEvnts[i].WaitHandle;
                calcs[i] = new Calculator(manEvnts[i]);
                taskFactory.StartNew(calcs[i].Calculation, Tuple.Create(i + 1, i + 3));
            }
            for (int i = 0; i < taskCount; i++)
            {
                int index = WaitHandle.WaitAny(waitHndls);
                if (index == WaitHandle.WaitTimeout)
                {
                    Console.WriteLine("Timeout!!");
                }
                else
                {
                    manEvnts[index].Reset();
                    Console.WriteLine("finished task for {0}, result: {1}",
                                      index, calcs[index].Result);
                }
            }

        }
    }

    public class Calculator
    {
        private ManualResetEventSlim mEvent;
        private CountdownEvent cEvent;
        public int Result { get; private set; }
        public Calculator(ManualResetEventSlim ev)
        {
            this.mEvent = ev;
        }
        public Calculator(CountdownEvent ev)
        {
            this.cEvent = ev;
        }

        public void Calculation(object obj)
        {
            Tuple data = (Tuple)obj;
            Console.WriteLine("Task {0} starts calculation", Task.CurrentId);
            Thread.Sleep(new Random().Next(3000));
            Result = data.Item1 + data.Item2;
            Console.WriteLine("Task {0} is ready", Task.CurrentId);
            mEvent.Set();
        }
    }
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend