Cannot quit Visual FoxPro

[Cannot Quit VFP message] The 'Cannot Quit Visual FoxPro' error message must be one of the most common problems new developers meet when starting to work with Visual FoxPro.

It has been with us since the launch of Visual FoxPro 3 but the problem still causes difficulties. The error message does not give you an error number so you cannot find it in the VFP Help system. Help also returns nothing if you search for 'Cannot Quit Visual FoxPro' and the online help at MSDN is equally unhelpful. A search on Google though will show you that it's a very common question in all the FoxPro support forums so it's disappointing that Microsoft have not added it to their own Help documentation.

Especially as it is a very simple problem to fix.

Close the application

The first thing to do is to close the application that has locked. The least damaging way of doing this is to use the Windows Task Manager. In Windows 2000 and Windows XP you can open the Task Manager by pressing Ctrl+Alt+Del or by right-clicking on the Windows status bar. Select Task Manager from the options that appear then click the Applications on the Task Manager dialog:

[Windows Task Manager]

Select your Visual FoxPro application from the list then click the End task button. You will be warned that this is an extreme act and asked to confirm that you do indeed want to cancel this process. Click OK and wait. Windows may take several seconds to complete the task of closing down the application. When it has been closed you will be told that you have terminated FoxPro prematurely and asked whether you want to report the problem to Microsoft. We have been reporting this for ten years now so I suspect it will be a waste of time to report it again.

Now that you have cancelled the application, close the Task Manager and open your project up again in FoxPro.

Solution

The problem arises because Visual FoxPro is event-driven. The RunTime engine opens your executable and then responds to events as they occur. You have closed your application but not told FoxPro to stop waiting for events.

The command to stop the event-processing loop is:

Clear Events

and FoxPro must execute this command whichever way that the application closes down. Depending on the structure of your application you might put the command is several different places:

  • In the procedure called by the Quit option on the File menu.
  • In the Click event of the button that closes your main form.
  • When the application shuts down:
    On Shutdown Clear Events

FoxPro executable flashes on screen and vanishes

The other problem you'll hit with the Visual FoxPro event-processing loop is when you find that your executable won't run outside of the development environment. You just see a flash of your main form on screen before the executable closes. This too is tied up with the event-processing loop. More details here.