随笔分类 -  线程

摘要:staticvoidMain(string[]args){Threadth=newThread(Fun);th.Start();Thread.Sleep(1000);th.Abort();}staticvoidFun(){try{while(true){try{Test();}catch(ThreadAbortExceptionex)//在此处ThreadAbortException能被捕获到,但是还是会向上层冒泡{Console.WriteLine("线程被终止了");//由于是在异常捕获中休眠,所以即使其它线程调用此线程的Abort方法结束此线程,也得等休眠时间过去才能 阅读全文
posted @ 2011-05-18 16:16 再快一点 阅读(3046) 评论(0) 推荐(0)
摘要:static void Main() { Thread t1 = new Thread(Thread1); Thread t2 = new Thread(Thread2); t1.Start(); Thread.Sleep(100); t2.Start(); } static void Thread1() { Semaphore sema = new Semaphore(2, 3, "my"); sema.WaitOne(); sema.WaitOne(); Console.WriteLine("Thread1 get the Semaphore : " 阅读全文
posted @ 2011-03-21 10:55 再快一点 阅读(143) 评论(0) 推荐(0)
摘要:using System;using System.Threading;using System.Runtime.Remoting.Contexts;namespace ConsoleApplication4{ [Synchronization] public class MyCounter : ContextBoundObject { private int _expectedCounterVal; private int _currentCounterVal; private ManualResetEvent _event = new ManualResetEvent(false); pu 阅读全文
posted @ 2011-03-14 11:08 再快一点 阅读(690) 评论(0) 推荐(0)
摘要:using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO;using System.Linq;using System.Data.SqlClient;using System.Data;using System.Text.RegularExpressions;using System.Diagnostics;using System.Net.Mail;using System.Threading;using System.C 阅读全文
posted @ 2010-11-16 10:06 再快一点 阅读(794) 评论(0) 推荐(0)
摘要:转自:http://hi.baidu.com/oohacker/blog/item/6004e6fb712feb254e4aea24.html有关C#锁语句的讨论网上说得很多, 但绝大多数只说了了怎么用, 对于lock的注意事项却很少, 尤其是关于lock(this), lock(typeof(ClassName))以及lock("thisLock")的讨论更是凤毛麟角, 就连MSDN中对于前面提及的三种lock可能导致的问题也是一笔带过(http://msdn.microsoft.com/zh-cn/worldwide/c5kehkcz.aspx)。百度和Google了许久,终于收集到 阅读全文
posted @ 2010-07-28 16:04 再快一点 阅读(1011) 评论(0) 推荐(2)