Sunday, January 15, 2012

Print to Kindle

It is an odd announcement that causes me to hijack my daughters Kindle Fire and Laptop. But I really wanted a sense of how this felt. After downloading the Send to Kindle desktop application onto the laptop and configuring it to use her Amazon account a print option 'Send to Kindle" appears in most applications. The most interesting place is in Word.

In many ways this functions in a manner similar to 'Save as PDF'. The format of what is printed is converted to a format that Kindle can display. The next step is the file is forwarded to Amazons cloud and then forwarded on to the kindle. But this all happens without the user comprehending or needing to comprehending how it occurs.

The result is the document appears on her Kindle. Which means she is now in a position to develop content for the Kindle with minimal effort. Furthermore, this will expand. The end goal is her Kindle becomes the most import device. And it has taken on the role of presenter of her cloud based Amazon account which starts to position itself as the ITunes of information stores.

Friday, January 13, 2012

Winform Background Thread


The easiest way to implement a background worker thread for a form in a Winform application is to use the BackgroundWorker from the System.ComponentModel namespace.




To summarize the changes imagine you have a form were you dropped a button with the default name 'button1'.


  1. From the toolbox open the component section.

     
  2. Select BackgroundWorker and drag it onto the form.
    1. The default name will be 'backgroundWorker1'.
       
  3. Select the backgroundWorker object and inspect its properties.
    1. Select the Events (it looks like a lightning bolt)
    2. Doubleclick in the space next to 'DoWork'
      1. Keep default name: backgroundWorker1_DoWork
    3. Doubleclick in the space next to 'ProgressChanged'
      1. Keep default name: backgroundWorker1_ProgressChanged
    4. Doubleclick in the space next to 'RunWorkerCompleted'
      1. backgroundWorker1_RunWorkerCompleted

         
  4. Button Click Code Handler
     
    private void button1_Click(object sender, EventArgs e)
    {
    this.backgroundWorker1.RunWorkerAsync("From Form");
    button1.Enabled = false;
    }
     
  5. Background DoWork Handler
     
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
    Thread.Sleep(1000);
    string comment = (string) e.Argument;
    backgroundWorker1.ReportProgress(12, comment + " From Background");
    Thread.Sleep(10000);
    }

     
  6. ProgressChanged Handler


    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
    string report = (string)e.UserState;
    listBox1.Items.Add("From Progress Change Hander");
    listBox1.Items.Add(report);
    }
     
  7. RunWorkerCompleted Handler

     
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    button1.Enabled = true;
    }

     




Implementation Versions: This code was developed and tested with Visual Studio 2010, C# 4.0 and the .Net Framework 4 Client Profile.

Tuesday, April 27, 2010

Is Boho a Windows Phone 7?

The short answer is no. If for no other reason than Silverlight is absent.
But it is also lacking in the minimal feature set which specifies minimum screen, camera and memory.

Saturday, April 24, 2010

Race Condition as Illustrated with Racer X and Speed Racer

Sometimes nothing works better than a simple example.

Below is a short program that demonstrates a race condition that occurs when two threads share some bit of state. It is written in C# and if you run it several times you should see inconsistent behaviors.

Sometimes Speed Racers thread finishes first and resets the SharedState to true and sometimes Racer X wins. This is the heart of threading problems, shared state and inconsistent results.

Now lets looks at a proposed fix. In RacerX there is a commented line
//System.Threading.Thread.Sleep(100);

By uncommenting it,  the inconsistent behaviour changes. Sometimes the program finishes because RacerX finished first and waited long enough for Speed Racer to finish. (You know he was the older brother and always looking out for Speed Racer)

But sometimes Speed Racer finishes first and he doesn't wait for RacerX. So Racer X keeps circling around. (Keep in mind Speed did not know that Racer X was his older brother)

So in this case it appears that the code has gotten better because the bug is now showing only half the time. But in reality it is just as flaky. The bug has now become intermittant and harder to reproduce.


class Program
{
static bool SharedState = true;

static void Main(string[] args)
{
Thread racerX = new Thread(RacerX);
racerX.Start();
Thread speedRacer = new Thread(SpeedRacer);
speedRacer.Start();
System.Threading.Thread.Sleep(2000);
SharedState = false;
}


static void RacerX()
{
while (SharedState)
{
Console.WriteLine("RacerX");
}
//System.Threading.Thread.Sleep(100);
SharedState = true;
}


static void SpeedRacer()
{
while (SharedState)
{
Console.WriteLine("SpeedRacer");
}
SharedState = true;
}
}