Sep 042016

Normally, you call the RunWorkerAsync() without an argument to start the background worker. Sometimes you might want to pass some data into the DoWork() function.

All you have to do is simply do:
RunWorkerAsync(some data)

For example, the following code pass an array of string:

string[] list = {"one", "two", "three"};

Then you have to write the receiving code in the DoWork Event function, the trick is in the DoWorkEventArgs e contains the data you passed. You simply use e.Argument, then cast the object into the original type when you passed the data.

For example: the following code receives the string[] list

private void MyBackgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
   //get the string[] that was passed by RunWorkerAsync(List)
   string[] myList = e.Argument as string[];
Sep 032016

Normally, you can not set design time property within a user control. Design time means when you drag and drop a user component on your windows form, you can see the properties grid, that allow you to set the properties of a component.

In order to make certain properties of your user control visible in properties grid during design time, you will have to add some attributes to your definition of properties.

For example: this is an example of simple property for a text box, that sets a prompt value.

        [Description("Prompt for entering some text"), Category("Data")]
        public string Prompt
                PromptText = value;
                this.Text = value;
                return this.Text;

Note: you must have a get block for this property, otherwise this property will not show up in design time.

Aug 122016

Sometimes when you try to access UI component from a different thread, you’ll run into exception on you can’t access UI component from a different Thread than the original UI thread that creates the component.

For example, I have a Label, I would like to set the Label1.txt to some text from inside of a backgroundWorker.

Here’s some reference links:

They come up with a very simple way of update any UI component from a different thread.

How to use:
1. Download the following code and place it inside your own project.


2. In the function where you want to update the UI component, invoke this class.

For example:

this.InvokeEx(f => f.label1.Text = "updating label ...");

Jul 302016

This is the step by step guide to show how to install the Virtuabox Guest Addition software on Ubuntu Virtual Machine, and set up a shared folder across windows host and Ubuntu Guest Virtual Machine.

  1. Login to your Ubuntu Virtual Machine inside of VirtuaBox. Open a Terminal.
  2. Update your APT database with this command: sudo apt-get update.
  3. Install the latest security updates with this command: sudo apt-get upgrade.
  4. Install the rquired packages with this command: sudo apt-get install build-essential module-assistant
  5. Configure your system for building kernel modules with this command: sudo m-a prepare
  6. From inside the VirtuaBox Software, go to menu Devices >> Insert Guest Addition CD Image. This will mount the guest addition software as an cd in your Ubuntu. Ubuntu will ask if you want to auto run the software installation. The guest addition will install and compiled automatically in the terminal. 
  7. In your Virtuabox, select Settings for your Ubuntu OS, and go to Shared folder and setup a folder from windows as the shared folder.
  8. In your Ubuntu system, you need to add the current user to the vboxsf group, otherwise, when you see the shared folder mounted and want to access, you’ll get a permission denied error. In a termainal, type this command: sudo adduser <Your User Name> vboxsf
  9. Reboot your Ubuntu System. Now you have access to this folder which allows you to easily share file between the host and and guest OS.
Jun 222016

The following query will get the latest entered_on date from the result table.


-- This select will get the latest result entered_on date
-- to check after a refresh what's the latest data
-- SELECT * FROM ( your_query_here ) WHERE ROWNUM &lt;= N.
select * from (select entered_on from result where trim(entered_on) is not null order by entered_on desc) where rownum &lt;= 25

the reason to use an inline query is that inline query is getting all the results back, while the main select would grab the top N results using rownum.

If you do “select entered_on from result where rownum <= N order by entered_on desc “, it would return the wrong result, instead of getting the top N records, you would get N random records.

Jun 132016

Windows has a build in command line tool called CertUtil, which allows you to generate a file checksum hash.

For exmaple:
CertUtil -hashfile [FileName] [Hash Algorithm]

[Hash Algorithm] could be: MD2 MD4 MD5 SHA1 SHA256 SHA384 SHA512

An example command would be:
CertUtil -hashfile c:\myfile.txt SHA512.

This command line will give you an output of hash string, which you can use to verify the file integrity after the file transfer.

Here’s what the output looks like:

Jun 102016

using Set command to set a variable with an input value. /P would tell command line to prompt with a question.

In the example below, command line will prompt you to “Please enter your name:”, then set the value you entered to the variable your_name. In the next line, echo would be printing out the value you entered for variable your_name.

@echo off
set /P your_name="Please enter your name:"
echo Hi, %your_name%

Here’s what the output looks like:


Apr 192016

In this example, I will show you how to easily upload a file to your web server via FTP.

You’d need to add using System.Net; Make a FTP connection and read a file into stream, then writes to FTP.

Important Note:

  • because you’re casting WebRequest to a FtpWebRequest, your Address need to start with, otherwise you will get an exception about invalid cast.

        public static string UploadFile(string UploadFileName, string Address, string Username, string Password)
            string res = "";
            System.IO.FileInfo fi = new System.IO.FileInfo(UploadFileName);
            FtpWebRequest req = (FtpWebRequest)WebRequest.Create(Address + "/" + fi.Name);
            req.Method = WebRequestMethods.Ftp.UploadFile;
            req.Credentials = new NetworkCredential(Username, Password);
            Stream ftpStream = req.GetRequestStream();
            FileStream file = File.OpenRead(UploadFileName);
            int length = 1024;
            byte[] buffer = new byte[length];
            int bytesRead = 0;
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer, 0, bytesRead);
            while (bytesRead != 0);
            return res;