Archive for February, 2010

Multithreading for the future

Sunday, February 28th, 2010

I haven’t talked about multithreading in some time but it’s even more important now than in the past.  Even some laptops are coming with quad cores now so you really need to be able to take advantage of them.  If you do nothing, then a computer will probably still see some better performance since several single-threaded applications can run concurrently, but if your application freezes up when it attempts to download a file or perform calculations then you’re going to look bad.

.NET 4 introduces some great parallel features.  You can create tasks that perform work and provide relatively easy synchronization.  They are lighter than threads since they are pooled and the API is easy.  As an abstraction over tasks is Parallel LINQ extensions.  You can do some amazing stuff by just adding .AsParallel() to a collection in your query.  This signifies that the collection can be split across threads, with the results joined back together at the end.  It’s super easy to do in most cases, and really provides a noticeable benefit.  If you need to provide hints for better performance then you can, but in many cases the defaults will do just fine.

Just working with existing libraries to manipulate data across multiple threads is useful though too, as plenty of legacy code won’t have access to new extensions. Understanding and using semaphors, locks, mutexes, wait handles, and even timers is invaluable.  Learn to synchronize code.  You should be able to start some work, and then notify another part of your code when it should proceed.

You need to really create a solid separation of data state from presentation – in other words, your UI should query the data.  State display needs to be a snapshot-in-time, not based on showing on-demand data from events.  You can either periodically check your state and update the UI, or take change events and dispatch them to a UI thread (as a window message) for deferred updates.  Once the window message is processed, you’ll be on the right thread and can show the new state, recent log messages, or updated status.

When a button is clicked, the event handler needs to do as little as possible.  Never perform an actual action directly from the handler.  Rather, start a background thread or task to do some work.  When the work completes, it can notify the UI, which in turn can be dispatched to the message queue.  Finally, the results can be displayed without ever causing the user to wait.

I use Picasa for photo management.  The programmers made a bad assumption that file access would be instantaneous.  You can never assume zero latency!  Even opening a file can freeze the UI when performed in an event handler.  How?  Imagine if you assume that a file is 120kb (and your testing reflects this) but the user is opening an uncompressed 25MB file.  What if the files are located on a network share?  Assumptions will always come back to bite you.  If you just assume that everything will be worst case and design your UI around that, then you’ll be fine.  As soon as you decide that a “normal” environment is close to your own, then you’ll make bad choices and performance will suffer.

Passionate programmers

Saturday, February 27th, 2010

Do you like to program?  I mean really like to program?  Apparently you aren’t in the minority if you said “no” to that question.  I got into an interesting discussion about hiring good programmers last week and the consensus was that the best programmers were the ones with a passion for it.  This isn’t all that revolutionary to those of us who are passionate about it, but perhaps you disagree.  Isn’t programming just about converting needs into code?  It seems as dry as materials engineering on the surface (no offense to materials engineers out there), and yet discussions about languages, algorithms, and design patterns bring about something akin to a holy war.  Somehow I doubt that materials engineers end up flaming each other over the general advantages of various alloys – independent of any specific application!

It’s always been amazing to me how polarizing computer science issues can become.  Though everyone agrees (in a general sense anyway) that the tools and language are dependent on the scenario, once you start talking specifics, no one can agree on anything!  Though this isn’t productive most of the time, it does certainly demonstrate a passion unmatched in many fields.  People who are really into programming get really passionate about every detail.  Conversely, people who tend to code as a job and nothing more don’t care about the details – they are just tools to get the work done.

Why is one programmer “better” than another?  Maybe that’s too strong a way to phrase it.  Why would I be more interested in hiring the passionate developer?  Well, even though I might need to face heated discussions about design and implementation from time to time, I would know that the person was putting a lot into the effort.  Hours spent at home learning new technologies, time spent at work to eek out every bit of performance and efficiency, and the attitude that the final work reflects personally on the code all mean that a passionate developer is more likely to create good code.  On the other hand, I’ve worked with developers who just like to argue because their knowledge base is narrow and they want to keep designs in their comfort zone.  We need to eliminate this too!

Thus, the programmers that I would look for would be passionate about the field, and their passion would lead them to explore new features and tools to provide more options.  A passionate programmer will consider coding more than just a job and will keep up to date as technology changes.  Does this mean that “day job” programmers can’t write great software, or that passionate programmers don’t have a life?  Not at all.  They aren’t mutually exclusive attributes.  Just like any field though, it’s passion that usually leads to the best solutions.

Windows 7 Resource Monitor (Not as boring as it sounds)

Friday, February 26th, 2010

Windows Vista introduced GenOne of a tool called the Reliability and Performance Monitor where you could see where your system resources were being used in realtime. This was cool because it was the first time you could access a list of all the processes using the CPU, all of the processes using Memory, all processes actively reading or writing to the hard disk, and all processess actively using network bandwidth. As good as this is for Windows Vista, interaction with this information was not available.

In Windows 7, this key functionality has been moved to a tool called Resource Monitor. Resoruce Monitor now has five tabs running across the top off the window:

  • Overview: Provides information on CPU, Memory, Disk, and Network usage.
  • CPU: Provides information on Processes, Services, Associated Handles, and Associated Modules.
  • Memory: Provides information on Processes and Physical Memory utilization.
  • Disk: Provides information on Processes with Disk Activity, Disk Activity, and Storage overview.
  • Network: Provides information on Processes with Network Activity, Network Activity overview, TCP Connections and Listening Ports.

So these tabs provide a lot more information for the administrator to research what is actually happening on the computer. However, the best part of this new tool is that you can actually filter individual processes and interact with them. For example, if you just want to konw what is happening with Microsoft Office Outlook, you can select that process and Resource Monitor will just show the disk activity, network activity, and memory utilization for OUTLOOK.EXE, and that is just when looking at the Overview tab. Switching to the CPU tab, you can review the list of Associated Handles. Switching to the Disk tab, you can review the files that are currently being used by Outlook. Switching to the Network tab, you review the remote address that Outlook is communicating with, the TCP port usage, Packet Loss, and network latency.

Two more things. In Resource Monitor, you can now right-click a process for a list of tasks such as ending the process, suspending the process, or searching online for more information on the process (read: find out whether it is legit or malware).

There is an option on this context menu to Analyze Wait Chain. Wait Chain Traversal (WCT) enables debuggers to diagnose application hangs and deadlocks. A wait chain is an alternating sequence of threads and synchronization objects; each thread waits for the object that follows it, which is owned by the subsequent thread in the chain. So with advanced knowledge of how the application works, you can troubleshoot application issues by using this feature.

Using Windows PowerShell as an IT Pro – Part 10

Friday, February 26th, 2010

In my last post I reviewed some the format cmdlets. Now I will play with the Out-Gridview cmdlet.
Windows PowerShell 2.0 provides a few new tools that help to treat you more like a human than a machine. For example, by using the Out-Gridview cmdlet after retrieving a list of items, you can have the output is pipelined to a separate window (Graphic 24).
Get-Service | Out-Gridview


From this window, the list can be sorted by clicking the column header. However, this is output from the command, not the objects themselves, preventing any manipulation directly from this window. For example, you cannot right-click an item here to get a context menu to appear in order to stop or start a service.

The PowerShell grid also includes a rudimentary filtering function, showing only those services whose returned data includes a specified keyword. Simply type that keyword in the text box labeled Search. After entering the keyword, the grid filters out any items that do not include that keyword in at least one of the columns.

If you want to limit the filter to a specific column, type the column name followed by a colon, and then the filter string, reducing the results to four services on the computer in this example.

In my next post we get into variables and types.

Using Windows PowerShell as an IT Pro – Part 8

Friday, February 26th, 2010

In my last post I talked about the format cmdlets. Now I will continue to examine the format cmdlets.
Using the Format-Table cmdlet with no property names specified to format the output of the Get-Process command, provides exactly the same output returned without performing any formatting. The reason is that processes are usually displayed in a tabular format, as are most Windows PowerShell objects.
The displayed properties can be controlled by any of the format cmdlets by leveraging the property parameter. In this example, the format of the table displays with three specific properties.
Get-Service m* | Format-Table -Property Status, Name, ServiceType

Format04

Before I show an example of a more advanced formatting command, let’s look at some sorting. The Sort-Object cmdlet sorts objects in ascending or descending order based on the values of properties of the object.
You can specify a single property or multiple properties (for a multi-key sort) and you can select a case-sensitive or case-insensitive sort. You can also direct Sort-Object to display only the objects with a unique value for a particular property. This command displays a sort of services by status.
Get-Service m* | Sort-Object Status

Format05

You can also perform advanced formatting by combining formatting commands and adding some grouping. This command sorts the service list by status, before passing the output to the format-table command, which groups the output by status. In the displayed output the stopped and running services are grouped together, and under each status is a table containing the name and display name of each service.
Get-Service m* | Sort-Object Status | Format-Table –Group Status -Property Name, DisplayName

Format06

In my next post we will look at the Output commands.

Using Windows PowerShell as an IT Pro – Part 9

Friday, February 26th, 2010

In my last post I wrapped up the format cmdlets. Now I will take a look at the output cmdlets.
Windows PowerShell provides several cmdlets that let you control data output directly. These cmdlets share two important characteristics.
First, they generally transform data to some form of text. They do this because they output the data to system components that require text input. This means they need to represent the objects as text. Therefore, the text is formatted as you see it in the Windows PowerShell console window.
Second, these cmdlets use the Windows PowerShell verb Out because they send information out from Windows PowerShell to somewhere else. The Out-Host cmdlet is no exception: the host window display is outside of Windows PowerShell. This is important because when data is sent out of Windows PowerShell, it is actually removed. Because of this, any output cmdlets needs to be put at the end of the pipeline, otherwise the data is sent out of Windows PowerShell and any subsequent commands will have nothing to process.
The Output cmdlets are Out-Default, Out-File, Out-GridView, Out-Host, Out-Null, Out-Printer, and Out-String.
Output can be sent to a file by using the Out-File cmdlet. The following command line sends a list of services to the file C:\temp\serviceslist.txt:
Get-Service | Out-File -FilePath C:\temp\serviceslist.txt

The results of using the Out-File cmdlet may not be what you expect if you are used to traditional output redirection. You must be aware of the context in which the Out-File cmdlet operates to understand its behavior.
By default, the Out-File cmdlet creates a Unicode file. This is the best default in the long run, but it means that tools that expect ASCII files will not work correctly with the default output format. The Out-file formats file contents to look like console output, causing the output to be truncated just as it is in a console window in most circumstances.
The Out-Null cmdlet is designed to immediately discard any input it receives, which is useful for discarding unnecessary data obtained as a side-effect of running a command. When entering the following command (Graphic 22), the command results stating that the directory was created are not delivered.
New-Item -Path C:\ -Name test -Type Directory | Out-Null

The Out-Printer cmdlet routes the output to the specified printer. The Out-String cmdlet converts the objects that Windows PowerShell manages into an array of strings. By default, Out-String accumulates the strings and returns them as a single string, but you can use the stream parameter to direct Out-String to return one string at a time. This cmdlet lets you search and manipulate string output as you would in traditional shells when object manipulation is less convenient.

In my next post we will look at the Out-Gridview cmdlet.

SQL Azure CTP is Over

Thursday, February 25th, 2010

I hadn’t used my SQL Azure is several weeks and was surprised when I was unable to add new databases and play around anymore. I had received some e-mails regarding SQL Azure but hadn’t bothered to read them yet. Afraid I would have to pay to continue to use my account, I looked at the options to continue with SQL Azure. I was pleased to find out there is a special introductory offer available on the Microsoft Online Customer Portal. This offer allows you to use SQL Azure free up to a certain usage each month. If you go over that amount, you only pay for the amount of time you use it past the free usage amount. This is great because I can continue to try out SQL Azure without putting a lot of money into it right now.

Windows Live Error 800706be – Service Unavailable

Tuesday, February 23rd, 2010

The other night I turned on my computer and when Windows Live tried to sign in, I got an error that the service was unavailable. To me, that sounded like Windows Live was offline for maintenance or something along those lines, so I figured I’d give it some time and try again later.

The next day I was surprised to see that I was still getting this message when I tried to log in. I checked with a friend and he was online, so my “down for maintenance” theory no longer seemed like a valid consideration.

I looked more closely at the error code: 800706be and did some standard Internet searching. Turns out that this highly descriptive and not-at-all-nebulous error code can apply to any number of problems, with solutions ranging from reinstalling/repairing Messenger, cleaning the registry, or just crossing your fingers and waiting a little while.

After a while, I finally stumbled across the fix. Evidently my Windows Live contacts cache had somehow gotten corrupted and the answer was to delete it and allow Windows Live Messenger to recreate it.

In Windows Explorer, I browsed to %USERPROFILE%\AppData\Local\Microsoft\Windows Live Contacts and then delete all subfolders. When I launched Messenger, I was able to log in again.

What’s new in Excel 2010

Tuesday, February 23rd, 2010

Last month I started looking into the new features of Office 2010, starting with Microsoft Word 2010. This month I’m going to take a look at Excel, Outlook, and PowerPoint.

Let’s start by taking a look at just a few of the new features and improvements in Excel 2010.

New templates save you time by letting choose from a format that contains common fields.

A new feature called Slicer lets you filter and drill-down so you can more easily find the specific information you need.

Sparklines is a new charting feature that allows you to create charges and graphs in a single cell, giving you a quick way to spot trends and patterns.

The Backstage view improves on the File menu from previous versions, allowing multiple ways to save, share, and even publish your spreadsheets to SharePoint.

The PowerPivot for Excel add-in helps you model and analyze large amounts of data. With this, you can combine data from multiple sources, including databases, other spreadsheets, and so on.

Excel 2010 offers improved performance by taking advantage of 64-bit processing and multithreading.

All in all, the visual features of Excel are better looking, more powerful, and more easily integrated, and Excel 2010 makes it easier than ever to access your data, by using Excel Web App or Excel Mobile 2010.

Vshost.exe has stopped working

Monday, February 15th, 2010

While working with the Microsoft Sync Framework, I kept coming across an error while debugging an application that syncs data from a server data source to a local cache in a SQL Server Compact database. I was using the Sync Designer that ships with Visual Studio.NET 2008. I started by creating a Windows Form Application, then when I went to debug the sync object, I received the “vshost.exe has stopped working” error in the image below.

Next, I tried to start the application without debugging. This time I got a different error: “Unable to load DLL ‘sqlceme35.dll’: The specified module could not be found.”

I found a solution that resolved both of these errors. You need to set the CPU to x86 in the debugging menu. Next to the Debug button on the toolbar, there is a drop-down box that lists Any CPU.

After changing this setting to x86, the debugging can be completed without errors.