我想 Button2 按下去时开始运行一段程序,Button3 按下去时停止于是我就void CChuankouDlg::OnButton2() 
{   
double xi;
short rtn;
int fangxiang=1;
GTInitial();
InputCfg();
    while (stop == 0)
{
...
}}
void CChuankouDlg::OnButton3() 
{
stop=1;
}结果运行一点 Button2 程序就死了,怎么办好啊?

解决方案 »

  1.   

    while( 1 )
    {
        while (stop == 0)
        {
            delay( 10 );
        }}
      

  2.   

    void
    delay( DWORD times )
    {
    DWORD dwStart = GetTickCount();
    DWORD dwEnd = dwStart;
    do
    {
    doevents();
    dwEnd = GetTickCount();
    } while( ( dwEnd - dwStart )  <=  times );
    }
      

  3.   

    单独开个线程干下面的事情:
    while (stop == 0)
        {
            ...
        }
    或者
        while (stop == 0)
        {
         MSG message;
        if(::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
        {
            ::TranslateMessage(&message);
            ::DispatchMessage(&message);
        }
        //干你自己的事
        }
      

  4.   

    void CChuankouDlg::OnButton2() 
    {   
        double xi;
        short rtn;
        int fangxiang=1;
        GTInitial();
        InputCfg();
        while (stop == 0)
        {
       MSG message;
      if(::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
      {
      ::TranslateMessage(&message);
      ::DispatchMessage(&message);
      }
      //干你自己的事
            ...
        }//while (stop == 0)}
      

  5.   

        while (stop == 0)
        {
            ...
        }    死循环,,放到线程里就行了
      

  6.   

        while (stop == 0)
        {
            PeekMessage();//看button3有没有按下。
        }
      

  7.   

    因为你的BTN2抢占不到资源,BTN1 的while把用户线程资源占尽了。要么你把while放到工作者线程里去,要么在while加个sleep(10),让BTN2,能拿到用户线程资源
      

  8.   

    我试了试在while中加延迟,不过还是不行,看来是你还有LS各位说的这种情况,得用开单独线程的方法。不过现在VC++还处于自学阶段,不知道怎么单独开个线程。