Click here to Skip to main content
15,899,679 members
Home / Discussions / C#
   

C#

 
GeneralRe: Setup and deployment Pin
jharker198725-Apr-08 2:26
jharker198725-Apr-08 2:26 
GeneralRe: Setup and deployment Pin
mav.northwind25-Apr-08 22:20
mav.northwind25-Apr-08 22:20 
GeneralListview interaction scheme question Pin
Spacix One24-Apr-08 12:55
Spacix One24-Apr-08 12:55 
QuestionNeed help with multi threaded modular application Pin
darthBug24-Apr-08 9:51
darthBug24-Apr-08 9:51 
GeneralRe: Need help with multi threaded modular application Pin
Luc Pattyn24-Apr-08 11:06
sitebuilderLuc Pattyn24-Apr-08 11:06 
GeneralRe: Need help with multi threaded modular application Pin
carbon_golem24-Apr-08 13:32
carbon_golem24-Apr-08 13:32 
GeneralRe: Need help with multi threaded modular application Pin
darthBug25-Apr-08 1:07
darthBug25-Apr-08 1:07 
GeneralRe: Need help with multi threaded modular application Pin
carbon_golem25-Apr-08 3:30
carbon_golem25-Apr-08 3:30 
Read the source code below. Not quite production, but you'll get the idea on how the ManualResetEvents work.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ManualResetExample {

    public class MockModule {

        public MockModule(String name) {
            Name = name;
        }

        public String Name { get; private set; }

        private Object syncLock = new Object();
        private EventHandler onDone;
        public event EventHandler OnDone {
            add {
                lock (syncLock) {
                    onDone += value;
                }
            }
            remove {
                lock (syncLock) {
                    onDone -= value;
                }
            }
        }
        private void FireOnDone(Object sender, EventArgs args) {
            if (onDone != null) {
                onDone(sender, args);
            }
        }

        public void StartWork() {
            ThreadPool.QueueUserWorkItem(new WaitCallback(WorkThread));
        }

        private void WorkThread(Object arg) {
            try {
                for (int i = 0; i < 5; i++) {
                    Thread.Sleep(250);
                    Console.WriteLine("{0} working", Name);
                }
            } finally {
                FireOnDone(this, new EventArgs());
            }
        }

    }

    public class WorkCompleteEventArgs : EventArgs {
        public WorkCompleteEventArgs(Boolean wasSuccessful) {
            Success = wasSuccessful;
        }
        public Boolean Success { get; private set; }
    }

    internal class ModuleSyncBlock {
        public ModuleSyncBlock(MockModule module) {
            Reset = new ManualResetEvent(false);
            MockModule = module;
        }
        public ManualResetEvent Reset { get; set; }
        public MockModule MockModule { get; set; }
    }

    public class ModuleManager {
        List<modulesyncblock> modules;
        List<mockmodule> mocks;

        public ModuleManager() {
            modules = new List<modulesyncblock>();
            mocks = new List<mockmodule>();
            //add some fake ones
            mocks.Add(new MockModule("One"));
            mocks.Add(new MockModule("Two"));
            mocks.Add(new MockModule("Three"));
            mocks.Add(new MockModule("Four"));
            mocks.Add(new MockModule("Five"));
        }

        
        public void StartAll() {
            ThreadPool.QueueUserWorkItem(new WaitCallback(RunInternal));   
        }

        private void RunInternal(Object arg) {
            List<manualresetevent> manuals = new List<manualresetevent>();
            mocks.ForEach(m => modules.Add(new ModuleSyncBlock(m)));
            modules.ForEach(m => manuals.Add(m.Reset));
            modules.ForEach(m => {
                m.MockModule.OnDone += new EventHandler(MockModule_OnDone);
                m.MockModule.StartWork();
            });
            if (WaitHandle.WaitAll(manuals.ToArray(), new TimeSpan(0, 0, 30), false)) {
                FireOnWorkComplete(this, new WorkCompleteEventArgs(true));
            } else {
                FireOnWorkComplete(this, new WorkCompleteEventArgs(false));
            }
        }
        void MockModule_OnDone(object sender, EventArgs e) {
            ModuleSyncBlock msb = modules.Find(m => ReferenceEquals(sender, m.MockModule));
            if (msb != null) {
                Console.WriteLine(msb.MockModule.Name + " Finished");
                msb.Reset.Set(); // signal that we're done.
            }
        }

        private Object syncLockA = new Object();
        private EventHandler<workcompleteeventargs> onWorkComplete;
        public event EventHandler<workcompleteeventargs> OnWorkComplete {
            add {
                lock (syncLockA) {
                    onWorkComplete += value;
                }
            }
            remove {
                lock (syncLockA) {
                    onWorkComplete -= value;
                }
            }
        }
        private void FireOnWorkComplete(Object sender, WorkCompleteEventArgs args) {
            if (onWorkComplete != null) {
                onWorkComplete(sender, args);
            }
        }

    }
}
</workcompleteeventargs></workcompleteeventargs></manualresetevent></manualresetevent></mockmodule></modulesyncblock></mockmodule></modulesyncblock>


"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

QuestionEnumeration Location Pin
snorkie24-Apr-08 9:42
professionalsnorkie24-Apr-08 9:42 
GeneralRe: Enumeration Location Pin
Broken Bokken24-Apr-08 9:47
Broken Bokken24-Apr-08 9:47 
GeneralRe: Enumeration Location Pin
darthBug24-Apr-08 10:01
darthBug24-Apr-08 10:01 
GeneralRe: Enumeration Location Pin
snorkie24-Apr-08 10:08
professionalsnorkie24-Apr-08 10:08 
GeneralRe: Enumeration Location Pin
carbon_golem24-Apr-08 10:53
carbon_golem24-Apr-08 10:53 
GeneralAdvice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
MumbleB24-Apr-08 7:06
MumbleB24-Apr-08 7:06 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
KaptinKrunch24-Apr-08 7:16
KaptinKrunch24-Apr-08 7:16 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
MumbleB24-Apr-08 7:26
MumbleB24-Apr-08 7:26 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
carbon_golem24-Apr-08 7:17
carbon_golem24-Apr-08 7:17 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
KaptinKrunch24-Apr-08 7:34
KaptinKrunch24-Apr-08 7:34 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
MumbleB24-Apr-08 7:40
MumbleB24-Apr-08 7:40 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
Ed.Poore24-Apr-08 8:45
Ed.Poore24-Apr-08 8:45 
GeneralRe: Advice required on Matching Input: Code copied from ARL(Advanced Reporting Language) Pin
MumbleB24-Apr-08 18:46
MumbleB24-Apr-08 18:46 
Generalproblems parsing [modified] Pin
stephan_00724-Apr-08 6:46
stephan_00724-Apr-08 6:46 
GeneralRe: problems parsing Pin
KaptinKrunch24-Apr-08 7:31
KaptinKrunch24-Apr-08 7:31 
GeneralRe: problems parsing Pin
Anthony Mushrow24-Apr-08 12:18
professionalAnthony Mushrow24-Apr-08 12:18 
Questionthe main class in C# Pin
A@YZ24-Apr-08 6:31
A@YZ24-Apr-08 6:31 

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.