天天看点

c#编程之多线程操作之基本操作备忘

如题,本博客使用ThreadStart来实现多线程。

1、声明一个多线程:

Thread m_subThread = new Thread(new ThreadStart(函数名));

代码示例:

void ThreadFunc()
{
    //此处是线程要干的事情
}
Thread m_subThread = new Thread(new ThreadStart(ThreadFunc));
m_subThread.IsBackground = true;
           

2、开始线程:

使用Start接口

代码示例:

m_subThread.Start(); //开始 
           

3、终止线程:

使用Abort接口

代码示例:

m_subThread.Abort();
           

4、暂定线程:

使用Suspend接口

代码示例:

if (m_subThread.IsAlive)  m_subThreadAutoTest.Suspend();
           

5、开始暂定的线程:

使用Resume接口

代码示例:

if (m_subThread.IsAlive)  m_subThreadAutoTest.Resume();
           

6、判断线程是否暂定状态?

代码示例:

if (0 != (m_subThread.ThreadState & ThreadState.Suspended))
{
    //线程被暂停了
}
else
{
    //线程没有暂停!
}
           
c#编程之多线程操作之基本操作备忘

---- The End.