java窗体管理项目_Windows窗体:后台工作程序同步和管理

我有一个问题跟随,非常简化的案例是我的项目的一部分 . 考虑我们有如下GUI:

03e7f8da-feec-4400-b934-beb6f59049cf.png

我有两个背景工作者:

plot_bgworker - 在这个例子中,它增加了绘图计数器,

data_bgworker - 在此示例中,它递增数据计数器 .

我还有label_timer,它会更新我的表单上显示的递增值 .

为了管理后台工作者和计时器,我写了两个函数:

private: void turnOnAcquisition() {

if (!counting_paused)

return;

if (!plot_bgworker->IsBusy)

plot_bgworker->RunWorkerAsync();

if (!data_bgworker->IsBusy)

data_bgworker->RunWorkerAsync();

label_timer->Enabled = true;

counting_paused = false;

}

private: void turnOffAcquisition() {

if (counting_paused)

return;

if (plot_bgworker->IsBusy)

plot_bgworker->CancelAsync();

if (data_bgworker->IsBusy)

data_bgworker->CancelAsync();

label_timer->Enabled = false;

counting_paused = true;

}

然后,当我点击每个按钮时会发生以下情况:

// Pauses counting on click

private: System::Void stop_btn_Click(System::Object^ sender, System::EventArgs^ e) {

turnOffAcquisition();

}

// Starts counting on click

private: System::Void start_btn_Click(System::Object^ sender, System::EventArgs^ e) {

turnOnAcquisition();

}

// Should restart counting on click, beginning from 0 (no matter what state counting is in right now)

private: System::Void restart_btn_Click(System::Object^ sender, System::EventArgs^ e) {

plot_counter = 0;

data_counter = 0;

turnOffAcquisition();

turnOnAcquisition();

}

最后,这里是我的后台工作人员(由CancelAsync()/ RunWorkerAsync()关闭/打开)和计时器:

// Calculating data counter

private: System::Void data_bgworker_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

for (;;) {

data_counter++;

Sleep(50);

if (data_bgworker->CancellationPending) {

e->Cancel = true;

return;

}

}

}

// Calculating plot counter

private: System::Void plot_bgworker_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) {

for (;;) {

plot_counter++;

Sleep(120);

if (plot_bgworker->CancellationPending) {

e->Cancel = true;

return;

}

}

}

// Display counters

private: System::Void label_timer_Tick(System::Object^ sender, System::EventArgs^ e) {

plot_counter_label->Text = numToMStr(plot_counter);

data_counter_label->Text = numToMStr(data_counter);

}

开始按钮和停止按钮都按预期工作,但现在我遇到重启按钮的问题 . 当我在计数过程中单击它时,它似乎重置值并停止后台工作者,但从不再启动它们(正如我在调用turnOnAcquisition后所期望的那样) . 但是,当我在计数关闭时单击它时,我可以按预期开启计数 .

我的第一个镜头是,当我试图检查我的工作人员是否忙时,取消标志还没有设置为另一个值,但是在呼叫之间使用Sleep()不起作用 . 另一个猜测是,它是由于竞争条件失败,所以我尝试使用MemoryBarrier(),但我不知道库,我不确定它是否会起作用 . 此外,我尝试使用Interlocked类,但无法正确使用它来处理void函数 .

1. Is this way of thinking correct? 2. If yes, why simple Sleep() doesn't do the trick? 3. How would I use any of mentioned methods in this case and which one would be the best match?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值