# Midi thread not exiting nicely on windows

**URL:** <https://forum.juce.com/t/midi-thread-not-exiting-nicely-on-windows/7090>\
**Category:** General JUCE discussion\
**Created:** [June 28, 2011, 10:51am UTC](https://forum.juce.com/t/midi-thread-not-exiting-nicely-on-windows/7090 "2011-06-28T10:51:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jpo/32/503_2.png) [@jpo](https://forum.juce.com/u/jpo)\
**Post date:** [June 28, 2011, 10:51am UTC](https://forum.juce.com/t/midi-thread-not-exiting-nicely-on-windows/7090/1 "2011-06-28T10:51:16Z")

</div>

Hi Jules,

When I “quit” my application with a call to some emergency function such “FatalAppExit” in the audio callback, the system basically locks up. Not completely, but it is extremely hard to regain control of the computer : the program is still running with a single thread remaining, pumping all the cpu. Apparentely, it is the midiin callback that enters an endless loops : it is receiving a 0-length sysex , calls writeFinishedBlocks etc, and then the midicallback is called again with a 0-length sysex etc, endlessly. As it is very high priority stuff that explains the computer lockup. The solution is just to not call writeFinishedBlocks when hdr-\>dwBytesRecorded is zero – I don’t know if it is a correct solution or if it will break some sysex stuff:

` void handleSysEx (MIDIHDR* const hdr, const uint32 timeStamp)
    {
        if (isStarted && hdr->dwBytesRecorded != 0)
        {
            concatenator.pushMidiData (hdr->lpData, hdr->dwBytesRecorded, convertTimeStamp (timeStamp), input, callback);
            writeFinishedBlocks();
        }
    }
`

However this test used to be there in older juce release, in handleSysEx()

(This is on windows 7 btw, the audio / midi interface is an echo audiofire)

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [June 28, 2011, 11:18am UTC](https://forum.juce.com/t/midi-thread-not-exiting-nicely-on-windows/7090/2 "2011-06-28T11:18:15Z")

</div>

Sounds like a soundcard bug to me, but that looks like a reasonable workaround for it, thanks!

---

<div class="post-metadata">

**Author:** ![dinaiz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dinaiz/32/182_2.png) [@dinaiz](https://forum.juce.com/u/dinaiz)\
**Post date:** [June 28, 2011, 1:36pm UTC](https://forum.juce.com/t/midi-thread-not-exiting-nicely-on-windows/7090/3 "2011-06-28T13:36:22Z")

</div>

Thanks for the workaround !
