.NET

Alternative to HttpClient for Windows Phone 7.5

If you wanted to move your WinRT project to support Windows Phone 7.5, you’ll rejoice at the recent news that Microsoft’s Microsoft.bcl.Async package in Nuget that allows using Async keywords and Task class but in the end, you really used them in WinRT because HttpClient class had a GetAsync method which is not available outside WinRT.

Worry not, the Microsoft.bcl.Async package comes with a Microsoft.Threading.Tasks.Extensions assembly that if you look inside, includes:

public static System.Threading.Tasks.Task<webresponse>GetResponseAsync(this System.Net.WebRequest source)
Member of AsyncExtensions</webresponse>

Which means you can use System.Net.WebRequest in its place and it supports Task/Async.

So you can do something like:

WebRequest request = WebRequest.Create(uri);
await request.GetResponseAsync();

Sharing to Facebook/Twitter from Windows 8/WinRT App

Sharing on “Metro style”/”Windows Store” app is a bit different from what we used to. Instead of invoking it directly in code, Microsoft encourages us to use the centralised “charm” bar where we would then implement a page event handler to fill in details to share.

To personalise share details, we would fill a DataPackage that would then be used across various media such as email or message or other apps. Read More…

Determining Run-Time/DesignMode in .NET

Suppose you fill your table with data from a database, you wouldn’t want it to do so while in Visual Studio’s Designer. Control.DesignMode ought to be the perfect property to check for in your form initialisations but unfortunately, it’s so buggy that it hardly does what you need. It doesn’t work in the constructors and have problems with user controls.

Then people tend to use

If (Process.GetCurrentProcess().ProcessName == "devenv")

which works fine if you call it once but inside a key user control’s initialiser, it may be called hundreds of time and cause a massive memory leak. Currently, the ProcessName string never gets freed and fills up memory very fast. Instead, save the process name in your own global variable when the program starts and check against

If (MyProcessName == "devenv")

instead.

DataGridView Change Cell Data Type After Binding

I don’t know actually since most of the relevant stuff becomes read-only after filling with data. But my approach is solve this as early on in the process as possible to the entirety of the data and save processing time on a custom loop after.

In other words, your DataGridView is typically bound to a DataTable. That is typically filled with stuff from the database. Make sure your AutoGenerateColumns property is set to true in the DataGridView and select your data to match the type you want to display in the DataGridView. For instance, to get checkboxes,

SELECT ..., CAST(column1 AS bit), ... FROM ...

and change the data type right out of the DataAdapter and DataGridView will figure out the rest automatically

Multi-Threading and Delegates Tutorial in VB .NET

This guide will show you everything you need to create a non-GUI multi-threaded application in VB .NET.

There are 2 ways of using multi-threading in VB .NET. First by making use of delegates which I will explain here. And second by programming the threads manually (not actually more complicated).

Using delegates, .NET helps simplify the entire process of using a separate thread and saves you from having to manage the parameter passing, result retrieving and timing issues. Read More…

.NET Compare 2 Datarows by Value Using LINQ

There are too many top Google search results giving the impression that it is impossible to compare 2 DataRows for its value contents and the programmer needs to iterate through everything himself.

Such is no longer true in .NET 3.5 but the blogosphere doesn’t seem to have caught on yet.

It is possible, given 2 DataTables or any other enumerable object types, to compare its contents using LINQ in just one line of code.

I will give an example in VB where I’d want to compare all column properties (description, data type, field length, etc) of all tables in 2 supposedly identical SQL databases. Read More…

Invoke UI Changes Across Threads on VB .Net

I need to do this all the time and don’t have the best memory in the world. Today, I decided that I looked this up one too many times so here’s my solution to this multithreading problem:

The Problem:

You try to modify UI components created in one thread in another thread. In VB, you can only make UI changes on the same thread that created it so you get a nice “Cross thread operation not valid” exception.

Here’s the wrong code:

thread = New System.Threading.Thread(AddressOf DoStuff)
thread.Start()
Private Sub DoStuff()
    'error occurs here'
    Me.Text = "Stuff"
End Sub

Read More…

 Scroll to top