【问题标题】:Handling a stop or exit event in c++/cli console application在 c++/cli 控制台应用程序中处理停止或退出事件
【发布时间】:2012-07-10 04:42:42
【问题描述】:

我在控制台应用程序中有以下代码,我使用 TCP/IP 连接连接到扫描设备,连接建立后,我不断获得扫描测量结果。但我需要用户停止测量在任何时候。

我正在考虑在控制台应用程序本身中完成它。我需要为此使用线程吗?如果用户在扫描过程中停止,我想完成特定扫描并在此之后退出(类似于锁)。我只是在学习 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;
}

【问题讨论】:

    标签: c++ .net multithreading event-handling c++-cli


    【解决方案1】:

    一种简单的方法(在 Windows 上)是使用 SetConsoleCtrlHandler,然后阻止终止,直到您完成工作,当然,如果使用 CTRL_CLOSE_EVENT,您在 Win7/Vista 上的时间有限。

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

    【讨论】:

    • 你能给我一个例子吗..请.. 我在这里给出的 processscan 函数中有很多函数。我不知道如何停止中间的数据处理并再次恢复,直到用户发出停止命令时整个过程扫描功能完成..
    • 如果你在PocessScan中有一个主循环,你可以检查那个循环的终止条件,如果它更复杂,你应该在更多的地方检查条件。如果您对该怎么做感到困惑,最好发布ProcessScan 方法的框架,我会尽力帮助您。
    猜你喜欢
    • 1970-01-01
    • 2010-10-20
    • 2010-11-10
    • 2016-01-04
    • 2014-02-17
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2011-02-01
    相关资源
    最近更新 更多