After writing a console based app that uses Npcap libs to process packets, I've started on a Windows desktop app using VS 2022 C++ Win32 API which, at the moment, just displays packet source and destination IPs in a listbox. I have 2 buttons; Start and Stop which starts and stops the pcap_loop callback function.
When I click the Start button, packets are displayed in the Listbox OK, but clicking the Stop button takes a few clicks and a few seconds to respond and when it does, an exception occurs - Access violation at the pcap_loop function call.
I'm thinking this is because the pcap_loop is a callback function and is running at the same time as the WndProc callback function that processes Window messages, but correct me if I'm wrong.

WinMain has the standard code:
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

If my thinking is right, how can I run the 2 callback functions and gracefully stop the packet capture in the pcap_loop callback?

Thanks for any advice.