I am using the basic queued message handler to seperate my event handler and tasks. An event on the front panel (button pressed) puts a message in the queue. When the queue handles this message, it performs a task. However, I now want to add a button on the front panel that immediately terminates this task at any point in the task.
For example: pressing start on the front panel causes the message handling loop to count up to 10000. Pressing stop on the front panel should cause the counting to stop.
How can I implement this?
Solved! Go to Solution.
In general you don't want to perform a task in a single frame of your MHL that will take a long time. You have two options:
1. Have the MHL message itself to increment the number. That way you can enqueue a priority "stop" message on the queue if you want to stop while this is happening.
2. Have a parallel loop that is incrementing the number, and have the MHL send messages to that loop to start and stop it. Take a look at the Continuous Measurement and Logging sample project for an example of messaging between loops.
Suppose you have a Message that says "Start Counting" and passes in the desired number of counts (say, 10000). This State initializes the Counter and calls (via a Message on the Message Queue) an "Increment Count" which handles a single count, incrementing the count. If the count is less than MaxCount, send another Increment Message. When you finish, you can send a "Finished" message.
Note that since you send a Message for every point, you can easily send a Stop message which "sneaks in" and lets you terminate the Count. Note that when you process the Stop message, you may find that there's a Count message on the Queue -- you can look for this and remove it if you want.
This fits the suggestion that processing a Message should not take a long time, making the Message Handler responsive.