Search code examples
c++mbed

c++ jump to eventhandler


I'm struggling with a GUI application in C++.

Clicking buttons activates routines, which may take some time. The window is then unresponsive. I would like it to keep listening and reactive. I heard about ISR, which seems unsuitable because I don't want to continue where I left, but rather forget about the aborted procedure and start fresh.

Please don't be harsh on me. I'd be glad if you can point me to somewhere useful. I've literally spent two hours finding nothing helpful (for me, might be my fault).


Solution

  • The exact solution is going to be very dependent on what you're trying to do, and what (toolkit, etc) you're trying to do it with.

    The quick (to give, not to do) answer is to use a separate thread for your work. You have one thread for your GUI stuff, and when the user hits a button you send a message to your worker thread from your GUI thread.

    If you're on C++11 or up, you can use std::thread to implement threads. Otherwise, you'll have to use whatever is available on your platform. These APIs are usually quite low level.

    std::async has a slightly higher level of operation. The GUI toolkit you're working with might have something. Whatever you choose, it's never going to be as 'simple' as a synchronous, single threaded program.

    In general, the earlier you think about your threading strategy in the design of a piece of software, the easier it is to implement. Adding multithreading to a well established program is often very difficult.

    Apologies if this is more 'vague' than you were hoping for. Perhaps if you add more details about platform and toolkits, people will be able to make more specific suggestions?

    Best of luck!