C#多线程编程实战1.4终止线程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
//终止线程
namespace Recipe4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("starting program...");
Thread t = new Thread(PrintNumbersWithDelay);
t.Start();
Thread.Sleep(TimeSpan.FromSeconds(6));
t.Abort();//给线程注入了ThreadAbortException方法,导致线程被终结。该异常可能在任何时刻发生并可能彻底摧毁应用程序。而且,该技术不一定总是能够终止线程。不推荐此方法终止线程
Console.WriteLine("a thread has been aborted");
Thread t1 = new Thread(PrintNumbers);
t1.Start();
PrintNumbers();
Console.ReadKey();
}
static void PrintNumbersWithDelay()
{
Console.WriteLine("starting...");
for (int i = 1; i < 10; i++)
{
Thread.Sleep(TimeSpan.FromSeconds(2));
Console.WriteLine(i);
}
}
static void PrintNumbers()
{
Console.WriteLine("Starting");
for (int i = 1; i < 10; i++)
{
Console.WriteLine(i);
}
}
}
}
C#多线程编程实战1.4终止线程的更多相关文章
- C#多线程编程实战1.1创建线程
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C#多线程编程实战1.7前台线程和后台线程
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C#多线程编程实战1.5检测线程状态
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C#多线程编程实战1.3等待线程
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C#多线程编程实战1.2暂停线程(休眠)
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- C#多线程编程实战(二)
1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...
- Java多线程编程实战指南(核心篇)读书笔记(三)
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76686044冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...
- 《Java多线程编程实战指南(核心篇)》阅读笔记
<Java多线程编程实战指南(核心篇)>阅读笔记 */--> <Java多线程编程实战指南(核心篇)>阅读笔记 Table of Contents 1. 线程概念 1.1 ...
- C#多线程编程系列(二)- 线程基础
目录 C#多线程编程系列(二)- 线程基础 1.1 简介 1.2 创建线程 1.3 暂停线程 1.4 线程等待 1.5 终止线程 1.6 检测线程状态 1.7 线程优先级 1.8 前台线程和后台线程 ...
随机推荐
- 【转】JMeter试用手记
JMeter是一款性能测试工具.个人认为与其说他是一个工具,不如说他是一个框架.因为JMeter的支持范围非常广,目前常见的需要进行性能测试的应用几乎都能应用(如:files, Servlets, P ...
- EasyUI TreeJson
1. TreeJson str = GetTreeJsonByTable(dt, "); StringBuilder treeResult = new StringBuilder(); St ...
- 1078 Hashing
题意:给出表长和待插入的元素,求每个元素的插入位置,散列函数为H(key)=key%TSize,解决冲突利用平方探测法(只考虑正向偏移). 思路:同1145 Hashing - Average Sea ...
- AJAX如何获取从前台传递过来的数据然后在通过servle传递给后台
1 用 request.getParameter接收值 <% String id1=request.getParameter("id"); out.print(id1); % ...
- MIS系统部署方案
- python使用multiprocessing进行多进程编程(1)
multiprocessing模块实现了对多进程编程的封装,让我们可以非常方便的使用多进程进行编程.它的使用方法非常类似threading模块. 1.创建一个进程 import multiproces ...
- 爬虫模块之Request
requests Requests唯一一个非转基因的Python HTTP库,人类就可以安全享用. Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是 ...
- GBK/ UTF-8/ UNICODE(字符编码)
在python2中:如果执行程序,在编译器中,因为默认的编码是ASCII码(英文),所以如果输入中文就会出现乱码,因此为了避免这种乱码的情况发生,在输入中文字符串之后,必须进行手动转码,将GBK/ U ...
- 设置窗口的z-order总是在最底部
想让窗口置顶,很简单,只需要在SetWindowPos中指定 HWND_TOPMOST就OK了, 但是如果想要窗口始终位于最底端,Windows却没有提供接口. 不过呢,Windows提供了一个消息W ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...