LessThanDot Site Logo

LessThanDot

A decade of helpful technical content

This is an archive of the posts published to LessThanDot from 2008 to 2018, over a decade of useful content. While we're no longer adding new content, we still receive a lot of visitors and wanted to make sure the content didn't disappear forever.

Why do we need error handling?

Why do we need error handling? Error handling is something that is often forgot or misunderstood or put off until something goes awry and we try to back peddle and retro fit the error handling logic in the script, stored procedure or function. I think there are quite a few reasons we should engage in handling something other than the happy path of logic. I’ve listed a few here and I’m sure you can think of some more.

Read More...

Parallel.For and a difference between C# and VB.Net

While writing my last 2 blogposts I noticed that the Parallel.For was kinda better in [C#][1] than the [VB.Net][2] version. This is what I had in VB.Net. Edit Parallel.For(0, 5, Sub(b) CheckOnline(b) End Sub)``` Which would be this in C# ```csharp Parallel.For(0, 5, b =&gt; {CheckOnline(b)});``` or the slightly shorter versions. ```vbnet Parallel.For(0, 5, Sub(b) CheckOnline(b))``` Which would be this in C# ```csharp Parallel.For(0, 5, b =&gt; CheckOnline(b));``` <span class="MT_red">End Edit.</span> or the even shorter version in C# first. ```csharp Parallel.For(0, 5, CheckOnline);``` Apparently it is able to infer the parameter to send in C# (see no more b just the methodname. Resharper told me that. So I searched for a way to do this in VB.Net and here it is. ```vbnet Parallel.For(0, 5, AddressOf CheckOnline)``` A little more verbose but just a little. I learn a little every day. [1]: /index.php/DesktopDev/MSTech/multithreaded-ping-shown-in-a [2]: /index.php/DesktopDev/MSTech/multithreading-pings-and-showing-them

Read More...

Multithreaded ping shown in a grid C# version

Since I don’t want my dear friend Ted to do this himself I converted the code in my previous post into C#. I also wrapped the ping in a using statement and now only swallow InvalidOperationException because catching Exception is not good. using System; using System.Collections.Generic; using System.Drawing; using System.Net.NetworkInformation; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace mutlithreadedping { public partial class Form1 : Form { private List&lt;int&gt; _success = new List&lt;int&gt;(); private List&lt;int&gt; _done = new List&lt;int&gt;(); private Thread _fThread; private Thread _fThread2; private delegate void AddRowDelegate(int column); private delegate void SetOnlineDelegate(int rowindex); private void Form1_Load(object sender, EventArgs e) { fGrid.Columns.Add("Ip", "Ip"); fGrid.Columns.Add("Ping", "Ping"); fGrid.Columns[0].Width = 100; fGrid.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; for (var i = 0; i &lt;= 4; i++) { var myRowIndex = fGrid.Rows.Add(); fGrid.Rows[myRowIndex].Cells[0].Value = "10.216.110." + (11 + myRowIndex); } } private void ThreadProc() { Parallel.For(0, 5, b =&gt; { while (!_done.Contains(b)) { fGrid.Invoke(new AddRowDelegate(AddRow), new Object[] { b }); Thread.Sleep(300); } fGrid.Invoke(new SetOnlineDelegate(SetOnline), new Object[] { b }); }); } private void AddRow(int rowindex) { if (fGrid.Rows[rowindex].Cells[1].Value == null || fGrid.Rows[rowindex].Cells[1].Value.ToString().Contains(".....") || !fGrid.Rows[rowindex].Cells[1].Value.ToString().Contains("Pinging")) { fGrid.Rows[rowindex].Cells[1].Value = "Pinging 10.216.110." + (11 + rowindex) + " "; } else { fGrid.Rows[rowindex].Cells[1].Value = fGrid.Rows[rowindex].Cells[1].Value + "."; } fGrid.Rows[rowindex].Cells[1].Style.BackColor = Color.White; } private void ThreadProc2() { Parallel.For(0, 5, CheckOnline); } private void CheckOnline(int rowindex) { using (var ping = new Ping()) { try { var pingreply = ping.Send("10.216.110." + (11 + rowindex), 2000); if (pingreply == null || pingreply.Status == IPStatus.Success) { lock (_success) { _success.Add(rowindex); } } } catch (InvalidOperationException) { // consider it done but no success } lock(_done) {_done.Add(rowindex);} } } private void SetOnline(int rowindex) { if (!_success.Contains(rowindex)) { fGrid.Rows[rowindex].Cells[1].Value = "Offline"; fGrid.Rows[rowindex].Cells[1].Style.BackColor = Color.Red; } else { fGrid.Rows[rowindex].Cells[1].Value = "Online"; fGrid.Rows[rowindex].Cells[1].Style.BackColor = Color.Green; } } private void pingToolStripMenuItem_Click(object sender, EventArgs e) { _done = new List&lt;int&gt;(); _success = new List&lt;int&gt;(); _fThread = new Thread(ThreadProc) {IsBackground = true}; _fThread.Start(); _fThread2 = new Thread(ThreadProc2) {IsBackground = true}; _fThread2.Start(); } } }

Read More...

Multithreading pings and showing them in a grid in VB.Net

Today I wanted a way to see which of my cameras were online at a certain point in time. Pinging them is a great way of doing that, it at least says they are there and the first two layers of the OSI model are working (if I remember correctly). Doing a ping to another computer in VB.Net is easy. Dim _ping As New Ping Dim _pingreply = _ping.Send(IpAddress, 2000) If _pingreply.Status = IPStatus.Success Then Like this.``` The first parameter of Ping.Send is the IpAddress as string and the second is the timeout in this case 2 seconds. I want to ping 5 cameras at a time to worst case this will take 10 seconds. 10 seconds is a long time to wait for a user who&#8217;s attention span is 3 seconds. So we need a way to do this asynchronously. So I create a grid with 2 columns and 5 rows. First column has the Ipaddresses and the second will show the result. Here is how. ```vbnet Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load fGrid.Columns.Add("Ip", "Ip") fGrid.Columns.Add("Ping", "Ping") fGrid.Columns(0).Width = 100 fGrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill For i As Integer = 0 To 4 Dim myRowIndex = fGrid.Rows.Add() fGrid.Rows(myRowIndex).Cells(0).Value = "10.216.110." & (11 + myRowIndex).ToString Next End Sub``` Looks like this. <div class="image_block"> <a href="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/ping/ping1.png?mtime=1302788168"><img alt="" src="https://lessthandot.z19.web.core.windows.net/wp-content/uploads/users/chrissie1/ping/ping1.png?mtime=1302788168" width="425" height="410" /></a> </div> So now that we have that we can do the pings in another thread. So I made a button to spawn this thread and begin the process. ```vbnet Private fThread = New Thread(New ThreadStart(AddressOf ThreadProc)) fThread.IsBackground = True fThread.Start() ``` and this will use the following things. ```vbnet Private Sub ThreadProc() Parallel.For(0, 5, Sub(b) CheckOnline(b) End Sub) End Sub Private Sub CheckOnline(ByVal rowindex As Integer) Dim _ping As New Ping Try Dim _pingreply = _ping.Send("10.216.110." & (11 + rowindex).ToString, 2000) If _pingreply.Status = IPStatus.Success Then SyncLock success success.Add(rowindex) End SyncLock End If Catch ex As Exception End Try End Sub``` Yep I used a parallel foreach that will spawn 5 more threads to do the pings in parallel for me. But I also needed a way to tell my users I was doing something and a way to tell them it was all finished and the result. I did that with a second thread that check the result in a list and sees if they are done, making sure I synchronize the lists add method because that is not threadsafe. ```vbnet Imports System.Threading Imports System.Threading.Tasks Imports System.Net.NetworkInformation Partial Public Class Form1 Private fThread As Thread Private fThread2 As Thread Public Delegate Sub AddRowDelegate(ByVal column As Integer) Delegate Sub CheckOnlineDelegate(ByVal rowindex As Integer) Delegate Sub SetOnlineDelegate(ByVal rowindex As Integer) Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load fGrid.Columns.Add("Ip", "Ip") fGrid.Columns.Add("Ping", "Ping") fGrid.Columns(0).Width = 100 fGrid.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill For i As Integer = 0 To 4 Dim myRowIndex = fGrid.Rows.Add() fGrid.Rows(myRowIndex).Cells(0).Value = "10.216.110." & (11 + myRowIndex).ToString Next End Sub Private Sub ThreadProc() Parallel.For(0, 5, Sub(b) Do While Not done.Contains(b) fGrid.Invoke(New AddRowDelegate(AddressOf AddRow), New Object() {b}) Thread.Sleep(300) Loop fGrid.Invoke(New SetOnlineDelegate(AddressOf SetOnline), New Object() {b}) End Sub) End Sub Private Sub ThreadProc2() Parallel.For(0, 5, Sub(b) CheckOnline(b) End Sub) End Sub Private Sub AddRow(ByVal rowindex As Integer) If fGrid.Rows(rowindex).Cells(1).Value Is Nothing OrElse fGrid.Rows(rowindex).Cells(1).Value.ToString.Contains(".....") OrElse Not fGrid.Rows(rowindex).Cells(1).Value.ToString.Contains("Pinging") Then fGrid.Rows(rowindex).Cells(1).Value = "Pinging 10.216.110." & (11 + rowindex).ToString & " " Else fGrid.Rows(rowindex).Cells(1).Value = fGrid.Rows(rowindex).Cells(1).Value.ToString & "." End If fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.White End Sub Private success As New List(Of Integer) Private done As New List(Of Integer) Private Sub CheckOnline(ByVal rowindex As Integer) Dim _ping As New Ping Try Dim _pingreply = _ping.Send("10.216.110." & (11 + rowindex).ToString, 2000) If _pingreply.Status = IPStatus.Success Then SyncLock success success.Add(rowindex) End SyncLock End If Catch ex As Exception End Try SyncLock done done.Add(rowindex) End SyncLock End Sub Private Sub SetOnline(ByVal rowindex As Integer) If Not success.Contains(rowindex) Then fGrid.Rows(rowindex).Cells(1).Value = "Offline" fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Red Else fGrid.Rows(rowindex).Cells(1).Value = "Online" fGrid.Rows(rowindex).Cells(1).Style.BackColor = Drawing.Color.Green End If End Sub Private Sub PingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PingToolStripMenuItem.Click done = New List(Of Integer) success = New List(Of Integer) fThread = New Thread(New ThreadStart(AddressOf ThreadProc)) fThread.IsBackground = True fThread.Start() fThread2 = New Thread(New ThreadStart(AddressOf ThreadProc2)) fThread2.IsBackground = True fThread2.Start() End Sub End Class As you can see I invoke the SetOnline and AddRow methods via the grid so that it can be handled in a thread safe way. I also use delegates for that.

Read More...

Silverlight 5 Beta Is Available For Download

Silverlight 5 Beta is available for download These are the top beta features XAML Debugging with breakpoints for binding debugging Implicit data templates for easy UI reuse Double (and multi) click support GPU-accelerated XNA-compatible 3D and immediate-mode 2D API Low-latency sound effects and WAV support Real operating system windows and multi-display support Significant performance improvements, fixes and much more More info about new features here: http://www.microsoft.com/silverlight/future/ There are also these items for download

Read More...

First Look at IE 10

Channel 9 has made available a video on their site about Internet Explorer 10, I have embedded the video here Here is what they say on the channel 9 site Just when you were expecting to hear more about IE9 on this opening day of Mix ‘11, and today we’ll be talking about the IE10 Platform Preview. IE10 builds on IE9 with support for even more standards like CSS3 gradients, multi-column, and grid layouts. In this video Rob Mauceri walks us through some of the new features of IE10 including an example of flowing of content in multi-column layouts.

Read More...

NuGet stats

If you don’t know NuGet, then you are missing out. NuGetis a package downloader ala gems in the ruby world. There is also the package explorer by dotnetjunky to browse through the packages. This is fun and it has some stats too. Looking at the stats we can see which opensource projects are very popular and perhaps worth a look if you don’t know them yet. These are the first 15. And I know most of them. Pretty sure that the number one position of the EFCodefirst package is due to people wanting to try it out and people using it for demos perhaps.

Read More...

Cycle Detection in F# – Fun With Immutability

I’ve been going through a lot of the problems from Project Euler in F#, and finding myself needing to change my way of thinking more and more often. I recently finished doing one problem that required use of a Cycle Detection algorithm, and as the solution evolved I thought it would be a good example of writing immutable code. Algorithm Being Examined For the example, I’ll use Brent’s variation on the tortoise-and-hare algorithm described at the link because it’s a little shorter. The basic idea of the tortoise-and-hare algorithm is basically what you’d expect, a function is repeatedly applied (at different rates) to a value. The way the value changes in response can help to identify patterns in the function’s behavior. If you’d like to know more about it, check out the wikipedia page. This algorithm is demonstrated with the following code:

Read More...

Get weekly transactions showing Monday's date

This recent MSDN thread presented an interesting, I believe, question: For a predefined set of the Application IDs show the weekly transactions and display Monday’s day for each week. The interesting twist in this common PIVOT query is to display a Monday’s date for a week. Rather than showing a solution for that particular thread, I show an AdventureWorks sample query. This query will return top 5 total amounts for each week by Customer (using SalesOrderHeader table).

Read More...

When changing column data types use ALTER TABLE TableName ALTER Column syntax, don't drop and recreate column

Someone asked how to change the data type from datetime to datetime2 Here is the question I have a SQL Server 2005 database with a datetime column. There is already data in the table but now the customer needs dates before 1753. So I decided to migrate the database to a SQL Server 2008 to use the datetime2 type. However I can’t just switch the type of the column from datetime to datetime2. Is there a way to do this conversion or do I have to reimport the data?

Read More...