Run a Windows Service as a console app

When working on a Windows Service project you may want to run it as a Console App so you can see what’s going on. If you try to run the service in Visual Studio you’ll get an error that says: “Cannot start service from the command line or a debugger. A Windows Service must first … Read more

How to set a timeout for TcpClient.ConnectAsync()

TcpClient has no direct way to set the connection timeout. It doesn’t have any parameters that allow you to control it, and SendTimeout / ReceiveTimeout don’t apply to the initial connection. The way I control the connection timeout is by awaiting a Task.WhenAny() with TcpClient.ConnectAsync() and Task.Delay(). Task.WhenAny() returns when any of the tasks complete. … Read more

How to update UI from another thread

I often need to be able to run multiple threads and update the UI based on the results. For example, I may need to execute GET requests to 10 different endpoints concurrently, and then report their results in a datagrid as they come back. The problem is you can’t just update the UI from any … Read more

C# – Using custom attributes

In this article I’ll be showing how to use custom attributes in C#. As an example, I’m creating a Console app that color codes output based on a status. Here’s what this looks like: What is an attribute? First of all, what is an attribute? Attributes are a clean way to add additional information about … Read more

ReportViewer – How to format phone numbers

I am building a ReportViewer report and need to format phone numbers. For example, I want “5551234” to be formatted as “555-1234.” Specify the phone number format Use the following expression: Now when I run the report it looks like this: Other formatting examples In my example above I was using a very simple format. … Read more

System.InvalidOperationException: Objects added to a BindingSource’s list must all be of the same type.

Problem I am building a report using ReportViewer RDLC and I’m trying to bind the data. When I try to add my BindingList to the BindingSource I get the following exception: System.InvalidOperationException: “Objects added to a BindingSource’s list must all be of the same type.” Here’s the code causing this: Solution Set the BindingSource.DataSource to … Read more