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:
http://stackoverflow.com/questions/711408/best-way-to-invoke-any-cross-threaded-code

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.

ISynchronizeInvokeExtensions

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:
hash_commandline

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:

prompt_set

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 ftp://www.hostname.com, 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;
 
            do
            {
                bytesRead = file.Read(buffer, 0, length);
                ftpStream.Write(buffer, 0, bytesRead);
            }
            while (bytesRead != 0);
 
            file.Close();
            ftpStream.Close();
 
            return res;
        }
Apr 062016
 

C# makes it very easy to parse a xml file or string.

For example, if you want to parse the following xml file, looking just for the value of the tag called “score”:


<?xml version=”1.0″ encoding=”UTF-8″?>
<results>
<status>OK</status>
<url>http://www.monkeycancode.com/</url>
<totalTransactions>1</totalTransactions>
<language>english</language>
<mixed>1</mixed>
<score>0.111642</score>
<type>positive</type>
</results>


This will work even with tag element that is in a nested tag.

        private string ParseResultXML(string sXML, string Tag)
        {
            string sOutput = "";
 
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(sXML);
            XmlNodeList nodeList = xmldoc.GetElementsByTagName(Tag);
 
            foreach (XmlNode aNode in nodeList)
            {
                sOutput = aNode.InnerText;
            }
 
            return sOutput;            
        }
Apr 042016
 

Here I write a very simple Python Class. It’s pretty obvious what it does.


# this is an example to illustrate how to 
# use a class in Python
 
class CAR:
	#some class variables for this object
	year = 2016
	make = ""
	model = ""
	mileage = 0
 
	TotalCar = 0
 
	def __init__(self, year, make, model):
		self.year = year
		self.make = make
		self.model = model
 
		CAR.TotalCar += 1
 
	def DisplayCount(self):
		print ("Total Number of cars %d" % CAR.TotalCar)
 
	def displayCar(self):
		print ("Car Info - MAKE: ", self.make, " , MODEL: ", self.model, " , YEAR: ", self.year, " has total ", self.mileage)
 
#now test the CAR class
car_Toyota = CAR(2004, "Toyota", "Camry");
car_Toyota.mileage = 23999
 
car_HondaCRV = CAR(2015, "Honda", "CR-V");
car_HondaCRV.mileage = 4300
 
car_Toyota.displayCar()
car_HondaCRV.displayCar()
 
print ("Total cars in inventory %d" % CAR.TotalCar)

Here’s what the output would look like:
pyton_class_example_output