开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 3116|回复: 0

[讨论] C# 线程--第一单线程基础

[复制链接]

0

精华

1

贡献

10

赞扬

帖子
9
软币
151
在线时间
11 小时
注册时间
2015-3-3
发表于 2015-3-13 14:28:01 | 显示全部楼层 |阅读模式
C# 线程--第一单线程基础
概念

什么是进程?
当一个程序被打开运行时,它就是一个进程。在进程中包括线程,进程可以由一个或多个线程组成。
什么是线程?
线程是程序执行流的最小单元。一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成。
什么是多线程?
多线程是指程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。

个人总结
  在C#中我们开启一个应用程序就是打开了一个进程,这个进程中包括一个主线程。我们可以在此基础上在增加自己写的单个或多个线程,来执行我们想要完成的任务。
  在Thread线程类中我们常用到的一样方法:
    • Start():启动线程;
    • Sleep(int):静态方法,暂停当前线程指定的毫秒数;
    • Abort():通常使用该方法来终止一个线程;
    • Suspend():该方法并不终止未完成的线程,它仅仅挂起线程,以后还可恢复;
    • Resume():恢复被Suspend()方法挂起的线程的执行。

  在C#中开启一个线程,来执行我们所写的方法。主要是通过ThreadStart代理委托来实现。下面写一些简单代码示例。

代码示例
单线程:
[size=1em]
[backcolor=rgb(244, 244, 244) !important]1

2

[backcolor=rgb(244, 244, 244) !important]3

4

[backcolor=rgb(244, 244, 244) !important]5

6

[backcolor=rgb(244, 244, 244) !important]7

8

[backcolor=rgb(244, 244, 244) !important]9

10

[backcolor=rgb(244, 244, 244) !important]11

12

[backcolor=rgb(244, 244, 244) !important]13

14

[backcolor=rgb(244, 244, 244) !important]15

16

[backcolor=rgb(244, 244, 244) !important]17

18

[backcolor=rgb(244, 244, 244) !important]19

20

[backcolor=rgb(244, 244, 244) !important]21

22

[backcolor=rgb(244, 244, 244) !important]23

24

[backcolor=rgb(244, 244, 244) !important]25

26

[backcolor=rgb(244, 244, 244) !important]27

28

[backcolor=rgb(244, 244, 244) !important]29

30

[backcolor=rgb(244, 244, 244) !important]31

32

[backcolor=rgb(244, 244, 244) !important]33

34

[backcolor=rgb(244, 244, 244) !important]35

36

[backcolor=rgb(244, 244, 244) !important]37

38

[backcolor=rgb(244, 244, 244) !important]39

40

[backcolor=rgb(244, 244, 244) !important]41

42

[backcolor=rgb(244, 244, 244) !important]43

44

[backcolor=rgb(244, 244, 244) !important]45

46

[backcolor=rgb(244, 244, 244) !important]47

48

[backcolor=rgb(244, 244, 244) !important]49

[backcolor=rgb(244, 244, 244) !important]using System;
using System.Threading;

namespace StudyThread
[backcolor=rgb(244, 244, 244) !important]{
    class Program
[backcolor=rgb(244, 244, 244) !important]    {
        static void Main(string[] args)
[backcolor=rgb(244, 244, 244) !important]        {
            OneThread();
[backcolor=rgb(244, 244, 244) !important]        }

[backcolor=rgb(244, 244, 244) !important]        static void OneThread()
        {
[backcolor=rgb(244, 244, 244) !important]            Thread threadA = new Thread(ThreadMethod);
            threadA.IsBackground = true;   //设置当前子线程为后台线程,为后台线程意味着,主线程关闭后,其他子线程都同时关闭
[backcolor=rgb(244, 244, 244) !important]            threadA.Start();

[backcolor=rgb(244, 244, 244) !important]            object ThreadParameter = "P";
            Thread threadB = new Thread(new ParameterizedThreadStart(ThreadMethodParameter));
[backcolor=rgb(244, 244, 244) !important]            threadB.IsBackground = true;
            threadB.Start(ThreadParameter);

            Console.WriteLine("Main Write:M");
[backcolor=rgb(244, 244, 244) !important]            Console.ReadLine();
        }

        static void ThreadMethod()
[backcolor=rgb(244, 244, 244) !important]        {
            for (int i = 0; i < 10; i++)
[backcolor=rgb(244, 244, 244) !important]            {
                Console.WriteLine("A");
[backcolor=rgb(244, 244, 244) !important]            }
        }

        /// <summary>
[backcolor=rgb(244, 244, 244) !important]        /// 带参数线程
        /// </summary>
[backcolor=rgb(244, 244, 244) !important]        /// <param name="Parameter">只能定义一个object参数,因为委托ParameterizedThreadStart为单参数object类型</param>
        static void ThreadMethodParameter(object Parameter)
[backcolor=rgb(244, 244, 244) !important]        {
            for (int j = 0; j < 10; j++)
[backcolor=rgb(244, 244, 244) !important]            {
                Console.WriteLine("B"+Parameter);
[backcolor=rgb(244, 244, 244) !important]            }
        }

    }
[backcolor=rgb(244, 244, 244) !important]}



在代理里分别开启两个线程。
第一个线程ThreadA没带参数,线程运行时去执行ThreadMethod()方法.
第二个线程ThreadB带上一个Object参数,通过ParameterizedThreadStart委托去执行ThreadMethodParameter(object Parameter)方法.此线程在调用Start()时传入所需参数。
(写代码时我在想为什么带参数线程只能传一个object参数?看了代码才知道ParameterizedThreadStart只有一个参数没有重载)。

执行结果:
Main Write:M 为主线程打印。
A:为线程ThreadA打印。
BP:为带参数线程ThreadB打印。



评分

参与人数 1赞扬 +1 收起 理由
1272323522 + 1 很给力

查看全部评分

回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-5-3 10:46

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表