我怎样才能保持我的运行函数,直到我单击 c++ 形式的按钮

How can I hold my running function until I click the button in c++ form?

本文关键字:单击 c++ 按钮 函数 我的 运行      更新时间:2023-10-16

我正在使用.net Framework 4和Visual Studio 2010。我正在使用 c++ 表单。我的代码是这样的:

int k = 0;
void writeFunction(int &k){
    ++k;
    textbox1->text = Convert::toString(k);
    //i want to suspend writeFunction in there, until i click the button1 which is on Form1 
    //because i don't want to stop function, it has to wait to click
    //after i clicked the button1 , the program continue to run code here
   writeFunciton(k);
}

你能添加更多关于你的程序的细节吗?您使用什么框架来生成表单,按钮按下如何与您的代码交互?

例如,如果您使用的是Qt,则可以使用信号和插槽将按下按钮与您的方法联系起来。根据框架的不同,可能会有其他适当的答案。

通常,我建议使用线程库来同步代码。

暂停 GUI 功能是不明智的。而不是在窗体类中使用操作状态变量,例如

enum OperationState 
 {
  os_normal,
  os_pointselection,
  os_event1,...
 }

调用文本框后>=...切换到选择状态,并使用此状态跟踪鼠标状态,例如单击后单击鼠标单击检查os_selection状态,然后继续您的职责。