我写了两个在main入口的方法.为什么只能执行第一个.有没有办法让两个都执行!谢谢
我的程序为什么只能执行第一个方法?
using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Data; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Timer tmr = new Timer(new TimerCallback(DoTimer), null, 0, 900000); Console.Read(); Timer tmr1 = new Timer(new TimerCallback(DoTimer1), null, 0, 1200000); Console.Read(); } static void DoTimer(object state) { //do something  } static void DoTimer1(object state) { //do something  } } }

解决方案 »

  1.   

    main()是程序的主入口,只会执行一个.如果让两个都运行,就在main里面调用另外一个方法.
      

  2.   

    去掉tmr后面的Console.Read(); 
      

  3.   

    不好意思重新贴代码      using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading; 
    using System.Data;
    namespace ConsoleApplication1

        class Program 

            static void Main(string[] args)
            {
                Timer tmr = new Timer(new TimerCallback(DoTimer), null, 0, 900000);
                Console.Read(); 
                Timer tmr1 = new Timer(new TimerCallback(DoTimer1), null, 0, 1200000);
                Console.Read(); 
            } 
            static void DoTimer(object state)
            {
                //do something  
            }
            static void DoTimer1(object state) 
            { 
                //do something 
            } 
        } 
    }      
      

  4.   

    to libinguest
    他说的不是两个main方法,是两个回调:TimerCallback!
    楼主这代码写得,没话说!
      

  5.   

    你这个程序是顺序执行的,要等Console.Read(); 执行玩了以后在执行下面的,如果要并行要开启多个线程
      

  6.   


          using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading; 
    using System.Data;
    namespace ConsoleApplication1

        class Program 

            static void Main(string[] args)
            {
                Timer tmr = new Timer(new TimerCallback(DoTimer), null, 0, 900000);
                
                Timer tmr1 = new Timer(new TimerCallback(DoTimer1), null, 0, 1200000);
                Console.Read(); 
            } 
            static void DoTimer(object state)
            {
                //do something  
            }
            static void DoTimer1(object state) 
            { 
                //do something 
            } 
        } 
    }
      

  7.   


    多線程類:
    System.Threading.Thread
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading; 
    using System.Data;
    namespace ConsoleApplication1

        class Program 

            static void Main(string[] args)
            {
                Timer tmr = new Timer(new TimerCallback(DoTimer), null, 0, 900000);
               // Console.Read(); 
                Timer tmr1 = new Timer(new TimerCallback(DoTimer1), null, 0, 1200000);
                Console.Read(); 
            } 
            static void DoTimer(object state)
            {
                //do something  
            }
            static void DoTimer1(object state) 
            { 
                //do something 
            } 
        } 
    }
      

  9.   

    并且要能给我的timer匹配.我实在想把它们两个一起执行.
      

  10.   


    如何改进呀.我以都是写的asp.net方面的.对这winform实在是不行
      

  11.   

    高手们!我已经去了第一个
    Console.Read(); 
    程序已经可以执行下一个.但是只有执行第一个后才能执行第二个.能不能同时执行这个呀?
      

  12.   

    绝对意义是的同时是不可能的,两个方法肯定得有一个执行的早些,另一个稍晚些,
    你如何知道第二个一定是在第一个方法完全执行完毕后才启动的呢?
    建议你在两个方法里的第一行前和最后一行后加两个语句:Console.WriteLine("《方法名》started");和Console.WriteLine("《方法名》Ended"); 然后看看这两对语句的输出是否“交叉”!
    当然,基于你的这段代码,不用看也知道不会“交叉”,原因很简单:看看你两个timer的间隔时间吧,一个900秒,另一个1200秒,什么概念?有什么方法在300秒内还执行不完呢?