public void RemoveWorker(ProcessWorker pr)
 {
     lock (workers) { workers.Remove(pr); }
 }
        /// <summary>
        /// Initializes the module. Loads all modules and signals
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            //Instance of the simulator
            Simulator sim = Simulator.GetInstance();

            //Used for errors, false if errors
            bool ok = true;

            if (Parent != null)
            {
                Parent.Children.Add(this);

                this.name = Parent.Name + "." + this.name;

                int i = 0;
                foreach (ModuleBase mb in Parent.Children)
                {
                    if (mb.name.StartsWith(this.name))
                    {
                        i++;
                    }
                }
                this.name += i.ToString();
            }

            //Initialize arrays
            sigs = new List <SignalBase>();

            //Add handler for simulator first step
            sim.FirstStep += new EmptyHandler(First);

            //Add handler for simulator before stop
            sim.OnStop += new EmptyHandler(OnStop);

            //Add handler for simulator after loading
            sim.Load += new EmptyHandler(OnLoad);

            //Add module to simulator
            // sim.AddModule(this);

            //Holder for signals found
            Dictionary <string, SignalBase> signals = new Dictionary <string, SignalBase>();

            //All fields in this class
            FieldInfo[] fis = this.GetType().GetFields();

            //Find all signals in this module and store them
            foreach (FieldInfo fi in fis)
            {
                Type t = fi.FieldType;

                //Check if the type is a subclass of Signalbase
                if (t.IsSubclassOf(typeof(SignalBase)))
                {
                    //Get the value of the filed
                    object o = this.GetType().InvokeMember(fi.Name, BindingFlags.GetField, null, this, new object[] { });

                    //If the value is null there is something
                    if (o == null)
                    {
                        sim.SendError(this.GetType() + ": Connection Error: " + fi.Name + " has no instance.");
                        ok = false;
                        continue;
                    }

                    //Cast to signalBase
                    SignalBase c = (SignalBase)o;

                    //Sets the fullname of the signal
                    if (this.Name == null)
                    {
                        c.FullName = fi.Name;
                    }
                    else
                    {
                        c.FullName = this.Name + "." + fi.Name;
                    }

                    //Sets the name of the signal
                    c.Name = fi.Name;

                    //Adds the signal to the module
                    Signals.Add(c);

                    //Stores the signals
                    signals.Add(fi.Name, c);
                }
            }

            //Find all signals that are properties and store them
            PropertyInfo[] pis = this.GetType().GetProperties();
            foreach (PropertyInfo pi in pis)
            {
                Type t = pi.PropertyType;

                //Check if the type is a subclass of Signalbase
                if (t.IsSubclassOf(typeof(SignalBase)))
                {
                    //Get the value of the filed
                    object o = pi.GetValue(this, new object[] { });

                    //If the value is null there is something
                    if (o == null)
                    {
                        sim.SendError(this.GetType() + ": Connection Error: " + pi.Name + " has no instance.");
                        ok = false;
                        continue;
                    }

                    //Cast to signalBase
                    SignalBase c = (SignalBase)o;

                    if (Signals.Contains(c))
                    {
                        continue;
                    }

                    //Sets the fullname of the signal
                    if (this.Name == null)
                    {
                        c.FullName = pi.Name;
                    }
                    else
                    {
                        c.FullName = this.Name + "." + pi.Name;
                    }

                    //Sets the name of the signal
                    c.Name = pi.Name;

                    //Adds the signal to the module
                    Signals.Add(c);

                    //Stores the signals
                    signals.Add(pi.Name, c);
                }
            }

            //Get all methods
            MethodInfo[] mis = this.GetType().GetMethods();
            //EmptyHandler eh;
            ProcessWorker pw;

            foreach (MethodInfo mi in mis)
            {
                object[] o = mi.GetCustomAttributes(typeof(ProcessAttribute), true);
                //If the method uses the process attribute
                if (o.Length > 0)
                {
                    //Run through all sensitive attributes
                    foreach (ProcessAttribute sa in o)
                    {
                        //Run through all signal names in process attribute
                        foreach (string name in sa.SignalNames)
                        {
                            if (signals.ContainsKey(name))
                            {
                                //If the method is sensitive to the signal add it to the signals list of methods to run
                                signals[name].SensType = sa.SensType;
                                //eh = (EmptyHandler)Delegate.CreateDelegate(typeof(EmptyHandler), this, mi.Name);
                                pw = new ProcessWorker(this, mi);
                                signals[name].Changed += new EmptyHandler(pw.Run);
                            }
                            else
                            {
                                //If the signal was not found call an error
                                sim.SendError(this.GetType() + ": Senstitivity Error: Could not find signal " + name + " that is needed by " + mi.Name);
                                ok = false;
                            }
                        }
                    }
                }
            }

            if (!ok)
            {
                throw new CommandException("Initialize of " + this.Name + " failed");
            }

            InitComplete = true;
        }
 public void AddWorker(ProcessWorker pr)
 {
     lock (workers) { workers.Add(pr); }
 }