Sfoglia il codice sorgente

添加多任务并行

章政 7 anni fa
parent
commit
9f4546f11f

+ 85 - 0
UAS_DeviceMonitor/Entity/Polling.cs

@@ -0,0 +1,85 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace UAS_DeviceMonitor.Entity
+{
+    class Polling
+    {
+        private string deviceCode;
+
+        private string iP;
+
+        private string port;
+
+        private int interval;
+
+        private bool enable;
+
+        public string DeviceCode
+        {
+            get
+            {
+                return deviceCode;
+            }
+
+            set
+            {
+                deviceCode = value;
+            }
+        }
+
+        public string IP
+        {
+            get
+            {
+                return iP;
+            }
+
+            set
+            {
+                iP = value;
+            }
+        }
+
+        public string Port
+        {
+            get
+            {
+                return port;
+            }
+
+            set
+            {
+                port = value;
+            }
+        }
+
+        public int Interval
+        {
+            get
+            {
+                return interval;
+            }
+
+            set
+            {
+                interval = value;
+            }
+        }
+
+        public bool Enable
+        {
+            get
+            {
+                return enable;
+            }
+
+            set
+            {
+                enable = value;
+            }
+        }
+    }
+}

+ 12 - 18
UAS_DeviceMonitor/Main.cs

@@ -10,6 +10,7 @@ using DevExpress.XtraEditors;
 using DevExpress.XtraGrid;
 using DevExpress.XtraGrid.Views.Grid;
 using System.Threading;
+using System.Threading.Tasks;
 
 namespace UAS_DeviceMonitor
 {
@@ -169,27 +170,22 @@ namespace UAS_DeviceMonitor
         private void ButtonStartPolling_Click(object sender, EventArgs e)
         {
             PollingTask pt = new PollingTask();
-            pt.AddTask(RunTask);
-            pt.AddTask(RunTask1);
-            pt.AddTask(RunTask2);
+            GridView grid = GridViewPollSetting;
+            for (int i = 0; i < GridPollingSetting.RowCount; i++)
+            {
+                Polling pl = new Polling();
+                pl.DeviceCode = GridViewPollSetting.GetRowCellValue(i, "DPC_DECODE").ToString();
+                pl.Interval = int.Parse(GridViewPollSetting.GetRowCellValue(i, "DPC_INTERVAL").ToString());
+                pl.Enable = (bool)GridViewPollSetting.GetRowCellValue(i, "DPC_ENABLE");
+                pt.AddTask(RunTask, pl);
+            }
             pt.StartAllTask();
         }
 
         private void RunTask(object i)
         {
-            Thread.Sleep(1 * 1000);
-            Console.WriteLine(1);
-        }
-
-        private void RunTask1(object i)
-        {
-            Thread.Sleep(2 * 1000);
-            Console.WriteLine(3);
-        }
-        private void RunTask2(object i)
-        {
-            Thread.Sleep(3 * 1000);
-            Console.WriteLine(3);
+            Polling pl = (Polling)i;
+            //Interval
         }
 
         private void ButtonPausePolling_Click(object sender, EventArgs e)
@@ -197,8 +193,6 @@ namespace UAS_DeviceMonitor
 
         }
 
-        
-
         /// <summary>
         /// 设置GridView下拉框的值
         /// </summary>

+ 17 - 15
UAS_DeviceMonitor/PublicMethod/PollingTask.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
+using System.Runtime.Serialization;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -27,8 +28,8 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// <returns></returns>
         public void AddTask(Action action)
         {
-            Task task1 = new Task(action);
-            list.Add(task1);
+            Task newTask = new Task(action);
+            list.Add(newTask);
         }
 
         /// <summary>
@@ -36,13 +37,12 @@ namespace UAS_DeviceMonitor.PublicMethod
         /// </summary>
         /// <param name="TCode"></param>
         /// <returns></returns>
-        public void AddTask(Action<object> action)
+        public void AddTask(Action<object> action,object obj)
         {
-            Task task1 = new Task(action, new object());
-            list.Add(task1);
+            Task newTask = new Task(action, obj);
+            list.Add(newTask);
         }
 
-
         /// <summary>
         /// 添加一系列任务
         /// </summary>
@@ -89,24 +89,26 @@ namespace UAS_DeviceMonitor.PublicMethod
             for (int i = 0; i < list.Count; i++)
             {
                 list[i].Start();
-                list[i].Wait();
             }
         }
 
-        /// <summary>
-        /// 暂停所有任务
-        /// </summary>
-        public void PauseAllTask()
-        {
-
-        }
+        ///// <summary>
+        ///// 暂停所有任务
+        ///// </summary>
+        //public void PauseAllTask()
+        //{
+        //
+        //}
 
         /// <summary>
         /// 停止所有任务
         /// </summary>
         public void StopAllTask()
         {
-
+            for (int i = 0; i < list.Count; i++)
+            {
+                var tokenSource = new CancellationTokenSource();
+            }
         }
 
         /// <summary>

+ 1 - 0
UAS_DeviceMonitor/UAS_DeviceMonitor.csproj

@@ -104,6 +104,7 @@
     <Compile Include="Device\Command\FormNewCommand.Designer.cs">
       <DependentUpon>FormNewCommand.cs</DependentUpon>
     </Compile>
+    <Compile Include="Entity\Polling.cs" />
     <Compile Include="Entity\SystemInf.cs" />
     <Compile Include="Main.cs">
       <SubType>Form</SubType>