-
Notifications
You must be signed in to change notification settings - Fork 106
Using Progress Report With Import Restore
Below guide is explained in WinForm.
I'm using WinForm to present the example. Below are some of the controls that will be using in this example:
Code behind:
using System.IO;
using MySql.Data.MySqlClient;
Declare, initialize components and create event handlers:
public partial class Form1 : Form
{
MySqlConnection conn;
MySqlCommand cmd;
MySqlBackup mb;
Timer timer1;
BackgroundWorker bwImport;
int curBytes;
int totalBytes;
bool cancel = false;
string dumpFile = "C:\\backup.zip";
public Form1()
{
InitializeComponent();
mb = new MySqlBackup();
mb.ImportProgressChanged += mb_ImportProgressChanged;
timer1 = new Timer();
timer1.Interval = 50;
timer1.Tick += timer1_Tick;
bwImport = new BackgroundWorker();
bwImport.DoWork += bwImport_DoWork;
bwImport.RunWorkerCompleted += bwImport_RunWorkerCompleted;
}
private void btImport_Click(object sender, EventArgs e)
{
}
private void btCancel_Click(object sender, EventArgs e)
{
}
void bwImport_DoWork(object sender, DoWorkEventArgs e)
{
}
void mb_ImportProgressChanged(object sender, ImportProgressArgs e)
{
}
void timer1_Tick(object sender, EventArgs e)
{
}
void bwImport_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
void CloseConnection()
{
}
}
A BackgroundWorker (bwImport)
is used to handle the Import Process. BackgroundWorker
will execute codes on another Thread. By doing this, the UI of WinForm will not freeze during the Import Process is running.
The codes start from the click of Import
button, below code is executed:
private void btImport_Click(object sender, EventArgs e)
{
// Reset variables
curBytes = 0;
totalBytes = 0;
cancel = false;
// Initialize MySqlConnection and MySqlCommand components
conn = new MySqlConnection(ConnectionString);
cmd = new MySqlCommand();
cmd.Connection = conn;
conn.Open();
// Start the Timer here
timer1.Start();
mb.ImportInfo.IntervalForProgressReport = (int)nmImInterval.Value;
mb.Command = cmd;
bwImport.RunWorkerAsync();
}
BackgroundWorker of bwImport.RunWorkerAsync()
will execute the following codes:
void bwImport_DoWork(object sender, DoWorkEventArgs e)
{
try
{
mb.ImportFromFile(dumpFile);
}
catch (Exception ex)
{
cancel = true;
CloseConnection();
MessageBox.Show(ex.ToString());
}
}
A method called CloseConnection()
is executed in the Exception Catching block.
If anything bad happens in the process, we need to close the connection between our application and MySQL server.
void CloseConnection()
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
if (cmd != null)
cmd.Dispose();
}
During the Import task is running, this event will raise to report it's status. The values is loaded to a temporary location.
void mb_ImportProgressChanged(object sender, ImportProgressArgs e)
{
if (cancel)
mb.StopAllProcess();
totalBytes = (int)e.TotalBytes;
curBytes = (int)e.CurrentBytes;
}
The timer will read the values and update the progress bar:
void timer1_Tick(object sender, EventArgs e)
{
if (cancel)
{
timer1.Stop();
return;
}
progressBar1.Maximum = totalBytes;
if (curBytes < progressBar1.Maximum)
progressBar1.Value = curBytes;
lbStatus.Text = curBytes + " of " + totalBytes;
}
After the BackgroundWorker - bwImport
finished running it's job, it will raise this event:
void bwImport_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
timer1.Stop();
CloseConnection();
if (cancel)
{
MessageBox.Show("Cancel by user.");
}
else
{
if (mb.LastError == null)
{
progressBar1.Value = progressBar1.Maximum;
this.Refresh();
MessageBox.Show("Completed.");
}
else
{
MessageBox.Show("Completed with error(s)." +
Environment.NewLine + Environment.NewLine +
mb.LastError.ToString());
}
}
}
user can cancel / stop the process any time by pressing the Cancel
button and this will execute this codes:
private void btCancel_Click(object sender, EventArgs e)
{
cancel = true;
}
An error log file will be created if specify in MySqlBackup.ImportInfo
.
You can also get the Last Exception by accessing MySqlBackup.LastError
.