C# WebService中任务处理线程创建子线程后
protected void WriteLog(string message)
{
lock (lockObject)
{
var file = System.IO.File.AppendText("C:\\log.txt");
file.WriteLine(message);
file.Close();
}
} protected void asyctest(int threadid)
{ this.WriteLog(string.Format("主线程({0})的子线程({1})开始", threadid, AppDomain.GetCurrentThreadId())); System.Threading.Thread.Sleep(10000); ProcessThread thread = this.GetThreadbyID(threadid); this.WriteLog(string.Format("主线程({0}: state: {1})的子线程({2})结束", threadid, thread == null ? -1 : (int)thread.ThreadState, AppDomain.GetCurrentThreadId())); } protected ProcessThread GetThreadbyID(int id)
{
foreach (ProcessThread item in System.Diagnostics.Process.GetCurrentProcess().Threads)
{
if (item.Id == id)
{
return item;
}
} return null;
} protected delegate void delegatetest(int threadid); [WebMethod]
public void test()
{
this.WriteLog(string.Format("主线程({0})开始", AppDomain.GetCurrentThreadId())); for (int i = 0; i < 10; i++)
{
delegatetest currentDaliyCollect = new delegatetest(asyctest);
IAsyncResult iADaliyCollect = currentDaliyCollect.BeginInvoke(AppDomain.GetCurrentThreadId(), null, null);
if (iADaliyCollect.IsCompleted)
{
currentDaliyCollect.EndInvoke(iADaliyCollect);
} }
}
结论:
C# WebService中任务处理线程创建子线程后,此任务处理线程并没有退出销毁,而是ThreadState切换成Wait状态,所以子线程也不会被中止,而得以继续执行。
本来是想验证,父线程退出后,子线程是否会被强制中止。时间关系没有验证,至少验证了在webservice,不用担心在rpc方法中创建的子线程会被中止。
C# WebService中任务处理线程创建子线程后的更多相关文章
- Handler系列之创建子线程Handler
上一篇我介绍了Handler机制的工作原理,默认情况下,ActivityThread类为我们创建的了主线程的Looper和消息队列,所以当你创建Handler之后发送消息的时候,消息的轮训和handl ...
- C#创建子线程,子线程使用委托更新控件
一.背景 由于在窗体程序中通过点击一个button按键后需要更新TreeView控件的内容,由于等待时间比较长,主程序无法一起在那边等待,需要去处理其它的事情,所以就需要创建新的子线程来处理.因为主线 ...
- vc中主线程等待子线程退出的方法
VC线程同步,在子线程中等待另一子线程结束,通过WaitForSingleObject可以实现,但是如果在主线程中等待子线程结束,这个函数是无法完成要求的,因为它会造成主线程挂起,导致程序死掉.我们可 ...
- 通过类创建子线程&同步锁
一.通过类创建子线程 import threading class MyThread(threading.Thread): def __init__(self,num): threading.Thre ...
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- IOS 线程处理 子线程
IOS 线程处理 子线程的启动与结束 技术交流新QQ群:414971585 IOS中,如果要在主线程中启动一个子线程,可以又两种方法: [NSThread detachNewThreadSelec ...
- Java多线程01(Thread类、线程创建、线程池)
Java多线程(Thread类.线程创建.线程池) 第一章 多线程 1.1 多线程介绍 1.1.1 基本概念 进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于 ...
- c# Winfrom窗体事件中启用多线程 并用子线程修改窗体里面的属性
昨天一个朋友问我一个问题,需求是 this.textBox1.Text = "睡眠前"; Thread.Sleep(1000); this.textBox1.Text = &quo ...
- 【转载】在C#中主线程和子线程如何实现互相传递数据
引用:https://blog.csdn.net/shuaihj/article/details/41316731 一.不带参数创建Thread using System; using System. ...
随机推荐
- 【转】使用 JMeter 完成常用的压力测试
本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web 服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项. ...
- 一个for循环写出9*9乘法表
public class ClassTest03 { public static void main(String[] args) { for(int i=1,j=1;j<=9;j++){ if ...
- [转]SQLite 经验集
SQLite 的默认时间 转自:http://www.cnblogs.com/pennant/archive/2011/08/11/2134897.html select strftime('%Y-% ...
- WP8 MVVM设计模式
类似了Android里边的MVC模式, Windows Phone 有自己的Model-View-ViewModel模式,这种模式的作用就是为了Data和UI分离开来. 如果你英文较好的话,你可以不再 ...
- 生成html页面客户端随机数和验证码
生成随机数: var chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', ...
- 根据段落编号自动添加书签的VBA
Sub 宏1() ' ' 宏1 宏 ' ' Dim myRange As Word.Range Dim num As String, content As String Selection.Ho ...
- http请求头设置
Curl curl -i -H 'Content-Type: application/json' -i/--include 输出时包括protocol头信息curl -x 10.12.15.1 ...
- Python环境搭建之OpenGL
以下内容为我python OpenGl 环境搭建历程: win7 64位操作系统,python3.5.3 ,无其他相关. 直接cmd或PowerShell输入以下命令: pip install PyO ...
- Python 小结
1. Python pass是空语句,是为了保持程序结构的完整性. pass 不做任何事情,一般用做占位语句. 2.删除一个list里面的重复元素 方法一:是利用map的fromkeys来自动过滤重复 ...
- Delphi 画箭头
procedure TForm1.Line(x, y, x2, y2: integer); begin canvas.MoveTo(x, y); canvas.LineTo(x2, y2); end; ...