2012-07-10 138 views
0

我在控制台应用程序中使用TCP/IP连接来连接扫描设备,并且在连接建立后,我有下面的代码:扫描测量。但我需要用户在任何时候停止测量。在C++/cli控制台应用程序中处理停止或退出事件

我想在控制台应用程序本身完成它。我是否需要为此使用线程?如果用户在扫描过程中停止,我想完成特定的扫描并在此之后退出(类似锁)。我只是在学习C++和C++/cli ..任何建议都会有帮助..谢谢

if (sopas.GetScanData(soScan) == true) 
{ 
printf("Continuous Scanning Measurement \n"); 
processcounter = ProcessScan(soScan,processcounter,gp,Background); 
} 

过程扫描该骨架是

unsigned int ProcessScan(const SLms100Scan& soScan,int iProcessedScanCount,FILE* gp,vector <double> &Background) 
{ 
     static unsigned int ulDeg90_index = 0; // Calculated during init phase 
    static unsigned int ulProcessedScanCounter = 0; 
    int iDataCount = soScan.aDataChannel16.aFlexArrayData[0].aData.uiFlexArrayLength; 

    int datalength = soScan.aDataChannel16.aFlexArrayData[0].aData.uiFlexArrayLength; 
    vector <double> afXvalue(datalength); 
    vector <double> afYvalue(datalength); 

    pair<double,double> coord(int a,int b); 
    vector< pair<double,double>> coordinates; 

    int aiDataReal[80]; 
    int count; 

    //Finding the blobs 

    // Write hex values of data in to the file 
    int success = 0; 
    writefile(fname,soScan,success); 
    if (success != 0) 
     { 
     printf("Error in printing the file"); 
     } 

    blobdetector(soScan, iProcessedScanCount,Background,coordinates); 
    // Read the hex value for XY co-ordinate calculation 
    //readfile(fname,aiDataReal,idx,success); 

    if (success == -1) 
     printf("Error in reading the file"); 

    //calculation of Polar Coardinates to XY values 
    findXY(soScan,afXvalue,afYvalue); 

    //writing the XY values to the file 
    writefile(wfname_XY,iDataCount,afXvalue,afYvalue,success); 
    if (success == -1) 
     printf("Error in printing the file"); 

    plotdata(afXvalue,afYvalue,coordinates,gp,iProcessedScanCount); 

    if (!coordinates.empty()) 
     { 
     //sendcoordinates(soScan,coordinates); 
     writefile("coordinates",soScan,coordinates); 
     coordinates.clear(); 
     } 

    iProcessedScanCount++; 
    return iProcessedScanCount; 
} 

回答

0

一个简单的方法(在Windows上)是使用SetConsoleCtrlHandler然后块终端,直到你的CTRL_CLOSE_EVENT情况下完成自己的工作,当然你只有有限时间在Win7/Vista上。

您可以使用不同的机制来指示您的主程序完成它的工作。在我的情况下,我在主文件中使用了一个全局变量,通过引用我的内部循环来传递它,并将其用作循环终止条件。但它也可以用其他方法实现(condition_variable,...)

+0

可以给我一个例子..pls ..我在这里给出的processscan函数里面有很多函数。我不知道如何停止它们之间的数据处理,然后再次恢复,直到用户发出停止命令时整个过程扫描功能完成为止。 – ShivShambo 2012-07-10 05:13:31

+0

如果在“PocessScan”中有一个主循环,则可以检查该循环中的终止条件,如果它更复杂,你应该在更多的地方检查条件。如果您对要做什么感到困惑,最好发布'ProcessScan'方法的框架,我会尽量帮助您。 – Mohammad 2012-07-10 12:56:50