Browse Source

测试项目

章政 6 years ago
parent
commit
12d8d45f4a

+ 1 - 1
TestProject/Program.cs

@@ -25,7 +25,7 @@ namespace TestProject
                 //如果是管理员的身份
                 if (principal.IsInRole(WindowsBuiltInRole.Administrator))
                 {
-                    Application.Run(new Form2());
+                    Application.Run(new TaskTEST());
                 }
                 else
                 {

+ 47 - 0
TestProject/TaskTEST.Designer.cs

@@ -0,0 +1,47 @@
+namespace TestProject
+{
+    partial class TaskTEST
+    {
+        /// <summary>
+        /// Required designer variable.
+        /// </summary>
+        private System.ComponentModel.IContainer components = null;
+
+        /// <summary>
+        /// Clean up any resources being used.
+        /// </summary>
+        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing && (components != null))
+            {
+                components.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        #region Windows Form Designer generated code
+
+        /// <summary>
+        /// Required method for Designer support - do not modify
+        /// the contents of this method with the code editor.
+        /// </summary>
+        private void InitializeComponent()
+        {
+            this.SuspendLayout();
+            // 
+            // Form3
+            // 
+            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
+            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
+            this.ClientSize = new System.Drawing.Size(692, 376);
+            this.Name = "Form3";
+            this.Text = "Form3";
+            this.Load += new System.EventHandler(this.Form3_Load);
+            this.ResumeLayout(false);
+
+        }
+
+        #endregion
+    }
+}

+ 207 - 0
TestProject/TaskTEST.cs

@@ -0,0 +1,207 @@
+using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Diagnostics;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace TestProject
+{
+    public partial class TaskTEST : Form
+    {
+        public TaskTEST()
+        {
+            InitializeComponent();
+        }
+
+        private Stopwatch stopWatch = new Stopwatch();
+
+        public void ParallelInvokeMethod1()
+        {
+            Parallel.For(0, 100, item =>
+            {
+                DoWork(item);
+            });
+        }
+
+        private void DoWork(int item)
+        {
+
+        }
+
+        public void ParallelBreak()
+        {
+            ConcurrentBag<int> bag = new ConcurrentBag<int>();
+            stopWatch.Start();
+            Parallel.For(0, 5000, (i, state) =>
+            {
+                if (bag.Count == 3000)
+                {
+                    state.Stop();
+                    return;
+                }
+                bag.Add(i);
+            });
+            stopWatch.Stop();
+            Console.WriteLine("Bag count is " + bag.Count + ", " + stopWatch.ElapsedMilliseconds);
+        }
+
+        public void Run1()
+        {
+            Thread.Sleep(2000);
+            Console.WriteLine("Task 1 is cost 2 sec");
+            throw new Exception("Exception in task 1");
+        }
+        public void Run2()
+        {
+            Thread.Sleep(3000);
+            Console.WriteLine("Task 2 is cost 3 sec");
+            throw new Exception("Exception in task 2");
+        }
+
+        public void ParallelInvokeMethod()
+        {
+            stopWatch.Start();
+            try
+            {
+                Parallel.Invoke(Run1, Run2);
+            }
+            catch (AggregateException aex)
+            {
+                foreach (var ex in aex.InnerExceptions)
+                {
+                    Console.WriteLine(ex.Message);
+                }
+            }
+            stopWatch.Stop();
+            Console.WriteLine("Parallel run " + stopWatch.ElapsedMilliseconds + " ms.");
+
+            stopWatch.Reset();
+            stopWatch.Start();
+            try
+            {
+                Run1();
+                Run2();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+            }
+            stopWatch.Stop();
+            Console.WriteLine("Normal run " + stopWatch.ElapsedMilliseconds + " ms.");
+        }
+
+
+        private void Form3_Load(object sender, EventArgs e)
+        {
+            //var task1 = new Task(() =>
+            //{
+            //    Console.WriteLine("Begin");
+            //    Thread.Sleep(2000);
+            //    Console.WriteLine("Finish");
+            //});
+            //Console.WriteLine("Before start:" + task1.Status);
+            //task1.Start();
+            //Console.WriteLine("After start:" + task1.Status);
+            //task1.Wait();
+            //Console.WriteLine("After Finish:" + task1.Status);
+            //Console.Read();
+
+            //var task1 = new System.Threading.Tasks.Task(() =>
+            //{
+            //    Console.WriteLine("Task 1 Begin");
+            //    Thread.Sleep(2000);
+            //    Console.WriteLine("Task 1 Finish");
+            //});
+            //var task2 = new System.Threading.Tasks.Task(() =>
+            //{
+            //    Console.WriteLine("Task 2 Begin");
+            //    Thread.Sleep(3000);
+            //    Console.WriteLine("Task 2 Finish");
+            //});
+
+            //task1.Start();
+            //task2.Start();
+            //var result = task1.ContinueWith(task =>
+            //{
+            //    Console.WriteLine("task1 finished!");
+            //    return "This is task result!";
+            //});
+
+            //Console.WriteLine(result.Result.ToString());
+
+            //TaskFactory tf = new TaskFactory();
+            //var SendFeedBackTask = tf.StartNew(() => { Console.WriteLine("Get some Data!"); })
+            //                .ContinueWith(s => { return false; })
+            //                .ContinueWith<string>(r =>
+            //                {
+            //                    if (r.Result)
+            //                    {
+            //                        return "Finished";
+            //                    }
+            //                    else
+            //                    {
+            //                        return "Error";
+            //                    }
+            //                });
+            //Console.WriteLine(SendFeedBackTask.Result);
+            //线程取消
+            var tokenSource = new CancellationTokenSource();
+            var token = tokenSource.Token;
+            var task = new TaskFactory().StartNew(() =>
+            {
+                for (var i = 0; i < 1000; i++)
+                {
+                    Thread.Sleep(1000);
+                    if (token.IsCancellationRequested)
+                    {
+                        Console.WriteLine("Abort mission success!");
+                        return;
+                    }
+                    Console.WriteLine("TASK");
+                }
+            }, token);
+            token.Register(() =>
+            {
+                Console.WriteLine("Canceled");
+            });
+            Console.WriteLine("Press enter to cancel task...");
+            Thread.Sleep(10000);
+            tokenSource.Cancel();
+            //关联子进程
+            //var pTask = new TaskFactory().StartNew(() =>
+            //{
+            //    var cTask = new TaskFactory().StartNew(() =>
+            //    {
+            //        Thread.Sleep(2000);
+            //        Console.WriteLine("Childen task finished!");
+            //    });
+            //    Console.WriteLine("Parent task finished!");
+            //});
+            //pTask.Wait();
+            //Console.WriteLine("Flag");
+            //Console.Read();
+
+            //加入关联关系,父Task会等子Task完成后才继续执行
+            //var pTask = new TaskFactory().StartNew(() =>
+            //{
+            //    var cTask = new TaskFactory().StartNew(() =>
+            //    {
+            //        Console.WriteLine("Childen task finished!");
+            //    }, TaskCreationOptions.AttachedToParent);
+            //    Thread.Sleep(2000);
+            //    Console.WriteLine("Parent task finished!");
+            //});
+            //pTask.Wait();
+            //System.Threading.Tasks.Task.WaitAll();
+
+            //Console.WriteLine("Flag");
+        }
+    }
+}

+ 120 - 0
TestProject/TaskTEST.resx

@@ -0,0 +1,120 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+  <!-- 
+    Microsoft ResX Schema 
+    
+    Version 2.0
+    
+    The primary goals of this format is to allow a simple XML format 
+    that is mostly human readable. The generation and parsing of the 
+    various data types are done through the TypeConverter classes 
+    associated with the data types.
+    
+    Example:
+    
+    ... ado.net/XML headers & schema ...
+    <resheader name="resmimetype">text/microsoft-resx</resheader>
+    <resheader name="version">2.0</resheader>
+    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+        <value>[base64 mime encoded serialized .NET Framework object]</value>
+    </data>
+    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+        <comment>This is a comment</comment>
+    </data>
+                
+    There are any number of "resheader" rows that contain simple 
+    name/value pairs.
+    
+    Each data row contains a name, and value. The row also contains a 
+    type or mimetype. Type corresponds to a .NET class that support 
+    text/value conversion through the TypeConverter architecture. 
+    Classes that don't support this are serialized and stored with the 
+    mimetype set.
+    
+    The mimetype is used for serialized objects, and tells the 
+    ResXResourceReader how to depersist the object. This is currently not 
+    extensible. For a given mimetype the value must be set accordingly:
+    
+    Note - application/x-microsoft.net.object.binary.base64 is the format 
+    that the ResXResourceWriter will generate, however the reader can 
+    read any of the formats listed below.
+    
+    mimetype: application/x-microsoft.net.object.binary.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+            : and then encoded with base64 encoding.
+    
+    mimetype: application/x-microsoft.net.object.soap.base64
+    value   : The object must be serialized with 
+            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+            : and then encoded with base64 encoding.
+
+    mimetype: application/x-microsoft.net.object.bytearray.base64
+    value   : The object must be serialized into a byte array 
+            : using a System.ComponentModel.TypeConverter
+            : and then encoded with base64 encoding.
+    -->
+  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+    <xsd:element name="root" msdata:IsDataSet="true">
+      <xsd:complexType>
+        <xsd:choice maxOccurs="unbounded">
+          <xsd:element name="metadata">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" />
+              </xsd:sequence>
+              <xsd:attribute name="name" use="required" type="xsd:string" />
+              <xsd:attribute name="type" type="xsd:string" />
+              <xsd:attribute name="mimetype" type="xsd:string" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="assembly">
+            <xsd:complexType>
+              <xsd:attribute name="alias" type="xsd:string" />
+              <xsd:attribute name="name" type="xsd:string" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="data">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+              <xsd:attribute ref="xml:space" />
+            </xsd:complexType>
+          </xsd:element>
+          <xsd:element name="resheader">
+            <xsd:complexType>
+              <xsd:sequence>
+                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+              </xsd:sequence>
+              <xsd:attribute name="name" type="xsd:string" use="required" />
+            </xsd:complexType>
+          </xsd:element>
+        </xsd:choice>
+      </xsd:complexType>
+    </xsd:element>
+  </xsd:schema>
+  <resheader name="resmimetype">
+    <value>text/microsoft-resx</value>
+  </resheader>
+  <resheader name="version">
+    <value>2.0</value>
+  </resheader>
+  <resheader name="reader">
+    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+  <resheader name="writer">
+    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+  </resheader>
+</root>

+ 13 - 0
TestProject/TestProject.csproj

@@ -73,6 +73,10 @@
     <NoWin32Manifest>true</NoWin32Manifest>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="BenQGuru.eMES.DLLService, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\UAS_MesInterface(4.0)\bin\Debug\BenQGuru.eMES.DLLService.dll</HintPath>
+    </Reference>
     <Reference Include="CSkin">
       <HintPath>tool\CSkin.dll</HintPath>
     </Reference>
@@ -102,6 +106,12 @@
     <Compile Include="Form2.cs">
       <SubType>Form</SubType>
     </Compile>
+    <Compile Include="TaskTEST.cs">
+      <SubType>Form</SubType>
+    </Compile>
+    <Compile Include="TaskTEST.Designer.cs">
+      <DependentUpon>TaskTEST.cs</DependentUpon>
+    </Compile>
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="QQButton.cs">
@@ -122,6 +132,9 @@
     <EmbeddedResource Include="Form2.resx">
       <DependentUpon>Form2.cs</DependentUpon>
     </EmbeddedResource>
+    <EmbeddedResource Include="TaskTEST.resx">
+      <DependentUpon>TaskTEST.cs</DependentUpon>
+    </EmbeddedResource>
     <EmbeddedResource Include="Properties\Resources.resx">
       <Generator>ResXFileCodeGenerator</Generator>
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>