天下無雙
阿龍 --质量是流程决定的。
ThreadDemo
    class ThreadDemo
    {

        
public static void ThreadProc()
        {
            
for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(
"ThreadProc:{0}", i);
                Thread.Sleep(
0);
            }
        }

        
public static void Main()
        {
            Console.WriteLine(
"在主进程中启动一个线程!");
            Thread t 
= new Thread(new ThreadStart(ThreadProc));
            t.Start();
//启动线程

            Thread ts 
= new Thread(new ThreadStart(ThreadProc));
            ts.Start();
//启动线程
            ts.Suspend();//挂起该线程

            
for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(
"主进程输出.....");
                Thread.Sleep(
0);//线程被阻塞的毫秒数.0表示应挂起此线程以使其他等待线程执行
            }

            Console.WriteLine(
"主线程调用线程Join方法直到ThreadProc1线程结束.");
            t.Join();
//阻塞调用线程,直到某线程终止时为止.
            Console.WriteLine("ThreadProc1线程结束.");
            ts.Resume();
           
//ts.IsBackground = true; //ts默认为前台线程,主进程要等ts执行完才会结束.如果rs设置会true,则主线程结束时,整个线程结束,不会等待ts执行完成.

        }

    }
SimpleThreadWithArgs
namespace ThreadArgs
{
    
public class SimpleThread
    {
        
private string procParameter = "";
        
public SimpleThread(string strPara)
        {
            procParameter 
= strPara;
        }

        
public void WorkerMethod()
        {
            Console.WriteLine(
"参数输入为:" + procParameter);
        }

    }

    
class MainClass
    {
        
static void Main(string[] args)
        {
            SimpleThread st 
= new SimpleThread("这是参数字符串!");
            Thread t 
= new Thread(new ThreadStart(st.WorkerMethod));
            t.Start();
            t.Join(Timeout.Infinite);
        }
    }

}
ThreadInWS
    class ThreadInWS
    {
        
static void Main()
        {
            localhost.StockService stock 
= new localhost.StockServce();

            
try
            {
                
//同步调用
                Console.WriteLine("请输入股票号:");
                
string strStcok = Console.ReadLine();
                
int nDelay = int.Parse(Console.ReadLine());
                Console.WriteLine(
"同步调用开始:");
                Console.WriteLine(
"结果为:"+stock.GetStock(strStcok,nDelay));
                Console.WriteLine(
"同步调用结束!");
                Console.ReadLine();

                
//异步调用
                Console.WriteLine("异步调用开始:");
                IAsyncResult aResult;
                aResult 
= stock.BeginGetStock(strStcok, nDelay, nullnull);
                Console.WriteLine(
"异步调用!进程没有被阻塞!");
                
while (!aResult.IsCompleted)
                {
                    Console.Write(
".");
                }
                Console.WriteLine(
"方法调用返回!");
                Console.WriteLine(
"结果为:"+stock.EndGetStaock(aResult));
            }
            
catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }

ThreadInWS说明:

                       StockService为已创建的WebServices,包含GetStock(string,int)方法.

                       WebService自动创建BeginGetStock(),EndGetStock()方法供异步调用.

 

使用委托的异步调用
        private delegate string MyMethodDelegate();

        
static void Main()
        {
            MyClass myClass 
= new MyClass();

            
//方式1:声明委托,调用MyMethod1
            MyMethodDelegate d = new MyMethodDelegate(myClass.MyMethod1);
            
string strEnd = d();
            Console.WriteLine(strEnd);

            
//方式2:声明委托,调用MyMethod2 (使用AsyncResult对象调用)
            d = new MyMethodDelegate(myClass.MyMethod2);//定义一个委托可以供多个方法使用
            AsyncResult myResult; //此类封闭异步调用的结果,通过AsyncResult得到结果
            myResult = (AsyncResult)d.BeginInvoke(nullnull); //开始使用
            while (!myResult.IsCompleted) //判断线程是否执行完成
            {
                Console.WriteLine(
"正在异步执行MyMethod2...");
            }
            Console.WriteLine(
"方法MyMethod2执行完成!");
            strEnd 
= d.EndInvoke(myResult);//等待委托调用的方法完成,并返回结果.
            Console.WriteLine(strEnd);
            Console.Read();

        }


 

posted on 2010-04-15 15:43  阿龍  阅读(124)  评论(0编辑  收藏  举报