Greedquest / VBA-Timing-Methods

11 stars 3 forks source link

Allow End or GUI stop button when debugging TimerProcs #1

Open Greedquest opened 4 years ago

Greedquest commented 4 years ago

Right now, there is a known limitation that Ending code execution of a TimerProc (through the debugger + Stop button, or the End keyword) will crash Excel. This is because stopping the TimerProc from returning means WinAPI never gets a response from Excel, and there is no way of catching the Error with VBA code as End destroys everything.

Now RubberDuck uses a clever method of invoking methods in their test explorer which circumvents this issue

https://github.com/rubberduck-vba/Rubberduck/blob/e498d9501935428f1a4918b228429c273e56687f/Rubberduck.VBEEditor/ComManagement/TypeLibs/TypeInfoVBEExtensions.cs#L141

However this cannot be implemented directly in VBA since even if the method of invoking catches End statements, there needs to be some code to handle that caught error, and if it's VBA then it will be destroyed - https://chat.stackexchange.com/transcript/message/54875311#54875311

There are 3 solutions I can think of:

  1. Use the RD method of invocation, but write some assembler to convert from WinAPI timer procs and whatever signature the invocation requires, and this way VBA is avoided entirely

  2. Create an addin in C# or VB6 perhaps which does what RD does natively and is unaffected by the code termination

  3. Write the error handling stuff in native assembler and forward the function call through that

cristianbuse commented 4 years ago

I came across this repo by mistake and I remembered I saw on your GitHub account that you have a massive repo with timers and subclassing. I am having the same issue as you in my repo but I have it with hooking the mouse. I just thought you might want to see it. The owner does assembler as you mentioned here, but in a really cool way with no need for addins or dlls. Unfortunately it's a VB6 repo.