DeviceRunControl.cs 2.4 KB
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
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>();
        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)
            {
                Thread.Sleep(200);
                ManualResetEvent.WaitAll(new ManualResetEvent[] { RobotManage.mainMachine.ResetEvent });
                if (!RobotManage.mainMachine.canRunning || !RobotManage.mainMachine.mstart)
                    continue;
                DevicesList.ForEach(x =>
                {
                    try
                    {
                        x.Process();
                    }
                    catch (Exception ex)
                    {
                        MsgService.MSList[x.GroupName].add(ex.ToString(), MsgLevel.warning);
                        MsgService.MSList[x.GroupName].setlogones();
                    }
                    finally
                    {
                        MsgService.MSList[x.GroupName].Show();
                    }
                });

                //ProcessMoveinfoEvent?.Invoke(MoveInfo.List);
                if (!RobotManage.mainMachine.UserPause)
                {
                    DevicesList.ForEach(x =>
                    {
                        MsgService.MSList[x.GroupName].clear();
                    });
                }
                resetEvent.Set();
                
            }
            LogUtil.info($"{DeviceListName}设备线程已退出.");
        }
    }
}