人生是一场不能存盘的RPG,我只能尽量多搞几个Screenshot

May 20, 2006

.NET 多线程1_基本操作

Filed under: .NET

和Jave一样, .net从语言级别上支持了线程操作.

//—————–.NET 定义的线程的状态
System.Threading.ThreadState
System.Diagnostics.ThreadState

//——————–Thread 相关的class
System.Threading.Thread
System.Threading.ThreadStart

System.Threading.Timer

System.Threading.ThreadPool

 

//—————- Create Thread
Thread threadObj = new Thread(new ThreadStart(MyWorkerThreadMethod)); //此时state为 Unstarted.
threadObj.Start();  //注意,此时线程一定马上执行,OS会负责把线程从ready态设置到running态.

MyWorkerThreadMethod()要符合ThreadStart Delegate的格式: 无参数,无返回值.可以是静态方法.

//—————- 访问线程信息
Thread currentThreadObject =Thread.CurrentThread;
currentThreadObject.Name = "PrimaryThread";

//—————–操作线程
//—- 挂起
if (threadObject.ThreadState ==ThreadState.Running )
{
  threadObject.Suspend();
}

//—- 恢复
if (threadObject.ThreadState ==ThreadState.Suspended )
{
  threadObject.Resume();
}

//—- Sleep
Thread.Sleep(5000);
Thread.Sleep(TimeSpan.Infinite);

//—- 等待另一个线程结束
if(Thread.CurrentThread.GetHashCode() != threadObject.GetHashCode())
{
    threadObject.Join();   
    //or threadObject.Join(1000);      
}

//—- 结束线程
if (threadObject.IsAlive == true )
{
  threadObject.Abort();
}

会引发一个ThreadAbortException Exception

//——————线程同步
保证在某一时刻,只能有一个线程访问某个数据块

private static readonly object lockObj = newobject();

Test obj = null;
lock(lockObj)
{
     if(obj == null)
        obj = new Test();
}

[*]引用一道网上流传的c#面试题:
调用test方法时i>10时是否会引起死锁?

public void test(int i)
{
    lock(this)
    {
        if (i>10)
        {
            i - - ;
            test(i);
        }
    }
}
我想不会,整个代码中没有哪一行要访问this的实例数据.

 

参考
Working with Threads in C# (2006.05.18)
http://aspalliance.com/846

Multithreading in .NET
http://www.codeproject.com/dotnet/multithread.asp

CLR 的线程池(Jeffrey Richter)
http://www.microsoft.com/china/MSDN/library/netFramework/netframework/NECLRT.mspx?mfr=true
http://msdn.microsoft.com/msdnmag/issues/03/06/NET/

利用.Net 线程池提高应用程序性能
http://edobnet.cnblogs.com/archive/2005/11/29/287094.html

.NET’s ThreadPool Class - Behind The Scenes
http://www.codeproject.com/csharp/threadtests.asp

ASP.NET 2.0 中的异步页
http://www.microsoft.com/china/msdn/library/webservices/asp.net/issuesWickedCodetoc.mspx?mfr=true

如何取字符串的字节数

Filed under: Code snippets

int len = System.Text.Encoding.Default.GetBytes(strTest).Length;

和 strTest.Length 不同,后者返回的是字符数.

MS 著名写手Dino Esposito

Filed under: ASP.NET

http://msdn.microsoft.com/asp.net/community/authors/dinoesposito/default.aspx

Scott Guthrie有关vs2005的一些文章(转载)

Filed under: ASP.NET

转自http://blog.joycode.com/saucer/archive/2005/08/30/62690.aspx

ASP.NET之父Scott Guthrie有关ASP.NET 2.0和VS 2005的文章,

1。VS 2005中的Web项目系统提供的新功能 (VS 2005 Web Project System: What is it and why did we do it?)

2。如何在VS 2005和Web项目系统中使用 IIS (Using IIS with VS 2005 and the new Web Project system)

3。如何设置 ASP.NET 2.0的应用服务使用SQL Server 2000和SQL Server 2005 (Configuring ASP.NET 2.0 Application Services to use SQL Server 2000 or SQL Server 2005)

4。更好地管理VS 2005Web项目中文件的几个技巧 (Some techniques for better managing files in VS 2005 Web Projects)

5。如何使用VS 2005建立可重用的ASP.NET用户控件和页面库 (Building Re-Usable ASP.NET User Control and Page Libraries with VS 2005)






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham