zoukankan      html  css  js  c++  java
  • C#基础之通过任务开启线程

    通过任务开启的线程方法不能有参数不能有返回值

    通过任务开启线程有两种方法

    第一种方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 任务开启线程
    {
        class Program
        {
            static void Main(string[] args)
            {
                Task task = new Task(test);//将线程放入任务task中
                task.Start();//开启任务,就会执行线程
                Task task1 = new Task(test1);
                task1.Start();
                Console.ReadKey();
            }
            static void test()//线程1
            {
                Console.WriteLine("a");
    
            }
            static void test1()//线程2
            {
                Console.WriteLine("b");
            }
        }
    }

    第二种方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 任务开启线程
    {
        class Program
        {
            static void Main(string[] args)
            {
                TaskFactory tf = new TaskFactory();
                Task task = tf.StartNew(test);//将方法交给任务后,开启任务,执行test
               
                Console.ReadKey();
            }
            static void test()//线程1
            {
                Console.WriteLine("a");
    
            }
          
        }
    }

    任务的知识

    连续任务:一个任务必须再另一个任务结束时才能执行

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 任务开启线程
    {
        class Program
        {
            static void Main(string[] args)
            {
                Task task1 = new Task(test1);
                Task task= task1.ContinueWith(test);//任务task必须在task1任务执行后在执行,且task此时已近启动该线程了
    
                task1.Start();
               
                Console.ReadKey();
            }
            static void test(Task t)//线程1
            {
                Console.WriteLine("a");
    
            }
            static void test1()//线程1
            {
                Console.WriteLine("b");
    
            }
    
        }
    }

    任务层次结构

    在一个任务中启动了一个新任务,如果父任务执行完,子任务未执行完,则父任务的状态是WaitForChildrenToComplete

                                                          如果父任务执行完了,子任务也执行完了,则父任务的状态是RunToComplete

  • 相关阅读:
    C#图片处理之:亮度和对比度的校正
    C#图片处理之:旋转图片90度的整数倍 .
    C#图片处理之:色彩调整 .
    C# 图片处理之:彩色图片转为黑白图 .
    C#图片处理之: 锐化
    C#图片处理之:图片缩放和剪裁 .
    C# 图片处理之:旋转图片任意角度 .
    C#图片处理之:Gamma校正 .
    C#图片处理之: 另存为压缩质量可自己控制的JPEG .
    c#图片处理之:在图片上打上文字
  • 原文地址:https://www.cnblogs.com/zhangyang4674/p/11414701.html
Copyright © 2011-2022 走看看