【问题标题】:FLTK event mapping / multithreadingFLTK 事件映射/多线程
【发布时间】:2012-11-22 07:13:02
【问题描述】:

我是 FLTK 和 C++ 的新手。我需要一些关于如何让活动正常工作的帮助。在其他语言中,您创建一个按钮并定义一个函数,当用户单击它时,该函数将在不同的函数中处理来自 button1 和 button2 的事件。就像向 button1 添加事件监听器并将其映射到 button1_click()。

这是来自多线程环境的一些代码。我想知道如何监听来自 button1 和 button2 的点击并正确处理它们。

此外,通过这种设计,我计划在循环中每 200 毫秒在 GUI 上设置一个单独的线程更新数据。如果我在这个循环中调用 lock 和 unlock,是否有可能引发异常?

谢谢!!

#pragma once

#include <Fl.H>
#include <Fl_Window.H>
#include <Fl_Button.H>
#include <Windows.h>

class MGui
{
    public:
    Fl_Window*  window;
    Fl_Button*  button1;
    Fl_Button*  button2;

    static MGui &i() 
    {
        static MGui sMGui;
        return sMGui;
    }
    static void init() 
    {
        i();
        DWORD dwThreadId;
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) run, (LPVOID) 0, NULL, &dwThreadId); 
    }
    static DWORD_PTR WINAPI run(void *theParam)
    {
        i().window = new Fl_Window(100, 100, 340, 180, "Window");
        i().button1 = new Fl_Button(10, 10, 50, 24, "Button1");
        i().button2 = new Fl_Button(10, 44, 50, 24, "Button2");
        i().window->end();
        i().window->show();
        Fl::run();
        return 0;
    }
};

【问题讨论】:

    标签: c++ fltk


    【解决方案1】:

    То 使用一个按钮执行一些操作,您需要传递一个回调函数:

    void cancel_callback(Fl_Widget* obj, void* data)
    {
    ..
    //do something
    ..
    }
    ...
    cancel = new Fl_Button(x, y, x1, y1, "Cancel");
    cancel->callback(cancel_callback,(void*)this);
    

    从线程更新 UI 是不安全的。它应该只从主线程完成。在 thFn 中使用 Fl::awake (Fl_Awake_Handler cb, void *message=0) 在主线程中执行一些回调函数。

    void updateUI(void *userdata) 
    {
    ...
    // update UI
    ...
    }
    
    void* thFn(void* param)
    {
    ...
    Fl::awake(updateUI, userdata);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多