Threading
Threading

Improving Thumbnailing Code

From the prior post about getting thumbnails from a video in .Net, it was just prototype code and wasn’t properly abstracted.  Now it is time to fix it.  We’ll create a class called VideoScreenShot.  This class will function in both an asynchronous and synchronous mode.  This still could be improved by queuing up work but this is a nice refactoring. To get access to the tester app, head over to codeplex and source code can be found there too! public delegate void CaptureWorkerDelegate(BitmapFrame frame, object state); public static void CaptureScreenAsync(Uri source, TimeSpan timeSpan, object state, CaptureWorkerDelegate finalWorkerPrimary) { CaptureScreenAsync(source, timeSpan, -1, state, finalWorkerPrimary, null); } public static void...

Getting Thumbnails in WPF … on a non-UI thread

On my internal application for Channel 9, I have to create thumbnails at certain time codes in a movie.  After a giant headache attempting to track down the proper way of doing this, I figured out how to do this even if for a few hours I thought 127 seconds was the same at 1 minute and 27 seconds.  That caused some testing headaches as my test clip is 1:40 long. In this example I’ll write out the code for a threaded video thumbnail creating tool in WPF (XAML) and c#. To get access to the tester app, head over to codeplex and...

Prime Number smack down.

Alfred Thompson and I tend to have “friendly” talks about programming and for a while we’ve been attempting to figure out who was the better coder.  Someone gave us the challenge of find quickest way to figure out the first prime number after 2.2 billion.  Turns out even if you’re on a cruise ship filled with PhD’s, seemed like no one knew of a better way off the top of their head other than brute force.  Alfred used VB.Net, I used c#.  Alfred’s VB.Net prime number solution is posted over at his blog on MSDN.  Alfred’s solution and my should...

It is the generic brand for multi-threading

In the prior post, I talked about getting the system thread safe.  I didn’t make the methods generic as that would have added in more complexity and I just wanted it to work. Now as all good coders know, cutting and copying code is the work of vegans and hippies and should be stopped.  So how should we make it reusable rather than a suburb where everything is a slightly tweaked copy of one another. So lets see how I’d call the Reset method. I’ll need 3 things The public method ...

Thread-Safe bartender

So, I could have done a simple comport.ReadLine() with a while loop and walked away.  As I enjoy over-engineering something that could be done in 5 lines of code, I did it in 50.  But why, why god why did I do it in 40 lines?  To make it thread safe and event driven! So lets look at the code. private string _fwVersion; private readonly Mutex mutex = new Mutex(); private AutoResetEvent firmwareAreState; public string GetFirmwareVersion() { mutex.WaitOne(); comport.DataReceived += comport_DataReceivedFirmware; _fwVersion = string.Empty; firmwareAreState = new AutoResetEvent(false); for (var i = 0; i < 5; i++) { if (!string.IsNullOrEmpty(_fwVersion)) break; comport.Write(firmwareVersion); firmwareAreState.WaitOne(500); } if (string.IsNullOrEmpty(_fwVersion)) _fwVersion = "ERROR"; comport.DataReceived -= comport_DataReceivedFirmware; mutex.ReleaseMutex(); return _fwVersion; } private void comport_DataReceivedFirmware(object...