I've made some progress on this thanks to answers to my previous question. That turned out to be a duplicate manifestation of something already covered here. TLDR version: debugging in Win64 behaves "oddly" with Form_Load events.
What I'm trying to do now is find the best way to deal with this, and get VS work properly so that I can actually get down to programming/debugging. There are so many settings, in different places, that it's hard to keep track of them all. I did a full set of tests with every combination of these:
a. Build Platform: Any CPU or x64
b. "Prefer 32-bit" (N/A when x64 selected)
c. Just My Code (enabled/disabled)
d. Break on all thrown exceptions (not just user-unhandled ones)
e. Throw exception in Form.Load or Form.New
Kept constant: Application Framework enabled; startup object is Form1, thus called from this non-user code:
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.TheNameOfMyApplication.Form1
End Sub
Can't post the full results here as it's long, and I can't find a way to make tables in these posts. But the conclusion is that I see 4 different behaviours from the VS debugger. In order of Good to Bad to Seriously Ugly:
1. Break on actual line that caused the exception. All good: this is "proper" behaviour.
2. Break on the line in OnCreateMainForm (above). Inner exception gives the dirt. No way to get it to break inside the procedure where the exception is thrown (and e.g. look at what's actually happening).
3. Break on a blank (no code) window. At the top it says "No source available: the call stack contains only external code".
4. No break. Exception swallowed. Procedure where exception is thrown is just abandoned; lines after the throw are not executed. Program carries on, whistling a happy tune and hoping I don't notice.
Obviously I want behaviour 1. Here's what I found.
- The Build Platform and "Prefer 32-bit" settings make absolutely no difference in this test.
- Enabling Break on thrown exceptions (in Debug/Exceptions dialog) makes everything work properly (Good behaviour 1).
- Without this enabled, code in Form.Load always results in Ugly behaviour 4 (exception swallowed).
- Without this enabled, code in Form.New does either of two things. If JustMyCode is enabled, I get Bad behaviour 3 ("No source available"). If not, I get Bad behaviour 2 (break in the call that creates the form - can't get into the New constructor to see what's going on).
I was hoping that coding in Form.New rather than Form.Load (as advised in answer to my previous question) would be a workaround. Turns out that it's not. Unless I'm missing something, the only way I can get debugging to work is to enable Break on Thrown Exceptions. I would really like not to have to do this; among other things, I do want to catch and selectively handle exceptions, and breaking on all thrown exceptions makes this a total PITA to work with.
(Bear in mind that this project only got slightly past the Hello World stage before I ran into this problem - I'm not doing anything funky yet, and really don't want to have a PITA debugger behaviour hanging round my neck when I do).
Effectively I'm having to tick all the "Break on Thrown Exceptions" boxes, as a substitute for the option I'm really looking for:
I also installed the hotfix mentioned in previous answers, and tried setting up exception handlers in a Sub Main procedure, but that didn't seem to solve the problem either. Is this Break on Thrown Exceptions my best option?