博文

指向多个函数的委托(多播)(2007-09-29 20:41:00)

摘要:using System;
using System.Collections.Generic;
using System.Text; namespace _8_2
{
    class Program
    {
        public delegate float calcincome(float hourlywages);       
        public static readonly calcincome monthlyincome = new calcincome(person.calcmonthlyincome);
        public static readonly calcincome annualincome = new calcincome(person.calcannualincome);         public static readonly calcincome allincomes = monthlyincome + annualincome;         static void Main(string[] args)
        {
            try
            {
      &nbs......

阅读全文(1953) | 评论:0

作为静态成员的委托(2007-09-29 20:20:00)

摘要:using System;
using System.Collections.Generic;
using System.Text; namespace _8_2
{
    class Program
    {
        public delegate float calcincome(float hourlywages);         //委托可以打包成属性或者类的静态成员
        public static readonly calcincome monthlyincome = new calcincome(person.calcmonthlyincome);
        public static readonly calcincome annualincome = new calcincome(person.calcannualincome);
       
        static void Main(string[] args)
        {
            try
            {
              &nb......

阅读全文(2303) | 评论:0

委托中的异常处理(2007-09-29 20:07:00)

摘要:using System;
using System.Collections.Generic;
using System.Text; namespace _8_2
{
    class Program
    {
        public delegate float calcincome(string hourlywages);
         
        static void Main(string[] args)
        {
            calcincome del = new calcincome(person.calcannualincome);
            Console.WriteLine(del("hihg")); //委托中的异常处理?????,怎么处理
           
        }
    }     public class person
    {
        public float income;
     &nb......

阅读全文(1956) | 评论:0

委托入门例子(2007-09-29 19:56:00)

摘要:using System;
using System.Collections.Generic;
using System.Text; namespace _8_1
{
    class Program
    {
        public delegate float calcincome(float hourlywages); //  声明委托类型
        static void Main(string[] args)
        {
            calcincome del = new calcincome(person.calcannualincome); //实例化委托对象
            Console.WriteLine(del(26));                    //调用委托对象
            calcincome del2 = new calcincome(person.calcmonthlyincome);
            Console.WriteLine(del2(26));
  ......

阅读全文(1826) | 评论:0

浅析.Net下的多线程编程(2)(2007-09-23 20:14:00)

摘要://来自 http://developer.51cto.com/art/200509/3358_1.htm   线程优先级

一旦一个线程开始运行,线程调度程序就可以控制其所获得的CPU时间。如果一个托管的应用程序运行在Windows机器上,则线程调度程序是由Windows所提供的。在其他的平台上,线程调度程序可能是操作系统的一部分,也自然可能是.Net框架的一部分。不过我们这里不必考虑线程的调度程序是如何产生的,我们只要知道通过设置线程的优先级我们就可以使该线程获得不同的CPU时间。

线程的优先级是由Thread.Priority属性控制的,其值包含:ThreadPriority.Highest、ThreadPriority.AboveNormal、ThreadPriority.Normal、ThreadPriority.BelowNormal和ThreadPriority.Lowest。从它们的名称上我们自然可以知道它们的优先程度,所以这里就不多作介绍了。

线程的默认优先级为ThreadPriority.Normal。理论上,具有相同优先级的线程会获得相同的CPU时间,不过在实际执行时,消息队列中的线程阻塞或是操作系统的优先级的提高等原因会导致具有相同优先级的线程会获得不同的CPU时间。不过从总体上来考虑仍可以忽略这种差异。你可以通过以下的方法来改变一个线程的优先级。

thread.Priority = ThreadPriority.AboveNormal;

或是:

thread.Priority = ThreadPriority.BelowNormal;

通过上面的第一句语句你可以提高一个线程的优先级,那么该线程就会相应的获得更多的CPU时间;通过第二句语句你便降低了那个线程的优先级,于是它就会被分配到比原来少的CPU时间了。你可以在一个线程开始运行前或是在它的运行过程中的任何时候改变它的优先级。理论上你还可以任意的设置每个线程的优先级,不过一个优先级过高的线程往往会影响到其他线程的运行,甚至影响到其他程序的运行,所以最好不要随意的设置线程的优先级。

挂起线程和重新开始线程



Thread类分......

阅读全文(1676) | 评论:0

时钟线程(2007-09-23 20:03:00)

摘要:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; class MyApp
{
    private static bool TickNext = true;     public static void Main()
    {
        Console.WriteLine("Press Enter to terminate...");         TimerCallback callback = new TimerCallback(TickTock);         Timer timer = new Timer(callback, null, 1000, 1000);         Console.ReadLine(); //为什么 按 enter 键结束?? 拖延程序执行时间,没有这行,看不什么结果。     }     private static void TickTock(object state)
    {
        Console.WriteLine(TickNext ? "Tick" : "Tock");         TickNext = !TickNext;     }
}......

阅读全文(1862) | 评论:0

后台线程例子(2007-09-23 19:46:00)

摘要:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; class MyApp
{
    static int i = 0;
    public static void Main()
    {
        for (i = 0; i < 10; i++)
        {             Thread thread = new Thread(new ThreadStart(ThreadFunc));
            thread.IsBackground = true;                    //标记为后台线程。
            thread.Start();         }
    }     private static void ThreadFunc()
    {
        int j = i;
 &......

阅读全文(1891) | 评论:0

前台线程例子(2007-09-23 19:44:00)

摘要:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; class MyApp
{
    static int i = 0;
    public static void Main()
    {
        for (i = 0; i < 10; i++)
        {             Thread thread = new Thread(new ThreadStart(ThreadFunc));             thread.Start();         }
    }     private static void ThreadFunc()
    {
        int j = i;
        DateTime start = DateTime.Now;         while ((DateTime.Now - start).Seconds < 2)
        {
   ......

阅读全文(2321) | 评论:1

启动线程(2007-09-22 15:29:00)

摘要:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading; namespace _6_1
{
    class Program
    {
        int i = 0;
        static void Main(string[] args)
        {
            //thread 类的构造函数将threadstart 委托的实例作为唯一参数
            Thread t = new Thread(new ThreadStart(new Program().calculate));
            t.Start();//线程实例话后,用start方法让它运行起来
        }
        public void calculate()
        {
            Console.WriteLine("the value of i is {0}",++i);
......

阅读全文(1796) | 评论:0

序列化与深度复制例子(2007-09-22 14:53:00)

摘要:using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary; namespace _5_14
{
    public class DeepCopyUtility
    {
        public static void Main()
        {
            Region obj = new Region("Reg 1");
            Console.WriteLine(obj);             Region copiedobj = (Region)DeepCopy(obj);
            Console.WriteLine(copiedobj);             Console.WriteLine(obj == copiedobj);
        } &nb......

阅读全文(2034) | 评论:0