Browse Source

使用Timer进行任务的定时控制

章政 7 years ago
parent
commit
cd57b680c1

+ 1 - 1
UAS_DeviceMonitor/CustomerControl/Button/ButtonDeleteRow.cs

@@ -43,7 +43,7 @@ namespace UAS_DeviceMonitor.CustomerControl.Button
                 GridView view = grid.MainView as GridView;
                 for (int i = 0; i < grid.RowCount; i++)
                 {
-                    string Checked = view.GetRowCellValue(i, "CHOOSE").ToString().ToUpper();
+                    string Checked = view.GetRowCellValue(i, view.Name.ToUpper() + "CHOOSE").ToString().ToUpper();
                     if (Checked != "0")
                         DeleteID.Add(view.GetRowCellValue(i, grid.ID.ToUpper()).ToString());
                 }

+ 30 - 0
UAS_DeviceMonitor/Entity/Polling.cs

@@ -7,6 +7,8 @@ namespace UAS_DeviceMonitor.Entity
 {
     class Polling
     {
+        private string code;
+
         private string deviceCode;
 
         private string iP;
@@ -15,6 +17,8 @@ namespace UAS_DeviceMonitor.Entity
 
         private int interval;
 
+        private int id;
+
         private bool enable;
 
         public string DeviceCode
@@ -81,5 +85,31 @@ namespace UAS_DeviceMonitor.Entity
                 enable = value;
             }
         }
+
+        public string Code
+        {
+            get
+            {
+                return code;
+            }
+
+            set
+            {
+                code = value;
+            }
+        }
+
+        public int Id
+        {
+            get
+            {
+                return id;
+            }
+
+            set
+            {
+                id = value;
+            }
+        }
     }
 }

+ 45 - 32
UAS_DeviceMonitor/Main.Designer.cs

@@ -69,7 +69,7 @@ namespace UAS_DeviceMonitor
             this.ButtonSaveCommand = new UAS_DeviceMonitor.CustomerControl.Button.ButtonSaveGrid();
             this.GridCommandSetting = new UAS_DeviceMonitor.CustomerControl.AutoDataGridControl.AutoDataGridControl();
             this.GridViewCommandSet = new UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum.GridViewWithSerialNum();
-            this.Choose = new DevExpress.XtraGrid.Columns.GridColumn();
+            this.GRIDVIEWCOMMANDSETCHOOSE = new DevExpress.XtraGrid.Columns.GridColumn();
             this.CheckEditCommandSet = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.dc_id = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dc_code = new DevExpress.XtraGrid.Columns.GridColumn();
@@ -85,6 +85,8 @@ namespace UAS_DeviceMonitor
             this.ButtonStartPolling = new DevExpress.XtraEditors.SimpleButton();
             this.GridPollingSetting = new UAS_DeviceMonitor.CustomerControl.AutoDataGridControl.AutoDataGridControl();
             this.GridViewPollSetting = new UAS_DeviceMonitor.CustomerControl.GridViewWithSerialNum.GridViewWithSerialNum();
+            this.GRIDVIEWPOLLSETTINGCHOOSE = new DevExpress.XtraGrid.Columns.GridColumn();
+            this.dpc_enableCheckEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.dpc_id = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_decode = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_plcode = new DevExpress.XtraGrid.Columns.GridColumn();
@@ -94,7 +96,6 @@ namespace UAS_DeviceMonitor
             this.PollingSetItemLookUpEdit = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
             this.dpc_function = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_enable = new DevExpress.XtraGrid.Columns.GridColumn();
-            this.dpc_enableCheckEdit = new DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit();
             this.dpc_status = new DevExpress.XtraGrid.Columns.GridColumn();
             this.dpc_remark = new DevExpress.XtraGrid.Columns.GridColumn();
             this.PagePolling = new DevExpress.XtraTab.XtraTabPage();
@@ -124,8 +125,8 @@ namespace UAS_DeviceMonitor
             this.PagePollingSetting.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.GridPollingSetting)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.GridViewPollSetting)).BeginInit();
-            ((System.ComponentModel.ISupportInitialize)(this.PollingSetItemLookUpEdit)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.dpc_enableCheckEdit)).BeginInit();
+            ((System.ComponentModel.ISupportInitialize)(this.PollingSetItemLookUpEdit)).BeginInit();
             this.PagePolling.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.GridPolling)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.GridViewPolling)).BeginInit();
@@ -488,7 +489,7 @@ namespace UAS_DeviceMonitor
             // GridViewCommandSet
             // 
             this.GridViewCommandSet.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
-            this.Choose,
+            this.GRIDVIEWCOMMANDSETCHOOSE,
             this.dc_id,
             this.dc_code,
             this.dc_name,
@@ -498,14 +499,14 @@ namespace UAS_DeviceMonitor
             this.GridViewCommandSet.Name = "GridViewCommandSet";
             this.GridViewCommandSet.OptionsSelection.MultiSelect = true;
             // 
-            // Choose
+            // GRIDVIEWCOMMANDSETCHOOSE
             // 
-            this.Choose.Caption = "勾选";
-            this.Choose.ColumnEdit = this.CheckEditCommandSet;
-            this.Choose.FieldName = "CHOOSE";
-            this.Choose.Name = "Choose";
-            this.Choose.Visible = true;
-            this.Choose.VisibleIndex = 0;
+            this.GRIDVIEWCOMMANDSETCHOOSE.Caption = "勾选";
+            this.GRIDVIEWCOMMANDSETCHOOSE.ColumnEdit = this.CheckEditCommandSet;
+            this.GRIDVIEWCOMMANDSETCHOOSE.FieldName = "GRIDVIEWCOMMANDSETCHOOSE";
+            this.GRIDVIEWCOMMANDSETCHOOSE.Name = "GRIDVIEWCOMMANDSETCHOOSE";
+            this.GRIDVIEWCOMMANDSETCHOOSE.Visible = true;
+            this.GRIDVIEWCOMMANDSETCHOOSE.VisibleIndex = 0;
             // 
             // CheckEditCommandSet
             // 
@@ -648,6 +649,7 @@ namespace UAS_DeviceMonitor
             // GridViewPollSetting
             // 
             this.GridViewPollSetting.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
+            this.GRIDVIEWPOLLSETTINGCHOOSE,
             this.dpc_id,
             this.dpc_decode,
             this.dpc_plcode,
@@ -662,6 +664,26 @@ namespace UAS_DeviceMonitor
             this.GridViewPollSetting.IndicatorWidth = 30;
             this.GridViewPollSetting.Name = "GridViewPollSetting";
             this.GridViewPollSetting.OptionsSelection.MultiSelect = true;
+            this.GridViewPollSetting.CellValueChanging += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(this.GridViewPollSetting_CellValueChanging);
+            // 
+            // GRIDVIEWPOLLSETTINGCHOOSE
+            // 
+            this.GRIDVIEWPOLLSETTINGCHOOSE.Caption = "选择";
+            this.GRIDVIEWPOLLSETTINGCHOOSE.ColumnEdit = this.dpc_enableCheckEdit;
+            this.GRIDVIEWPOLLSETTINGCHOOSE.FieldName = "GRIDVIEWPOLLSETTINGCHOOSE";
+            this.GRIDVIEWPOLLSETTINGCHOOSE.Name = "GRIDVIEWPOLLSETTINGCHOOSE";
+            this.GRIDVIEWPOLLSETTINGCHOOSE.Visible = true;
+            this.GRIDVIEWPOLLSETTINGCHOOSE.VisibleIndex = 0;
+            // 
+            // dpc_enableCheckEdit
+            // 
+            this.dpc_enableCheckEdit.AutoHeight = false;
+            this.dpc_enableCheckEdit.Name = "dpc_enableCheckEdit";
+            this.dpc_enableCheckEdit.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
+            this.dpc_enableCheckEdit.PictureChecked = ((System.Drawing.Image)(resources.GetObject("dpc_enableCheckEdit.PictureChecked")));
+            this.dpc_enableCheckEdit.PictureUnchecked = ((System.Drawing.Image)(resources.GetObject("dpc_enableCheckEdit.PictureUnchecked")));
+            this.dpc_enableCheckEdit.ValueChecked = "-1";
+            this.dpc_enableCheckEdit.ValueUnchecked = "0";
             // 
             // dpc_id
             // 
@@ -675,7 +697,7 @@ namespace UAS_DeviceMonitor
             this.dpc_decode.FieldName = "DPC_DECODE";
             this.dpc_decode.Name = "dpc_decode";
             this.dpc_decode.Visible = true;
-            this.dpc_decode.VisibleIndex = 0;
+            this.dpc_decode.VisibleIndex = 1;
             // 
             // dpc_plcode
             // 
@@ -683,7 +705,7 @@ namespace UAS_DeviceMonitor
             this.dpc_plcode.FieldName = "DPC_PLCODE";
             this.dpc_plcode.Name = "dpc_plcode";
             this.dpc_plcode.Visible = true;
-            this.dpc_plcode.VisibleIndex = 1;
+            this.dpc_plcode.VisibleIndex = 2;
             // 
             // dpc_plname
             // 
@@ -691,7 +713,7 @@ namespace UAS_DeviceMonitor
             this.dpc_plname.FieldName = "DPC_PLNAME";
             this.dpc_plname.Name = "dpc_plname";
             this.dpc_plname.Visible = true;
-            this.dpc_plname.VisibleIndex = 2;
+            this.dpc_plname.VisibleIndex = 3;
             // 
             // dpc_interval
             // 
@@ -699,7 +721,7 @@ namespace UAS_DeviceMonitor
             this.dpc_interval.FieldName = "DPC_INTERVAL";
             this.dpc_interval.Name = "dpc_interval";
             this.dpc_interval.Visible = true;
-            this.dpc_interval.VisibleIndex = 3;
+            this.dpc_interval.VisibleIndex = 4;
             // 
             // dpc_dccode
             // 
@@ -708,7 +730,7 @@ namespace UAS_DeviceMonitor
             this.dpc_dccode.FieldName = "DPC_DCCODE";
             this.dpc_dccode.Name = "dpc_dccode";
             this.dpc_dccode.Visible = true;
-            this.dpc_dccode.VisibleIndex = 4;
+            this.dpc_dccode.VisibleIndex = 5;
             // 
             // PollingSetItemLookUpEdit
             // 
@@ -726,7 +748,7 @@ namespace UAS_DeviceMonitor
             this.dpc_function.FieldName = "DPC_FUNCTION";
             this.dpc_function.Name = "dpc_function";
             this.dpc_function.Visible = true;
-            this.dpc_function.VisibleIndex = 5;
+            this.dpc_function.VisibleIndex = 6;
             // 
             // dpc_enable
             // 
@@ -735,17 +757,7 @@ namespace UAS_DeviceMonitor
             this.dpc_enable.FieldName = "DPC_ENABLE";
             this.dpc_enable.Name = "dpc_enable";
             this.dpc_enable.Visible = true;
-            this.dpc_enable.VisibleIndex = 6;
-            // 
-            // dpc_enableCheckEdit
-            // 
-            this.dpc_enableCheckEdit.AutoHeight = false;
-            this.dpc_enableCheckEdit.Name = "dpc_enableCheckEdit";
-            this.dpc_enableCheckEdit.NullStyle = DevExpress.XtraEditors.Controls.StyleIndeterminate.Unchecked;
-            this.dpc_enableCheckEdit.PictureChecked = ((System.Drawing.Image)(resources.GetObject("dpc_enableCheckEdit.PictureChecked")));
-            this.dpc_enableCheckEdit.PictureUnchecked = ((System.Drawing.Image)(resources.GetObject("dpc_enableCheckEdit.PictureUnchecked")));
-            this.dpc_enableCheckEdit.ValueChecked = "-1";
-            this.dpc_enableCheckEdit.ValueUnchecked = "0";
+            this.dpc_enable.VisibleIndex = 7;
             // 
             // dpc_status
             // 
@@ -753,7 +765,7 @@ namespace UAS_DeviceMonitor
             this.dpc_status.FieldName = "DPC_STATUS";
             this.dpc_status.Name = "dpc_status";
             this.dpc_status.Visible = true;
-            this.dpc_status.VisibleIndex = 7;
+            this.dpc_status.VisibleIndex = 8;
             // 
             // dpc_remark
             // 
@@ -761,7 +773,7 @@ namespace UAS_DeviceMonitor
             this.dpc_remark.FieldName = "DPC_REMARK";
             this.dpc_remark.Name = "dpc_remark";
             this.dpc_remark.Visible = true;
-            this.dpc_remark.VisibleIndex = 8;
+            this.dpc_remark.VisibleIndex = 9;
             // 
             // PagePolling
             // 
@@ -914,8 +926,8 @@ namespace UAS_DeviceMonitor
             this.PagePollingSetting.ResumeLayout(false);
             ((System.ComponentModel.ISupportInitialize)(this.GridPollingSetting)).EndInit();
             ((System.ComponentModel.ISupportInitialize)(this.GridViewPollSetting)).EndInit();
-            ((System.ComponentModel.ISupportInitialize)(this.PollingSetItemLookUpEdit)).EndInit();
             ((System.ComponentModel.ISupportInitialize)(this.dpc_enableCheckEdit)).EndInit();
+            ((System.ComponentModel.ISupportInitialize)(this.PollingSetItemLookUpEdit)).EndInit();
             this.PagePolling.ResumeLayout(false);
             ((System.ComponentModel.ISupportInitialize)(this.GridPolling)).EndInit();
             ((System.ComponentModel.ISupportInitialize)(this.GridViewPolling)).EndInit();
@@ -988,7 +1000,7 @@ namespace UAS_DeviceMonitor
         private GridViewWithSerialNum GridViewPollSetting;
         private LabelControl Brand_label;
         private ComboBoxEdit Brand;
-        private DevExpress.XtraGrid.Columns.GridColumn Choose;
+        private DevExpress.XtraGrid.Columns.GridColumn GRIDVIEWCOMMANDSETCHOOSE;
         private DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit CheckEditCommandSet;
         private CustomerControl.Button.ButtonDeleteRow ButtonDeleteCommand;
         private CustomerControl.Button.ButtonDeleteRow ButtonDeletePolling;
@@ -1001,5 +1013,6 @@ namespace UAS_DeviceMonitor
         private CustomerControl.Button.ButtonSaveGrid ButtonSaveCommandSet;
         private DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit PollingSetItemLookUpEdit;
         private System.Windows.Forms.Timer PollingTimer;
+        private DevExpress.XtraGrid.Columns.GridColumn GRIDVIEWPOLLSETTINGCHOOSE;
     }
 }

+ 55 - 15
UAS_DeviceMonitor/Main.cs

@@ -12,6 +12,7 @@ using DevExpress.XtraGrid.Views.Grid;
 using System.Timers;
 using System.Threading;
 using System.Threading.Tasks;
+using System.Collections.Generic;
 
 namespace UAS_DeviceMonitor
 {
@@ -42,7 +43,7 @@ namespace UAS_DeviceMonitor
             ButtonSavePolling.Grid = GridPolling;
             ButtonAddPolling.Grid = GridPolling;
             //轮询配置界面
-            GridPollingSetting.GetDataSQL = "SELECT DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG";
+            GridPollingSetting.GetDataSQL = "SELECT 0 GRIDVIEWPOLLSETTINGCHOOSE,DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK FROM DEVICEPOLLINGCONFIG";
             GridPollingSetting.ID = "DPC_ID";
             GridPollingSetting.TableName = "DEVICEPOLLINGCONFIG";
             GridPollingSetting.InsertSQL = "insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,:DPC_DECODE ,:DPC_PLCODE ,:DPC_PLNAME ,:DPC_INTERVAL ,:DPC_DCCODE ,:DPC_FUNCTION , :DPC_ENABLE,:DPC_STATUS ,:DPC_REMARK)";
@@ -51,7 +52,7 @@ namespace UAS_DeviceMonitor
             ButtonDeleteCommandSet.Grid = GridPollingSetting;
 
             //指令设置界面
-            GridCommandSetting.GetDataSQL = "select 0 CHOOSE,dc_id,dc_code,dc_name,dc_command from devicecommand".ToUpper();
+            GridCommandSetting.GetDataSQL = "select 0 GRIDVIEWCOMMANDSETCHOOSE,dc_id,dc_code,dc_name,dc_command from devicecommand".ToUpper();
             GridCommandSetting.TableName = "devicecommand";
             GridCommandSetting.ID = "dc_id";
             GridCommandSetting.InsertSQL = "insert into devicecommand(dc_id,dc_code,dc_name,dc_command) values(devicecommand_seq.nextval,:dc_code,:dc_name,:dc_command)";
@@ -169,40 +170,79 @@ namespace UAS_DeviceMonitor
 
         #region PagePollingSetting业务代码
 
-        bool PollingSign = false;
-
+        PollingTask pt = new PollingTask();
         private void ButtonStartPolling_Click(object sender, EventArgs e)
         {
-            PollingTask pt = new PollingTask();
             GridView grid = GridViewPollSetting;
             for (int i = 0; i < GridPollingSetting.RowCount; i++)
             {
                 Polling pl = new Polling();
+                pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_ID").ToString());
                 pl.DeviceCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DECODE").ToString();
                 pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString());
                 pl.Enable = GridViewPollSetting.GetRowCellValue(i, "DPC_ENABLE").ToString() != "0";
+                pl.Code = GridViewPollSetting.GetRowCellValue(i, "DPC_PLCODE").ToString();
                 if (pl.Enable)
                     pt.AddTask(RunTask, pl);
             }
-            PollingSign = true;
-            pt.StartAllTask();
         }
 
+        Dictionary<int, PollingTimer> Ptime = new Dictionary<int, PollingTimer>();
+
         private void RunTask(object i)
         {
-            while (PollingSign)
+            PollingTimer timer = new PollingTimer();
+            Polling pl = (Polling)i;
+            timer.Polling = i;
+            timer.Interval = pl.Interval * 1000;
+            timer.Elapsed += Timer_Tick;
+            Ptime.Add(pl.Id, timer);
+            timer.Start();
+        }
+
+        private void Timer_Tick(object sender, EventArgs e)
+        {
+            PollingTimer timer = (PollingTimer)sender;
+            Polling pl = (Polling)timer.Polling;
+            string Decode = pl.DeviceCode;
+            Console.WriteLine(Decode);
+        }
+
+        private void ButtonPausePolling_Click(object sender, EventArgs e)
+        {
+            foreach (var item in Ptime)
             {
-                Polling pl = (Polling)i;
-                int Interval = pl.Interval;
-                string Decode = pl.DeviceCode;
-                Thread.Sleep(Interval * 1000);
-                SystemInf.dh.ExecuteSql("insert into DEVICEPOLLINGCONFIG(DPC_ID,DPC_DECODE ,DPC_PLCODE ,DPC_PLNAME ,DPC_INTERVAL ,DPC_DCCODE ,DPC_FUNCTION , DPC_ENABLE,DPC_STATUS ,DPC_REMARK) values(DEVICEPOLLINGCONFIG_seq.nextval,'" + Decode + "','','' ,'' ,'' ,'' , '','' ,'')", "insert");
+                item.Value.Stop();
             }
         }
 
-        private void ButtonPausePolling_Click(object sender, EventArgs e)
+        private void GridViewPollSetting_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
         {
-            PollingSign = false;
+            if (e.Column.Name.ToUpper() == "DPC_ENABLE")
+            {
+                GridViewPollSetting.SetRowCellValue(e.RowHandle, e.Column, e.Value);
+                Console.WriteLine(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString());
+                if (GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() == "0")
+                {
+                    int id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
+                    if (Ptime.ContainsKey(id))
+                    {
+                        Ptime[id].Stop();
+                        Ptime.Remove(id);
+                    }
+                }
+                else
+                {
+                    Polling pl = new Polling();
+                    pl.Id = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ID").ToString());
+                    pl.DeviceCode = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_DECODE").ToString();
+                    pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_INTERVAL").ToString());
+                    pl.Enable = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_ENABLE").ToString() != "0";
+                    pl.Code = GridViewPollSetting.GetRowCellValue(e.RowHandle, "DPC_PLCODE").ToString();
+                    if (pl.Enable)
+                        pt.AddTask(RunTask, pl);
+                }
+            }
         }
 
         /// <summary>

+ 32 - 21
UAS_DeviceMonitor/PublicMethod/PollingTask.cs

@@ -1,16 +1,17 @@
 using System;
-using System.Collections;
 using System.Collections.Generic;
-using System.Runtime.Serialization;
 using System.Threading;
 using System.Threading.Tasks;
+using UAS_DeviceMonitor.Entity;
 
 namespace UAS_DeviceMonitor.PublicMethod
 {
     class PollingTask
     {
-
-        List<Task> list = new List<Task>();
+        /// <summary>
+        /// 内部调用的任务,添加了取消标识符号
+        /// </summary>
+        Dictionary<string, Task> InTask = new Dictionary<string, Task>();
         /// <summary>
         /// 获取任务
         /// </summary>
@@ -26,24 +27,26 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// </summary>
         /// <param name="TCode"></param>
         /// <returns></returns>
-        public void AddTask(Action action)
-        {
-            Task newTask = new Task(action);
-            list.Add(newTask);
-        }
-
+        //public void AddTask(Action action, string Code)
+        //{
+        //    Task newTask = Task.Factory.StartNew(() => action);
+        //    InTask.Add(Code, newTask);
+        //}
+       
         /// <summary>
         /// 添加任务,默认添加后自动执行,返回创建的TASK的ID
         /// </summary>
         /// <param name="TCode"></param>
         /// <returns></returns>
-        public void AddTask(Action<object> action,object obj)
+        public void AddTask(Action<object> action, object obj)
         {
-            Task newTask = new Task(action, obj);
-            list.Add(newTask);
+            Polling pl = (Polling)obj;
+            if (!InTask.ContainsKey(pl.Code))
+            {
+                InTask.Add(pl.Code, Task.Factory.StartNew(action, obj));
+            }
         }
 
-        /// <summary>
         /// 添加一系列任务
         /// </summary>
         /// <param name="action"></param>
@@ -66,8 +69,19 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// 移除任务
         /// </summary>
         /// <returns></returns>
-        public bool RemoveTask()
+        public bool RemoveTask(string Code)
         {
+            InTask.Remove(Code);
+            return true;
+        }
+
+        /// <summary>
+        /// 移除所有的任务
+        /// </summary>
+        /// <returns></returns>
+        public bool RemoveAllTask()
+        {
+            InTask.Clear();
             return true;
         }
 
@@ -86,9 +100,9 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// </summary>
         public void StartAllTask()
         {
-            for (int i = 0; i < list.Count; i++)
+            foreach (var item in InTask)
             {
-                list[i].Start();
+                item.Value.Start();
             }
         }
 
@@ -105,10 +119,7 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// </summary>
         public void StopAllTask()
         {
-            for (int i = 0; i < list.Count; i++)
-            {
-                var tokenSource = new CancellationTokenSource();
-            }
+
         }
 
         /// <summary>

+ 25 - 0
UAS_DeviceMonitor/PublicMethod/PollingTimer.cs

@@ -0,0 +1,25 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace UAS_DeviceMonitor.PublicMethod
+{
+    class PollingTimer : System.Timers.Timer
+    {
+        private object polling;
+
+        public object Polling
+        {
+            get
+            {
+                return polling;
+            }
+
+            set
+            {
+                polling = value;
+            }
+        }
+    }
+}

+ 3 - 0
UAS_DeviceMonitor/UAS_DeviceMonitor.csproj

@@ -118,6 +118,9 @@
     <Compile Include="PublicMethod\LogManager.cs" />
     <Compile Include="PublicMethod\ModeBusTCPServer.cs" />
     <Compile Include="PublicMethod\PollingTask.cs" />
+    <Compile Include="PublicMethod\PollingTimer.cs">
+      <SubType>Component</SubType>
+    </Compile>
     <EmbeddedResource Include="CustomerControl\AutoDataGridControl\AutoDataGridControl.resx">
       <DependentUpon>AutoDataGridControl.cs</DependentUpon>
     </EmbeddedResource>