public Task Go() { Type t = this.GetType(); Name = t.FullName; _network = this; _netObject = new Object(); // try //{ // Monitor.Enter(_netObject); //Thread.CurrentThread.Name = "Go"; InitBlock(); // moved up DateTime now = DateTime.Now; // _traceFileList = new List<StreamWriter>(); _traceFileDictionary = new Dictionary<string, StreamWriter>(); //Settings d = Settings.Default; // _tracing = d.Tracing; // get value from Properties for FBPLib /* if (_tracing && !_forceConsole && !_useConsole) { try { //instantiate one _traceWriter for each level of the Network and its SubNets lock (_traceObject) { var newTw = GetOrMakeTraceWriter(n); } Trace($"Trace file = {s}"); } catch (Exception e) { // file cannot be created or opened, so _useConsole lock (_network) // TODO: review is this lock really needed? { Console.Out.WriteLine("Trace file [" + s + "]\n could not be opened, so writing all tracing to console..."); Console.Out.Flush(); _useConsole = true; } throw; // Temporary, to know it happened. TODO: review this } } */ //InitBlock(); return Task.Factory.StartNew(() => { try { CallDefine(); bool res = true; Thread.CurrentThread.Name = "NetworkGo"; foreach (Component comp in _components.Values) { res &= comp.CheckPorts(); } if (!res) FlowError.Complain("One or more mandatory connections have been left unconnected: " + Name); _cdl = new CountDownLatch(_components.Count); Trace(Name + ": run started"); _active = true; Initiate(); WaitForAll(); } catch (FlowError e) { string s = "Flow Error :" + e; Console.Out.WriteLine("Network: " + s); Console.Out.Flush(); // rethrow the exception for external error handling // in case of a deadlock: deadlock is the cause throw e; } if (_error != null) { // throw the exception which caused the network to stop throw _error; } TimeSpan duration = DateTime.Now - now; Console.Out.WriteLine("{0} - run time: {1}", Name, duration); Console.Out.WriteLine("Counts: C: {0}, D: {1}, S: {2}, R (non-null): {3}, DO: {4}", creates, drops, sends, receives, dropOlds); Console.Out.WriteLine("End of job"); //lock(Network._traceObject) // add lock //{ // Console.Out.Flush(); // Flush to write trace JPM //Thread.Sleep(500); CloseTraceFiles(); // JPM }); }
public override void Execute() { OutputPort subEndPort = null; if (_status != States.Error) { //_network.Trace(this.Name + ": started"); _components.Clear(); //_tracing = _mother._tracing; //_traceFileList = _mother._traceFileList; subEndPort = _outputPorts["*SUBEND"]; try { CallDefine(); bool res = true; foreach (Component comp in _components.Values) { res &= comp.CheckPorts(); } if (!res) FlowError.Complain("One or more mandatory connections have been left unconnected: " + Name); _cdl = new CountDownLatch(_components.Count); Initiate(); // activateAll(); // don't do deadlock testing in subnets - you need to consider the whole net! _deadlockTest = false; WaitForAll(); foreach (IInputPort ip in _inputPorts.Values) if (ip is InitializationConnection) { InitializationConnection ic = (InitializationConnection)ip; ic.Close(); } /* * Iterator allout = (outputPorts.values()).iterator(); * * while (allout.hasNext()) { * * * OutputPort op = (OutputPort) allout.next(); op.close(); * * } */ // status = Component.StatusValues.TERMINATED; //will not be set if // never activated // mother.indicateTerminated(this); _network.Trace(this.Name + ": closed down"); if (subEndPort != null) { subEndPort.Send(Create(null)); } } catch (FlowError e) { string s = "Flow Error :" + e; Console.Out.WriteLine("Network: " + s); throw e; } } }
[MTAThreadAttribute] // this the default, so not really necessary public void Go() { Type t = this.GetType(); Name = t.FullName; _network = this; DateTime now = DateTime.Now; InitBlock(); _mainthread = new Thread(delegate() { try { CallDefine(); bool res = true; foreach (Component comp in _components.Values) { res &= comp.CheckPorts(); } if (!res) FlowError.Complain("One or more mandatory connections have been left unconnected: " + Name); _cdl = new CountDownLatch(_components.Count); Trace(Name + ": run started"); _active = true; Initiate(); WaitForAll(); } catch (FlowError e) { string s = "Flow Error :" + e; Console.Out.WriteLine("Network: " + s); Console.Out.Flush(); // rethrow the exception for external error handling // in case of a deadlock: deadlock is the cause throw e; } if (_error != null) { // throw the exception which caused the network to stop throw _error; } TimeSpan duration = DateTime.Now - now; Console.Out.WriteLine("{0} - run time: {1}", Name, duration); Console.Out.WriteLine("Counts: C: {0}, D: {1}, S: {2}, R (non-null): {3}, DO: {4}", creates, drops, sends, receives, dropOlds); CloseTraceFiles(); }); _mainthread.Start(); }