Archiv der Kategorie ‘Mobile Development‘

Part II: Mimic SynchronizationContext behaviour on .NET CF

Sonntag, den 15. Februar 2009

I just posted the second part of the article Part I: Mimic SynchronizationContext behaviour on .NET CF on planetgeek.ch! On the article I try to show how the basic behavior of the SynchronizationContext can be achieved on the .NET compact framework platform. Please refer to the article under:

http://www.planetgeek.ch/2009/02/15/part-ii-mimic-synchronizationcontext-behaviour-on-net-cf/

Part I: Mimic SynchronizationContext behaviour on .NET CF

Sonntag, den 8. Februar 2009

Before I got into the details of the problem I want to briefly describe what the SynchronizationContext class really does and what it’s main purpose really is in the first part of the article. From that perspective I’m going to show how the basic functionality of the SynchronizationContext class can be implemented for the .NET compact framework in the second part of the article..

The msdn library documentation states:

Provides the basic functionality for propagating a synchronization context in various synchronization models.

I must admit the first time when I read this definition I didn’t really get the key point behind the SynchronizationContext class. Detailed look into the implementation of SynchronizationContext and its base classes provided me the following information:

The SynchronizationContext class is a class belonging to the System.Threading namespace. The SynchronizationContext provides a model to make the communication between threads easier and more robust especially if multiple threading contexts/apartments such as “UI threading context” etc. are present.

To get a deeper understanding of the definition above I want to give you a short example. Imagine if you have a separate thread performing an intense calculation such as calculating the n-th Fibonacci number. When the separate thread has finished its long running operation you want to display the n-th Fibonacci number on a user interface. Normally (without using the SynchronizationContext class) you would need to do the following (or at least something similar):

private delegate void FibonacciResultDelegate(long fibonacciResult);
 
private void MethodCalledByTheFibonacciThread(long fibonacciNumber)
{
   if( fibonacciResultTextBox.InvokeRequired)
   {
      FibonacciResultDelegate fibonacciDelegate =
         MethodCalledByTheFibonacciThread;
      fibonacciDelegate.Invoke(this, new object[] { fibonacciNumber });
      return;
   }
   fibonacciResultTextBox.Text = fibonacciNumber.ToString();
}


With the SynchronizationContext class we can invoke delegates in the context of a different thread. For the example above we could do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class FibonacciPresenter
    {
        private readonly Thread workerThread;
 
        private readonly SynchronizationContext context;
 
        private readonly IFibonacciView fibonacciView;
 
        public FibonacciWorker(IFibonacciView view)
        {
            fibonacciView = view;
 
            context = SynchronizationContext.Current;
 
            workerThread = new Thread(new ThreadStart(FibonacciCalc));
 
            workerThread.Start();
        }
 
        private void FibonacciCalc()
        {
            long result = CalculateFibonacciNumber( ... );
            context.Post(new SendOrPostCallback(delegate(object state)
            {
               fibonacciView.DisplayResult(result);
            }), null);
        }
 
        // details omitted...
    }

In code line 13 we can see how the SynchronizationContext is retrieved. The SynchronizationContex.Current property points to the SynchronizationContext of the thread where the FibonacciWorker was created (in my example the FibonacciWorker would be created in a control). Then the FibonacciCalc method can post (asynchronous) or send (synchronous) a SendOrPostCallback delegate containing the “job” which needs to be marshaled over the SynchronizationContext. Therefore the line fibonacciView.DisplayResult(result) would be invoked on the UI thread which allows us to remove the “invoke required” code parts and directly set the fibonacci calculation result to the textbox text property.

We can briefly summarize that the purpose of the SynchronizationContext is to post (asynchronous) or send (synchronous) SendOrPostCallback delegates in the correct threading context which simplifies marshaling.

Rumors about Windows Mobile 6.5

Mittwoch, den 21. Januar 2009

There are rumors going around on the internet about the release of windows mobile 6.5. Some say that windows mobile 6.5 is never going to be released but other strongly believe that the new windows mobile 6.5 will be released soon.

Not long ago people were blogging on the internet that Microsoft is probably going to focus on windows 7 and it’s integration into the newest mobile devices. But that has not been approved by Microsoft.

And today some screenshots about windows mobile 6.5 came up out of never on the following blog:

http://wmpoweruser.com/?p=2536

Nobody can approve if these screenshots are real or just another horrible fake. We are staying put and watching the latest news on this topic. We’ll keep you guys posted!

Delegate.DynamicInvoke for .NET Compact Framework

Sonntag, den 18. Januar 2009

As you might already know I’m a certified windows mobile application developer. My speciality is hybrid application development for applications which target both the full .NET framework platform and also the mobile platform. Of course nobody wants to write the same code for each platform again so you have to come up with some tricks and solutions to overcome some limitations on the compact framework.

One such limitation is the missing Delegate.DynamicInvoke method. The Delegate.DynamicInvoke method allows to dynamically invoke delegates late-bound. That means normally when you are invoking a method via a delegate you actually need to have knowledge about the target type where the delegate gets executed. With Delegate.DynamicInvoke this is not longer necessary. The beauty of this is, that you can have base code like the following:

// Elsewhere
RegisterDelegate(SomeClass.SomeMethod);
RegisterDelegate(SomeOtherClass.SomeOtherMethod);
FireForAllWith(1, 2, 3, 4);
 
// Code in some utility
public void FireForAllWith(params object[] args)
{
   someGenericCollection.ForEach(dlg => dlg.DynamicInvoke(args));
}
public void RegisterDelegate(Delegate dlg)
{
   someGenericCollection.Add(dlg);
}

But if you try to use Delegate.DynamicInvoke in the compact framework your infrastructure code will not compile because for some obscure reasons microsoft decided not to implement Delegate.DynamicInvoke for .NET compact framework. Here is my solution to this problem:

I created an extension method for the delegate class with the name DynamicInvoke. This extension method uses a small trick to implement the DynamicInvoke behaviour of the full framework platform.

    public static class DelegateExtensions
    {
        public static object DynamicInvoke(this Delegate dlg, params object[] args)
        {
            return dlg.Method.Invoke(dlg.Target, BindingFlags.Default, null, args, null);
        }
    }

As you can see I’m using the delegates method property which returns a MethodInfo object. On the MethodInfo I’m able to call Invoke and pass the arguments to the bound method. But the problem here is that Invoke requires a target where the method gets executed. This is where the delegates target property comes into play. That’s the whole magic and you’re able to dynamically invoke late bound methods via Delegate.DynamicInvoke.

Download DelegateExtensions for the source code.

Really useless stuff you can do with G-Sensor

Freitag, den 28. November 2008

First of all I must say I’m watching a movie and at the same time I’m writing a blog post on my computer. I really miss my multi monitor environment I have at my office ;) Back to the topic…

I just stumbled over two really cool programs you can install on your windows mobile device with G-Sensor. The first application is BeMario. BeMario is a cool implementation of the lovely Super Mario Game we all played till we had wound fingers. So you might asking yourself what is new with this game? The cool thing is that Mario only jumps in this game if you jump with your phone in your hands (or shake it accordingly) ;)

bemario

http://forum.xda-developers.com/showthread.php?p=2934494

The second one is the old game called spin-the-bottle. This romantic game should not be played with your work colleagues ;) Every shake with your phone will make the bottle spin. Hopefully the bottle will point to a nice cute person standing in front of you ;)

 http://blog.lieberlieber.com/2008/11/13/warum-programmieren-und-trinken-so-gut-zusammen-passt/

Android vs. Windows Mobile

Samstag, den 4. Oktober 2008

John Wang von HTC sagt, Android sei kein Konkurrenz-Betriebssystem sondern ziele auf im Gegensatz zu Windows Mobile weniger auf die Business-Features ab sondern sei flexibler in Sachen Applikations- und Gerätedesigns und würde eher auf die Usability fokusieren. HTC plant, weiter beide Betriebssysteme einzusetzen und Geräte dafür zu entwickeln.

Quelle: Digitimes.com (Englisch).

Das ist vor Allem interessant wenn man bedenkt, dass Microsoft weiter daran festhällt, von den Herstellern und den Mobilfunkanbietern Lizenzgebüren zu fordern (zwischen USD 8 und USD 15 pro Gerät). Wenn man dazu überlegt, dass für Hersteller keine Lizenzgebüren fällig werden für Symbian oder Android-OS.

Quelle: PocketPCthouchts.com

Managed Services für Windows Mobile

Donnerstag, den 12. Juni 2008

Sicherlich haben schon einige von euch die Möglichkeit Services unter Windows Mobile zu erstellen vermisst. Selbstverständlich war das bis anhin schon möglich aber nur über komplizierte PInvokes. Dies hat sich nun geändert! Peter Nowak hat auf Codeplex eine Library veröffentlicht, mit der es möglich ist, Managed Services für Windows Mobile zu erstellen! Die Library ist folgendermassen aufgebaut:

ManagedServiceClassDiagram

Mehr dazu unter:

http://www.codeplex.com/managedserviceswm

Die technischen Details dahinter:

http://bansky.net/blog/2008/04/services-for-windows-mobile-in-managed-code/

Schnelle Bitmapmanipulation unter Windows Mobile

Mittwoch, den 11. Juni 2008

Roshan Khan zeigt in seinem Blogartikel wie man unter Windows Mobile eine schnelle und effiziente Manipulation von Bitmaps durchführen kann. Obwohl die Klasse Bitmap sealed ist, zeigt er eine elegante Möglichkeit Bilder zu manipulieren.

http://blogs.msdn.com/windowsmobile/archive/2008/04/15/faster-c.aspx

Windows Mobile 6.1 unter Visual Studio emulieren

Montag, den 14. April 2008

Hier wieder einmal interessante News von der mobile Front.

Im Download Center von Microsoft stehen Emulator Images für Windows Mobile 6.1 zur Verfügung. Diese können entweder mit Visual Studio 2005 oder Visual Studio 2008 verwendet werden, um Applikationen zu testen, die für die neue Windows Mobile-Version erstellt wurden. Die Images können auch als Standalone-Version ohne Visual Studio benutzt werden. Bisher stehen lediglich englische Images zur Verfügung.

Quelle: http://it-republik.de/dotnet/news/Windows-Mobile-6.1-unter-Visual-Studio-emulieren-042647.html

Mobile Programming unter VS2008

Mittwoch, den 19. März 2008

Visual Studio 2008 hat den Support für das Programmieren auf mobilen Plattformen erheblich erweitert. Ich möchte hier aber nicht auf diese Feature eingehen sondern einen wichtigen Hinweis betreffend der Unterstützung von Visual Studio 2008 für mobile Programmierung machen. Neu ist nämlich nicht mehr Visual Studio 2008 Standard bereits in der Lage SmartDevice Projekte zu erstellen, sondern es ist die Professional Variante von Nöten! Also Achtung beim Kauf von Visual Studio 2008.