I am currently participating in a project that is porting old VB6 code to C # (.Net Framework 3.5). My mandate is simply to migrate; any functional improvements or refactoring should be carried forward to a later stage of the project. Not perfect, but there you go.
Thus, part of the VB6 code calls the SetTimer function of the Windows API. I have undergone this operation and cannot make it work.
A migrated project is created as a DLL; I created a small WinForms test harness that references a DLL and calls this code. Very simple, just to prove that a challenge can be made.
The corresponding code in the redirected DLL is as follows:
[DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] public extern static int SetTimer(int hwnd, int nIDEvent, int uElapse, AsyncObjectCallerDelegate lpTimerFunc); public delegate void AsyncObjectCallerDelegate(int hwnd, int uMsg, int idEvent, int dwTime); static public int StartTimer( AsyncGeoServer.GeoWrapper AsyncObj) { m_objGeoWrapper = AsyncObj; int lngReturn = SetTimer(0, 0, 1, new AsyncObjectCallerDelegate(AsyncObjectCaller));
The above calls are wrapped by a DLL by an external DoProcessing () method; this creates an event using CreateEvent before calling StartTimer (both calls to Windows Kernel) and then calling WaitForSingleObject before continuing with the processing. The AsyncObjectCaller function will set the event as part of its execution to continue processing.
So my problem is this: if the code is called as above, it fails. The AsyncObjectCaller callback method never starts, and the WaitForSingleObject call is interrupted.
If, however, I uncomment the call to MessageBox.Show in StartTimer, it works as expected ... sort of. The AsyncObjectCaller callback method starts immediately after the MessageBox.Show call. I tried to place MessageBox.Show in different places in the code, and this is the same no matter where I put it (as long as it called after SetTimer was called) - the callback function does not start until it appears message is displayed.
I am completely at a dead end, and none of them are familiar with VB6 or Windows API encoding, based on the main .Net background.
Thanks for any help!