using System; using System.Collections.Generic; using System.Threading; using System.Windows.Forms; using TBF.BenchControl; using TBF.BenchControl.Generic; namespace DeviceTest { public partial class DeviceTestDlg : Form { IList compClasses; IComponentFactory parentFactory; IComponentFactory componentFactory; IComponentCfg parentCfg; IComponentCfg componentCfg; static IList tbfComponents; static IList tbfDevices; static Thread workerThread; public DeviceTestDlg() { InitializeComponent(); compClasses = new List(); foreach (var componentFactory in TbfComponents.Factories) { compClasses.Add(componentFactory.ClassName); } } private void DeviceTestDlg_Load(object sender, EventArgs e) { startButton.Enabled = false; stopButton.Enabled = false; foreach (var clName in compClasses) { parentClassNameComboBox.Items.Add(clName); classNameComboBox.Items.Add(clName); } } private void configureParentButton_Click(object sender, EventArgs e) { bool factoryChanged = false; bool classNotFound = true; foreach (var factory in TbfComponents.Factories) { if (parentClassNameComboBox.Text == factory.ClassName) { if (parentFactory != factory) { parentFactory = factory; factoryChanged = true; } classNotFound = false; break; } } if (classNotFound) { MessageBox.Show("Invalid parent component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (factoryChanged || parentCfg == null) { parentCfg = parentFactory.DefaultConfig(); } IComponentCfgCtrl cfgControl = parentCfg.GetControl(); cfgControl.Config = parentCfg; cfgControl.Config.ItemNr = 0; TBF.ComponentParametersDlg cfgForm = new TBF.ComponentParametersDlg(); cfgForm.TbfComponents = new List(); cfgForm.ComponentCfgCtrl = cfgControl; cfgForm.UnlockAfterStart = true; DialogResult dr = cfgForm.ShowDialog(); if (dr != DialogResult.OK) return; parentNameTextBox.Text = parentCfg.Name; } private void configureButton_Click(object sender, EventArgs e) { bool factoryChanged = false; bool classNotFound = true; foreach (var factory in TbfComponents.Factories) { if (classNameComboBox.Text == factory.ClassName) { if (componentFactory != factory) { componentFactory = factory; factoryChanged = true; } classNotFound = false; break; } } if (classNotFound) { MessageBox.Show("Invalid parent component class", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } if (factoryChanged || componentCfg == null) { componentCfg = componentFactory.DefaultConfig(); } IComponentCfgCtrl cfgControl = componentCfg.GetControl(); cfgControl.Config = componentCfg; cfgControl.Config.ItemNr = 1; TBF.ComponentParametersDlg cfgForm = new TBF.ComponentParametersDlg(); /// IList compEntities = new List(); if (parentFactory != null && parentCfg != null) compEntities.Add(parentCfg.CreateDbEntity()); cfgForm.TbfComponents = compEntities; cfgForm.ComponentCfgCtrl = cfgControl; cfgForm.UnlockAfterStart = true; DialogResult dr = cfgForm.ShowDialog(); if (dr != DialogResult.OK) return; nameTextBox.Text = componentCfg.Name; } private void initializeButton_Click(object sender, EventArgs e) { tbfComponents = new List(); tbfDevices = new List(); try { if (parentFactory != null && parentCfg != null) { TBF.BenchControl.Generic.IComponent component = parentFactory.GetComponent(parentCfg, tbfComponents); tbfComponents.Add(component); if (component is IDevice) tbfDevices.Add(component as IDevice); } if (componentFactory != null && componentCfg != null) { TBF.BenchControl.Generic.IComponent component = componentFactory.GetComponent(componentCfg, tbfComponents); tbfComponents.Add(component); if (component is IDevice) tbfDevices.Add(component as IDevice); } if (tbfComponents.Count == 0) throw new Exception("There are no components"); foreach (var d in tbfDevices) d.Initialize(); workerThread = new Thread(Worker); workerThread.CurrentCulture = Thread.CurrentThread.CurrentCulture; workerThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture; workerThread.Start(); } catch (Exception exc) { MessageBox.Show(string.Format("Exception: {0}", exc.Message), "Initialization failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); configureButton.Enabled = true; configureParentButton.Enabled = true; startButton.Enabled = false; return; } configureButton.Enabled = false; configureParentButton.Enabled = false; startButton.Enabled = true; // MessageBox.Show("Hura !", "Initialization successful", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void startButton_Click(object sender, EventArgs e) { startButton.Enabled = false; stopButton.Enabled = true; } private void stopButton_Click(object sender, EventArgs e) { startButton.Enabled = true; stopButton.Enabled = false; configureButton.Enabled = false; configureParentButton.Enabled = false; } static void Worker() { do { Thread.Sleep(1000); foreach (var d in tbfDevices) d.RunDeviceBefore(); foreach (var d in tbfDevices) d.RunDeviceAfter(); } while (true); } } }