编了一个winform的程序(注:要求是winform的形式,不许写成服务程序),数据库中只要有新数据,就从数据库读取数据再进行运算后把结果再保存到数据库中,这个运算过程需要很长时间。第一次的时候把整个已有的数据进行运算(大概90多万条)得10个小时以上。这个winform的要求是:
1. 每次运行程序的时候都要自动进行运算,这时winform界面要显示出来,并且运算完成后把运算时间显示在这个winform上。
2. 在运算的过程中,可以暂停或者取消操作,这时就不进行运算,从自动运行的方式跳出来。第一个问题:
我把一些对于Form的加载数据写到了构造函数和Load函数中了,其中进行计算数据的主程序我写到了Load函数中了,但是这种情况当运行程序进行自动运算时候不能显示主界面,(要求显示出来)。看不见界面,当运算完成时候才能看见。不知道这个问题该怎么解决呢?要求运行程序的时候自动运行计算过程,如果不写在比如Load程序中那应该怎么办呢?第二个问题:
不知道该怎么能实现在数据运算的过程中,比如按下暂停或取消按钮,程序从自动运行的方式跳出来。因为这个很重要,因为运算过程时间过长,要求随时可以终止运算过程。先谢谢大家了,过年了,希望大家心想事成!

解决方案 »

  1.   

    用线程暂停:   
    using   System.Threading;   
        
    Thread.Sleep(1000);   [DllImport("kernel32.dll")]   
      private   static   extern   void   Sleep   (int   dwMilliseconds);   
        
      The   Sleep   function   suspends   the   execution   of   the   current   thread   for   at   least   the   specified   interval.   
        
      dwMilliseconds     
      [in]   Minimum   time   interval   for   which   execution   is   to   be   suspended,   in   milliseconds.     
      A   value   of   zero   causes   the   thread   to   relinquish   the   remainder   of   its   time   slice   to   any   other   thread   of   equal   priority   that   is   ready   to   run.   If   there   are   no   other   threads   of   equal   priority   ready   to   run,   the   function   returns   immediately,   and   the   thread   continues   execution.   
        
      A   value   of   INFINITE   indicates   that   the   suspension   should   not   time   out.   
      

  2.   

    参考:
    http://msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker.aspx
      

  3.   

    第一个问题:
    你如果不想在界面加载的时候处理你的运算你可以加个button也可以啊,只有在你点击button的时候才会执行你的运算。第二个问题:
    其实线程来操作我认为并不是真正意义上的暂定,但是可以起到暂停的作用了换了我,我会选择用事件+委托来操作,就是一个发一个负责接收。。
    你的暂停就可以通过取消注册事件来完成
      

  4.   

    第一个在load中消耗时间,使用多线程分页获取数据实现计算,Apllication.DoEvents();
    使用线程实现中断、暂停等,记录日志
      

  5.   


    那你怎么知道那些处理了,那些没有处理呢?
    你就在那些没有处理的中间,每次top 100,就可以了呀