Cyberiafreak

"Fortunate are those who take the first steps.” ― Paulo Coelho

What is DispatcherTimer in WPF?

For WPF applications,
there is a new type of timer that utilizes the Dispatcher—the DispatcherTimer class. Like other timers, the DispatcherTimer class supports specifying an interval between ticks as well as the code to run when the timer’s event fires. You can see a fairly pedestrian use of the DispatcherTimer.
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(2000); 
_timer.Tick += new EventHandler(delegate(object s, EventArgs a) 
{ 
    statusText.Text = string.Format(
        "Timer Ticked:  {0}ms", Environment.TickCount); 
});
_timer.Start();
src: http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

 


Advertisements

July 13, 2011 Posted by | Uncategorized | Leave a comment

Learning Part 1-Memory/Lock tips.

  1. DispatcherTimer issue:  http://geekswithblogs.net/dotnetrodent/archive/2009/11/05/136015.aspx
  2. Memory leaked caused by event handler http://blogs.msdn.com/b/abhinaba/archive/2009/05/05/memory-leak-via-event-handlers.aspx
  3. Lock usage(based on common misuses found in smart code) From MSDN: http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.80).aspx
  4. lock calls Enter at the beginning of the block and Exit at the end of the block.In general, avoid locking on a public type, or instances beyond your code’s control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:
  • lock (this) is a problem if the instance can be accessed publicly. (A few places in smart still does this.)
  • lock (typeof (MyType)) is a problem if MyType is publicly accessible.
  • lock(myLock) is a problem since any other code in the process using the same string, will share the same lock.

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.

  1. Don’t use lock if only one thread is involved.

a)         Don’t lock on code run only on one UI thread. (use a Boolean flag to prevent re-entry if that is the intention)

  1. Know what you are try to lock, in other word, keep the lock scope as tight as possible.

a)         Lock a large code block (or a method that does a lot is a problem, even worse, if the method uses pushframe that waits for messages)

b)         Don’t try to use lock to synchronize action/method, use thread safe producer-consumer queue.(a live example is Dispatcher in the UI thread).

(Few learnings from my team, I’m not the original author.. credit goes to my fellow teammate. thkx)

July 11, 2011 Posted by | Uncategorized | Leave a comment

URLDownloadToCacheFile failed with HRESULT ‘-2146697208’

One of my team mate faced this issue and not able to install Smart Clickonce coz of this err. On searching i found this information somewhere in the 3’rd page of the search.. damm it.
http://support.microsoft.com/kb/934387

July 7, 2011 Posted by | Uncategorized | | Leave a comment

   

%d bloggers like this: