Smart Device Programming Chat

Thu, November 11, 2004, 12:58 PM under MobileAndEmbedded
Earlier today the Compact Framework chat of the month took place. For those of you that don't know about them, check out the schedule for online chats and search the archive of previous transcripts here. So these are opportunities to ask MSFTies and/or MVPs questions regarding a particular technology (in this case the CF).

Some times we may get interesting news out of them as well but, as always, nothing communicated in a chat is a commitment (more like "this is what we believe now"). Example: In a previous chat, I was told that the TreeView in CF 2.0 will support the NodeMouseClick event
"Q: Will CF 2.0 support TreeView.NodeMouseClick (since AfterSelect does not fire when selecting an already selected tree node)?
A: Yes, the NodeMouseClick event will be available in CF 2.0."

Today I was told it is not in CF 2.0 "for reasons of ROM size". Oh well, still if we all go and vote for it, I will feel better.

Other interesting points from today's chat:

Service Pack 3 for CF 1.0 will be out within a month (good news for me, as my next IQView release depends on a SP3 bug fix)

There will be no deterministic GC in CF 2.0 - it is on the consideration list for v3
Same goes for Code Access Security (not in CF2 but planned for future release).

It was confirmed that COM Interop in CF 2.0 is limited to managed clients activating unmanaged COM types. Unmanaged COM code can call managed code in terms of events/callbacks only.

That's all I recall. For the full story, keep an eye out for when the transcript becomes available.

Don't poll

Thu, November 11, 2004, 08:09 AM under dotNET
Sometimes we write code that uses polling. A brief example of that is some method/routine that runs on a timer (e.g. every 5 seconds), reads new entries from a file/database and performs some operation based on the input - and repeates this cycle until we stop the timer.

Although there are valid scenarios for taking the approach described above, there are others where it is a sub-optimal solution. For example, if you need to know as soon as there is a new entry to process, then there is no point waiting for your timer's tick; conversly, if there are no new items to process then there is no point running the function. A similar (equally not good) alternative is instead of running a timer to make the thread sleep for a while.

So in those case where the 2 aforementioned similar solutions are not the best, what is the correct way? Use a thread! The following code shows you an example of how. Note that I use this technique with the Compact Framework so, for example, we need the extra boolean to tell the thread to kill itself/exit since the CF 1.0 threads are missing many properties that could help (e.g. IsBackground, Abort etc).

        // declarations
        private Thread mSendNextThread;            // worker thread
        private bool mStayAlive;    // for controlling the thread termination
        private Queue mQue;                        // buffer of jobs
        private ManualResetEvent mWaitEvent;// signal the thread to wake up
 
        // Initialisation logic
        public void Start(){
            Console.WriteLine("*********");
            Console.WriteLine("Start");
            mQue = new Queue();
            mWaitEvent = new ManualResetEvent(false);
            mStayAlive = true;
            mSendNextThread = new Thread(new ThreadStart(OnMyOwnThread));
            mSendNextThread.Start();
        }
 
        // Tear down logic
        public void Stop(){
            Console.WriteLine("Stop request");
            lock (mQue.SyncRoot){
                mQue.Clear();
 
                // kill the thread
                mWaitEvent.Reset();
                Thread.Sleep(10);
                mStayAlive = false;
                mWaitEvent.Set();
                Thread.Sleep(10);
                mSendNextThread = null;
            }
            Console.WriteLine("Stopped");
        }
 
        // queue more jobs
        public void MoreInput(object someInput){
            Console.WriteLine("More Input: " + someInput.ToString());
            lock (mQue.SyncRoot){
                mQue.Enqueue(someInput);
            }
 
            mWaitEvent.Set();
        }
 
        // process jobs
        public void OnMyOwnThread(){
            while (mStayAlive){
                if (mWaitEvent.WaitOne()){
 
                    mWaitEvent.Reset();
                    while (mQue.Count > 0){    //Whether we do the looping depends
                                            //on the specifics of DoWork
                        if (!mStayAlive){
                            break;
                        }
 
                        this.DoWork2();
                        mWaitEvent.Reset();
                    }
                }
            }
            Console.WriteLine("Thread exiting");
        }
 
        private void DoWork2(){
            object queObj = null;
            lock (mQue.SyncRoot){
                if (mQue.Count > 0){
                    queObj = mQue.Dequeue();
                }
            }
 
            // do some long processing with this job/input
            if (queObj != null){
                Console.WriteLine("Processing: " + queObj.ToString());
                Thread.Sleep(300);//LONG PROCESSING
                Console.WriteLine("Processed " + Environment.TickCount.ToString());
            }
        }

CopySourceAsHtml

Wed, November 10, 2004, 09:29 PM under Links
Testing if this works in blogger

C# (btw, for the full MsgQueue API set look here)
        /// <summary>
        /// This function opens a handle to an existing message queue based on 
        /// a message queue handle.
        /// </summary>
        /// <param name="hSrcProc">[in] Handle to a source process that owns the 
        /// hMsgQ message queue handle. </param>
        /// <param name="hMsgQ">[in] Handle to a message queue returned by the 
        /// CreateMsgQueue function.</param>
        /// <param name="lpOptions">[in] Pointer to an MSGQUEUEOPTIONS structure 
        /// that sets the properties of the message queue.</param>
        /// <returns>Returns a handle to a message queue, or returns NULL if the 
        /// queue could not be opened.</returns>
        [DllImport("coredll.dll", SetLastError=true)]
        public static extern IntPtr OpenMsgQueue(IntPtr hSrcProc, IntPtr hMsgQ,    
            MsgQueueOptions lpOptions);


VB (btw, this is a another example of this)
    Private Function DoWork2() As Boolean
        ' If I don't have a requester then I can't do anything with the input 
        ' so ignore it (and don't obtain the lock!)
        Dim req As ForMyPending = Nothing
        req = Me.FindFreeRequester
        If req Is Nothing Then
            Return False
        End If
 
        ' Get the next object from the queue
        Dim queObj As ForMyQueue = Nothing
        SyncLock mQue.SyncRoot
            If mQue.Count > 0 Then
                queObj = DirectCast(mQue.Dequeue(), ForMyQueue)
            End If
        End SyncLock
 
        ' Deal with the next object from the queue without holding the lock!
        If Not queObj Is Nothing Then
            If queObj.ReqArgs.MaxNumResponses <> 0 Then'chance for client to cancel
                req.Deleg = queObj.Deleg
                If req.Requester.Request(queObj.ReqArgs, queObj.State) = False Then
                    If Not req.Deleg Is Nothing Then
                        req.Deleg.Invoke(Me, ReplyArgs.Empty)'failed
                        req.Deleg = Nothing'otherwise it wil never be reused again!
                    End If
                End If
            End If
        End If
 
        Return True
    End Function


Nick Cave Looking For a Job

Wed, November 10, 2004, 06:06 PM under Personal
Just came back from a Nick Cave concert. I am not some big fan but Jenny is crazy about his music so she dragged me up to Brixton and now I must thank her for a good night out!

Speaking of my girlfriend, she is looking for a ASP.NET/C# job in Sussex so let me know if you are interested. With 4 years development experience (Java/Delphi) and having just completed an MSc (7 .NET projects), I'd hire her! Here is a public "congratulations" for getting a distinction on your MSc project!

Blog link of the week 45

Sun, November 7, 2004, 02:42 PM under Links
Windows CE 5.0 - Get the Facts, by Mike Hall

While on the subject of CE, a bunch of QFEs for all versions were published this week; if you want to know when MSFT downloads are available just subscribe to their rss feed (e.g. then you'd know that this week a new .NET CF sample became available also)

My.IsNot.For.Me

Thu, November 4, 2004, 03:59 PM under Whidbey | VisualStudio
If you are not familiar with the new My feature of VB2005, please go read about it and make your own mind up. I suggest reading my previous entry (in addition to the two links I offer there also check these out [1,2])

So it is clear that we are talking about:
1. A speed dial into the framework
2. Application events
3. Stuff generated at compile time into your project

My opinion is that the first one is a waste of space. Speed dial?! They could have given us this as example code and be done with it (or invest the energy to improving the help/navigation system). Yes the framework is large and some times discovering some methods may take longer than what it should, but how does having a parallel framework help? Now I have two frameworks to learn! We don't need YAF (yet another framework). The process of discovering the framework is called "becoming a .NET developer". While searching for a solution to a problem, the developer discovers not only the correct namespace/class/method but also a bunch of other classes/methods that they will not have to search for next time they have a different problem. The My feature discourages this behaviour and hence hinders learning the framework (which can only be a bad thing).

I wasn't too thrilled with VB.NET 2002 when I discovered all those "global" functions that come as part of Microsotf.VisualBasic.dll However, there is a major difference with those: they are necessary for smooth upgrade of VB6 code by tools (and some parts of it actually offer functionality not found in the framework). VB2005 takes the VB6 approach of having global functions everywhere and repackages them in a structured library. Structured or not, it is still a bunch of globals that existing .NET devs did not need (globals=statics.. err sorry I meant shared). In case it is not clear yet, My is a bunch of stateless operations that offer functionality already available in the framework. It is obvious the feature was added to lure VB6 devs that have not yet moved to .NET, but is My really helping them in the long run?

Application events as a concept are a nice idea. Will I use them in VB? Unfortunately the answer is no. Not only I am not too fond of having to give up my own Sub Main, but more importantly they will not be supported in CF 2.0

I haven't made my mind up on whether I like the 3rd part of My. On the face of it, it seems dirty, but only time will tell.

To finish off with a sweeping comment on the whole of the My feature: it is nowhere near in the same league as refactoring (that will not be part of Whidbey) and I would like to talk to the person that was responsible for prioritising these features (not that it will happen, but it doesn't hurt to ask:-).

I had written the above and then came across a post with a similar ending to mine.

More Windows Mobile 2005 Details Emerge

Wed, November 3, 2004, 12:29 PM under MobileAndEmbedded
If you are PocketPC enthusiast you might be interested in Windows Mobile 2005 (codename magneto).

There are some details about it here [found the link via this (Greek) site]

I will not VOTE!

Tue, November 2, 2004, 12:53 PM under Personal
...because I am not an American and I live in the UK anyway! Sorry couldn't resist this entry given all the encouragement to vote from our American blogging friends :-)

May the best man win.

In other news the score is currently Arsenal 1-0 Panathinaikos. Hmmm, should I support the rivals of Manchester United or one of the rivals of PAOK? I'll have to go with the Greek team I guess...

Normal service will resume soon

Blog link of the week 44

Sun, October 31, 2004, 03:26 PM under Links
David Notario (x86 JIT developer) kicks off his blog with two great posts.

Welcome

Sun, October 31, 2004, 07:26 AM under Blogging
This blog has moved to this new home (http://www.danielmoth.com/Blog/) from its previous place (zen13120.zen.co.uk).

If you are reading this in your browser, welcome!

If you are reading this in your newsreader, welcome and thanks for updating your subscription (or for having subscribed to feed://feeds.feedburner.com/DanielMoth)