1

我在 C# 中有代码,其中 Window 正在获取消息(来自 GigE 相机的图像)。在函数处理程序中,我调用另一个函数对从消息中获得的图像进行一些处理。此处理可能需要比我收到下一条消息之间的时间更多的时间。我想使用一些不错的机制来忽略消息,直到处理完成。我可以简单地写:

bool is_processing = false;

void HandleUeyeMessage(int wParam, int lParam) 
{
   frame = getNewFrame();

   if(!is_processing) {
      doProcessing(frame);
   } else non-blocking ignore 
}

void doProcessing(frame f)
{
    is_processing = true;
    // some processing work...
    is_processing = false;
    return;
}

但我想使用一些同步机制,但我真的不知道该使用什么,因为这通常不是线程的东西......

4

1 回答 1

0

您不能忽略单个线程中的消息 - 当您的应用能够响应时,它们将被排队和处理。您可以使用带有一些基本同步的工作线程。

BackgroundWorker _worker=new BackgroundWorker();

void OnMessage(int lparam, int wparam)
{
    frame frame=GetFrame();
   if(!_worker.IsBusy)
        _worker.RunWorkerAsync(frame);
}

void DoWork(object sender,DoWorkEventArgs e)
{
    //do processing
}
于 2013-03-07T14:45:09.483 回答