The Application Is In Break Mode Error


Active member
Oct 12, 2023
Programming Experience
So my Windows Desktop application is writing a large amount of data to a binary file and has a ProgressBar that increments until it is written. There is one Sub that writes a lot of data and I get this error below, about halfway through:

Error - The application is in break mode.png

The exception states: "Managed Debugging Assistant 'ContextSwitchDeadlock' : 'The CLR has been unable to transition from COM context 0xd06388 to COM context 0xd062d0 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations."

I am able to click Continue and it finishes writing the file just fine. So I guess by reading this Exception, it wants be to display more on the screen while it is completing. I already have the ProgressBar updating AND I also have a Label that I update the text on constantly during the process.

I have the Performance panel turned on and I can see, as I would expect, it is fairly busy when the file is writing, but only when it's writing.

I do not want to add an On Error Resume Next event to my application.

I have another Sub where I write a file of the same length or longer, have the ProgressBar and Label update, but I do not get this error.

Is there a way to avoid this error or the best way to troubleshoot it further so I can get rid of it?

Thank you
This happens because you are preforming a time-consuming operation on the UI thread. You should use a Task or separate thread to do the work, leaving the UI thread free to manage the UI. If you don't want to have to learn too much new stuff, the easiest option is probably to use a BackgroundWorker. You call RunWorkerAsync and then do the work in the DoWork event handler, which is executed on a secondary thread. To update the UI while doing the work, call ReportProgress and handle the ProgressChanged event, the handler for which is executed on the UI thread. The RunWorkerCompleted event handler is also executed on the UI thread, once the work is done. If you're prepared to learn something new, asynchronous programming using Tasks would be the best way to go.
OK, I was able to search for Asynchronous Programming and I read a bit about it and saw some very basic examples. It has to be more difficult than just moving the routine to a
Public Async Function nnnn()
Public Asyn Sub nnn()

I can do that. My sub that writes the file is currently in a
Private Sub nnnn()

Does it work a little like VB6's DoEvents command?

Thank you.