Archive for September, 2007

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

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

Download MySQLDump. Downloads: 2248

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 »