Author Archive

News, Source Code, Wiki-Dashboard, Wordpress, php

Wordpress Plugin: Wiki-Dashboard 0.1

Wiki-Dashboard is a Wordpress plugin that allows to all registered user on a blog to share and edit a text like a wiki, and can be usefull even on mono-author blogs, giving a space to write notes.

Features:

  • Dashboard subpage with the text where to put notes
  • Text shared to all blog authors
  • bbCode parser for text formatting
  • Internationalization support

Installation:
Simply download the Zip-Archive and extract all files into your wp-content/plugins/ directory. Then go into your WordPress administration page, click on Plugins and activate it.
After that you will have a new submenu called “Wiki” under the “Dashboard” menu.

Download Wiki Dashboard 0.1. Downloads: 997

Screenshots:
Wiki-Dashboard showing the text:

Wiki-Dashboard View

Wiki-Dashboard editing the text:

Wiki-Dashboard Edit
Continue Reading »

C, Source Code

Edit form elements from a Thread

Scenario:
The program has to execute a long operation (such as a downloaded file from the internet) when the user clicks on a button, but during the operation the user has the opportunity to interact with the program, and the program gives a visual feedback of the task completion percentage.
Easy, but not working, solution:
To solve a problem like that we need to use a Thread that executes the task in a separate context from the main Form, otherwise, if we execute the long operation in the onClick method of the button, the form will remain frozen during the task execution. The problem comes out when the program tries to update the form from the thread:

private void button1_Click(object sender, EventArgs e)
{
    testThread = new Thread(threadProc);
    testThread.Start();
}

public void threadProc()
{
    // wast a lot of time...
    for (int i = 1; i <= 10; i++)
    {
        Thread.Sleep(250);
        progressBar.Value = i * 10;
    }
    MessageBox.Show("Operation Completed!");
}

Unfortunately, the code that assigns the value to the progressBar will produce an exception like that:
“Illegal cross-thread operation: Control ‘progressBar1′ accessed from a thread other than the thread it was created on.”

lock

Continue Reading »

MySQL, Source Code, Web, php

MySQLDump backup class interface

MySQLDump class is now at version 2.0 and this article contains obsolete informations about the version 1.0. For information about the new version check this article

After the success of the article PHP backup of a mysql database, I decided to write a short post about a file that can be usefull to download the backup of your db site/wordpress blog directly from your browser.
To use the script, you can simply download it and edit the first lines with the basic configuration:

// DB Configuration
$server = "IP of your server (localhost will work in most cases)";
$username = "Valid username for your mysql db";
$password = "Password";
$dbToDump = "Db to dump";
// password to access the php page. PLEASE change the default password!!!
$backupPassword = "UltraSecretKeyThatYouMUSTInsertHere";
// filename to save
$filename = "backup.sql.gz";

After that you can upload the file to your server, in the same folder where is located the class MySQLDump.
To access the script you have to specify two parameters: the password to access the script (pass), and another parameter (t) that if it’s 1 will separate the SQL inserts every 100 rows.
For example, if you upload the file to the root directory of yoursite, you have to write in the address bar of the browser:

http://www.yoursite.com/backup.php?pass=UltraSecretKeyThatYouMUSTInsertHere&t=1

…and your favorite browser will open the dialog to save the file with the db backup!

Download MySqlDump Interface.php. Downloads: 485

For more info about the MySQLDump class, read the original post, or download the source from here:

Download MySQLDump. Downloads: 2599

C

C# Threads using anonymous methods

When we want to use a Thread in C#, we usually write a method with the code that the Thread must execute, and when we create the Thread object we set its delegate to the method already created:

public void DoSomethingUsefull()
{
	for (int i = 0; i < 100; i++)
	{
		// doing something...
		Thread.Sleep(500);
	}
}

static void Main()
{
	// here we create the thread, passing to it the right delegate
	Thread t = new Thread(DoSomethingUsefull);
	t.Name = "Test Thread";
	t.Start();
}

dual core

To create a Thread, C# offers the opportunity to use anonymous methods. To use an anonymous method, we need to write the Thread’s code inside the code that creates the Thread using the delegate keyword:
Continue Reading »

C, MySQL

Convert a DateTime Object in a SQL field

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

If we want to insert a date in a SQL string in C#, the solution seems obvious:

DateTime date = DateTime.Now;
string sql = "INSERT INTO tableWithDate VALUES(1, "+ date.ToString() + ")";

Unfortunately this code can’t work! The function DateTime.ToString() returns a string like 23/02/2007 19.00.49, while MySQL wants a string like 2007-02-23 19:00:49. The following static function converts a DateTime object in a valid date field for MySQL:

public static string GetSQLDate(DateTime date)
{
	string sql;
	sql = date.Year.ToString() + "-" + date.Month.ToString() + "-" + date.Day.ToString() + " "
	+ date.Hour.ToString() + ":" + date.Minute.ToString() + ":" + date.Second.ToString();
	return sql;
} 

I inserted the method in a utility class, that i called Utils, so the working code is:

string sql = "INSERT INTO tableWithDate VALUES(1, "+ Utils.GetSQLDate(date) + ")";

C

Deprecated methods in C#

A deprecated method is a method that is no more supported. For example, you wrote an API with a function that saves something on the disk but in the future versions of your program you want to support only Database saves. In this case you can deprecate the method that saves to disk for a lot of versions: the programmers are now warned from the compiler every time they use the function, and have the time to migrate the code.

stop

If you want to deprecate a method in c#, you can simply add the Obsolete attribute in the function.

[Obsolete("This is a deprecated method.")]
public void DeprecatedMethod()
{
	MessageBox.Show(”I’m a deprevated method! XD”);
}

Continue Reading »

C, MySQL

How to save a SQL Blob in C#

This post is part of the SQLStringBuilder project. For the latest version of the code i suggest to take the source code from the CVS.

Today at work i had my daily dilemma: how can i save a blob field in a MySQL DB with c#??? The solution it’s pretty simple, but it can be hard to find.

The Blob is a binary field where data can be saved: the problem is that we must write these binary data in a textual SQL string. To resolve this problem we can convert the binary data in an hexadecimal representation in order to write the data in a textual format.

Binary data

The resulting SQL will look like that:

INSERT INTO tableWithBlob VALUES(1,0xab425fa52d3e13);

Where 0x… are our binary data.

To start, we must first convert the data structure that we want to put in the database in a byte array (byte[]), or in a format in which we can access all structure’s bytes.
Continue Reading »

C, Source Code, Windows

C++ Timer Class

Starting from the article “C++ timer”, we created a simply class that act as a wrapper to the Windows API and that easily allows to start a timer, stop it. and assign the function to call when the timer is elapsed.

Timer

The following is a little example that explains how to use the class:
Continue Reading »

MySQL

How to create Stored Procedures in MySQL 5.0

Stored Procedures are a way to re-use and parametrize long SQL queries.
To create a Stored Procedure you need at least MySql 5.0. Using MySQL Query Browser the stored procedure creation is very quick:

Right click on the database where do you want to create the procedure (from version 5.0.1 procedures are assigned to a specific database, instead in the 5.0 version the procedures are globals)

New Stored Procedure From QueryBrowser

Continue Reading »

C, Windows

C++ Timer

To create a timer in c++ on Windows we must invoke the SetTimer() method, defined in windows.h.

UINT_PTR SetTimer(
// handle to the window associated to the timer
HWND hWnd,
// Timer ID
UINT_PTR nIDEvent,
// Elapse time in milliseconds
UINT uElapse,
// callback method to invoke when the timer expires
TIMERPROC lpTimerFunc
);

The hWnd argument is the handle to a window: for semplicity we don’t use any Handle, so we set it to NULL.

The function always returns an uint that represents the ID of the timer. if hWnd is NULL, the second parameter will be ignored and we can’t directly set the timer ID, but it will be assigned from the function. We can check if the creation of the timer didn’t have any problem if the uint returned from the function it’s not zero. Instead, if hWnd is not null, the timer ID will be assigned from the nIDEvent parameter

Add a global variable (ok, it’s not a good programming pratice adding global variabile, but this is only an howto) where we can save the timer ID:

uint m_timerID;

Continue Reading »

« Prev - Next »