Blond Mobile


Archive for the ‘C#’ Category

Windows Mobile, C#: How to stop a thread after calling CeRunAppAtEvent / WaitForSingleObject

Thursday, April 17th, 2008

In a previous post, I wrote about how to run a method when the device wakes up, or comes back from sleep mode. In that post I wrote about calling the CeRunAppAtEvent native function, which signals a given  event (hEvent) when the device wakes up.  I used the WaitForSingleObject(hEvent, INFINITE) function to wait until the hEvent is signaled.

The problem occurs when wanting to close the application, and a thread, which called the WaitForSingleObject()  is still waiting for the event. In that post, I wrote that in case wanting to close the application the below function should be called in order to terminate the waiting thread.

public static void Close()
{
    _close = true;
    if(_wakeupServiceThread != null)
       _wakeupServiceThread.Abort();
}

Unfortunately, The wakeupServiceThread could not be aborted, since it was waiting due to the call to WaitForSingleObject function.

The solution was to stop waiting on the WaitForSingleObject function,  and for that the _nativeWakupEvent event needed to be signaled.

Here is the code:

[DllImport("coredll.dll", SetLastError = true)]
        public static extern bool EventModify(IntPtr hEvent,
            [In, MarshalAs(UnmanagedType.U4)] int dEvent);
private const int NATIVE_EVENT_SET = 3;

public static void Close()
{
     _close = true;
     if (_hEvent != IntPtr.Zero)
             SetEvent(_hEvent);
}
private static bool SetEvent(IntPtr hEvent)
{
     return EventModify(hEvent, NATIVE_EVENT_SET);
}

Windows Mobile,C#: How to minimize your application/Send it to the background

Friday, April 11th, 2008

I wanted to send my application to the background. Simple enough, I just wrote Form.Hide() (or Form.visible = false). this did the job, my application was minimized and running at the background. At some point I wanted to use my application again, I clicked the applications icon, but nothing happend, I went to the running programs list in the settings, but my application was not there! my application was running in the background, and there was no way for me to close or access it!

Lucky enough, I used the Remote Process Viewer to find my application and kill it.

It seems that when Hiding a form (form.Hide()), The form is hidden from the user, and not only minimized. Funny that when clicking the “x” (which for some reason is called smart minimize), the application is minimized and sent to the background, where as using the hide() or visible=false, the application is hidden from the user and is not accessible.

The  Form class has a property which is called WindowsState. This property indicates the visual state of the form. unfortunately, the states that Windows Mobile/ .Net compact framework supports are ‘Normal’ and ‘Maximized’ and there is no support for the ‘Minimized’ state.

The solution for this problem is to call the Native function ShowWindow() with the ‘Minimized’ property:

[DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_MINIMIZED = 6;

        public void Hide()
        {
            ShowWindow(_form.Handle, SW_MINIMIZED);
        }

Hiding the application this way, will not “hide” the application from the user, but only minimize it and the application will be  “reachable” from the running programs list.

Windows Mobile: How to measure execution time in C#

Wednesday, March 12th, 2008

I wanted to test a function’s duration (for various  cases).

I thought it to be simple and be as in . Net C#.

I wrote something like this:

DateTime start = DateTime.Now;
//function code......

DateTime end = DateTime.Now;
TimeSpan duration = end - start;

I got that the duration is 0!! I also tried using

duration.TotalMilliseconds; and DateTime.Now.Ticks;

but the ticks for the start and end were the same. this seemed very strange.

To solve the problem, instead of using  DateTime.Now I used

System.Environment.TickCount;

It seems that the time resolution for DateTime.Now depends on the operation system, and for .Net compact framework its  one Second!!!.

 

Jajah is the VoIP player that brought you web-activated telephony.