public override void Start() { string embedDriverArgs = moduleInfo.Args()[0]; string comPortName = embedDriverArgs; if (!String.IsNullOrEmpty(comPortName)) { if (comPortName.Contains(MBED)) { comPortName = comPortName.Replace(MBED + " - ", ""); } this.portName = comPortName; } connectionChecker = new SafeThread(delegate() { CheckComConnection(); }, "ComPort connection-checker", logger); if (InitializePort() == true) { try { serialPort.Open(); connectionChecker.Start(); while (!connectionChecker.IsAlive()) ; } catch (Exception) { List<COMPortFinder> comportList = COMPortFinder.GetCOMPortsInfo(); foreach (COMPortFinder comPortInfo in comportList) { if (comPortInfo.Description.Contains(MBED)) { this.portName = comPortInfo.Name; break; } } InitializePort(); //serialPort.Open(); } //testPort(); } //.................instantiate the port VPortInfo portInfo = GetPortInfoFromPlatform(embedDriverArgs); mbedPort = InitPort(portInfo); // ..... initialize the list of roles we are going to export and bind to the role List<VRole> listRole = new List<VRole>() { RoleMbedSoftUps.Instance }; BindRoles(mbedPort, listRole); //.................register the port after the binding is complete RegisterPortWithPlatform(mbedPort); }
/// <summary> /// Send a port deregisterd message to all active modules /// </summary> /// <param name="port"></param> /// <param name="owner"></param> public void BroadcastPortDeregistration(VPort port, VModule owner) { Dictionary<String, SafeThread> listOfThreads = new Dictionary<string, SafeThread>() ; lock (this) { foreach (VModule module in runningModules.Keys) { if (!module.Equals(owner)) { //***< Thread that monitors and timesout the port deregistered thread SafeThread mPortDeregThreadControl = new SafeThread(delegate() { //***< Thread that invokes port deregistered on the module SafeThread mPortDeregThread = new SafeThread(delegate() { InvokePortDeregistered(module, port); }, module + ".PortDeregistered(" + port + ")", logger); //***> mPortDeregThread.Start(); mPortDeregThread.Join(TimeSpan.FromMilliseconds(Settings.MaxPortDeregisteredExecutionTime)); try { if (mPortDeregThread.IsAlive()) mPortDeregThread.Abort(); } catch (Exception) { //The PortDeregistered() calls when aborted (if so) will raise an exception } } , module + ".PortDeregistered(" + port + ") - Control" , logger); //*** listOfThreads[mPortDeregThreadControl.Name()] = mPortDeregThreadControl; //store the list because we want to wait on these later mPortDeregThreadControl.Start(); } } } foreach (SafeThread t in listOfThreads.Values) { t.Join(); } }
private void AddInCleanup(VModule moduleStopped) { //cleanup if (Constants.ModuleIsolationLevel == ModuleIsolationModes.AppDomain) { logger.Log("AppDomain cleanup for "+ moduleStopped.GetInfo().AppName()); bool done = false; AddInController aiController = AddInController.GetAddInController(moduleStopped); SafeThread t = new SafeThread(delegate() { while (!done) { try { aiController.Shutdown(); done = true; } catch (CannotUnloadAppDomainException) { logger.Log("AppDomain Unload did not succeed. Retrying."); System.Threading.Thread.Sleep(1000); // keep trying to unload until it gets unloaded } } }, moduleStopped.ToString()+"-UnloadingAppDomain", logger); t.Start(); t.Join(TimeSpan.FromMilliseconds(Settings.MaxFinallyBlockExecutionTime)); if(t.IsAlive()) t.Abort(); } else if (Constants.ModuleIsolationLevel == ModuleIsolationModes.Process) { //TODO: test this AddInController aiController = AddInController.GetAddInController(moduleStopped); aiController.Shutdown(); } else if (Constants.ModuleIsolationLevel == ModuleIsolationModes.NoAddInAppDomain) { // TODO handle cleanup here } else {// Globals.ModuleIsolationLevel == ModuleIsolationModes.None // TODO handle cleanup here } }
public bool StopModule(VModule moduleToStop) { //*** try { SafeThread t = new SafeThread(delegate() { moduleToStop.Stop(); }, moduleToStop.GetInfo().AppName() + "stop thread", logger); // invoke stop on the module t.Start(); t.Join(TimeSpan.FromMilliseconds(Settings.MaxStopExecutionTime)); if (t.IsAlive()) t.Abort(); ModuleFinished(moduleToStop); // dereg its ports broadcast ports' dereg; wipe module off data structures AddInCleanup(moduleToStop); // addin cleanup return true; } catch (Exception e) { logger.Log("Exception in stopping of module: " + e); return false; } //*** }