DeviceRunControl.cs 3.7 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeviceLibrary
{
    public class DeviceRunControl
    {
        public static List<ManualResetEvent> manualResets = new List<ManualResetEvent>();
        public static List<DeviceRunControl> DeviceRunControlList = new List<DeviceRunControl>();
        List<IDevice> DevicesList;
        string DeviceListName;
        ManualResetEvent resetEvent = new ManualResetEvent(false);
        public DeviceRunControl(string name, List<IDevice> device) {
            DevicesList = device;
            DeviceListName = name;
            manualResets.Add(resetEvent);
            LogUtil.info($"{DeviceListName}导入设备{DevicesList.Count}个");
        }
        Thread thread;
        public void Start() {
            thread = new Thread(new ThreadStart(Run));
            thread.Start();
            resetEvent.Reset();
        }
        void Run() {
            LogUtil.info($"{DeviceListName} 设备线程启动");
            while (RobotManage.mainMachine.mstart)
            {
                try
                {
                    Thread.Sleep(200);
                    ManualResetEvent.WaitAll(new ManualResetEvent[] { RobotManage.mainMachine.ResetEvent });
                    
                    Debug.WriteLine($"DeviceRunControl:{DeviceListName} canRunning:{RobotManage.mainMachine.canRunning}, mstart:{RobotManage.mainMachine.mstart}");
                    if (!RobotManage.mainMachine.canRunning || !RobotManage.mainMachine.mstart)
                    {
                        if (RobotManage.mainMachine.mstart)
                        {
                            DevicesList.ForEach(x =>
                            {
                                x.FrontStopProcess();
                            });
                        }
                        continue;
                    }
                    DevicesList.ForEach(x =>
                    {
                        try
                        {
                            x.Process();
                        }
                        catch (Exception ex)
                        {
                            LogUtil.error($"{DeviceListName} 出错:" + ex);
                            MsgService.MSList[x.GroupName].add(ex.ToString(), MsgLevel.warning);
                            MsgService.MSList[x.GroupName].setlogones();
                        }
                        finally
                        {
                            MsgService.MSList[x.GroupName].Show();
                        }
                    });

                    if (!RobotManage.mainMachine.UserPause && RobotManage.mainMachine.mstart)
                    {
                        DevicesList.ForEach(x =>
                        {
                            MsgService.MSList[x.GroupName].clear();
                        });
                    }
                }
                catch(Exception ex) {
                    LogUtil.error($"{DeviceListName} 设备线程出错:"+ex);
                }
                finally
                {
                    resetEvent.Set();
                }
            }
            LogUtil.info($"{DeviceListName} 设备线程已退出.");
        }

        public static void AddDevice(string name, List<IDevice> devices)
        {
            DeviceRunControlList.Add(new DeviceRunControl(name, devices));
        }
        public static void StartAll()
        {
            DeviceRunControlList.ForEach(x => { x.Start(); });
        }
    }

}