大可山博客[GDI+,WPF, .Net图形图像]

WPF,WinForms,asp.net开发,图形图像处理系统研究 (Johnson Blog)      [信奉:凡事靠自己] MSN:a3news(at)hotmail.com QQ:329325120

原创 有趣的多线程编程(1)——一个简单的例子收藏

//HelloWordThread.cs
//------------------------
using System;
using System.Threading;

public class Test
{
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();
        
        for (int i=0; i < 5; i++)
        {
            Console.WriteLine ("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }
    
    static void ThreadJob()
    {
        for (int i=0; i < 10; i++)
        {
            Console.WriteLine ("Other thread: {0}", i);
            Thread.Sleep(500);
        }
    }
}
结果:
Main thread: 0
Other thread: 0
Other thread: 1
Main thread: 1
Other thread: 2
Other thread: 3
Main thread: 2
Other thread: 4
Other thread: 5
Main thread: 3
Other thread: 6
Other thread: 7
Main thread: 4
Other thread: 8
Other thread: 9

//UsingDelegate.cs
------------------------------------
using System;
using System.Threading;

public class Test
{
    static void Main()
    {
        Counter foo = new Counter();
        ThreadStart job = new ThreadStart(foo.Count);
        Thread thread = new Thread(job);
        thread.Start();
       
        for (int i=0; i < 5; i++)
        {
            Console.WriteLine ("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }
}

public class Counter
{
    public void Count()
    {
        for (int i=0; i < 10; i++) { Console.WriteLine ("Other thread: {0}", i);
            Thread.Sleep(500);
        }
    }
}

发表于 @ 2006年01月27日 10:50:00|评论(loading...)

新一篇: 有趣的多线程编程(2)——线程中的参数传递 | 旧一篇: 图书条形码跟ISBN号互相转换的类(续)

用户操作
[即时聊天] [发私信] [加为好友]
大可山(Johnson)
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
大可山(Johnson)的公告
MSN:a3news(at)hotmail.com,从2007年8月8日起笔名改为:大可山(以前叫阿山Net)
Q:329325120
引用本人原作,请注明出处。
文章分类
收藏
软件开发
你的灯亮着吗?(RSS)
图书出版
大溪水的博客(RSS)
图形图像
C#新型报表工具 XDesigner(RSS)
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 大可山(Johnson)