C # uses multi-threading and recommends the use of BackgroundWorker.
1. Definition:
BackgroundWorker bgworker = new BackgroundWorker ();
2. Initialize:
Bgworker.workerreportsprogress = true;
Bgworker.dowork + = Bgworker_dowork;
Bgworker.progresschanged + = bgworker_progresschanged;
bgworker.runworkercompleted + = bgworker_runworkercompleted;
Where DoWork is the function that the thread will execute.
ProgressChanged is a callback function when a progress update is made
RunWorkerCompleted is the callback function at the completion of the thread execution, which can invoke something directly from the UI thread.
3, function Body implementation:
void Bgworker_progresschanged (object sender, ProgressChangedEventArgs e)
{
int progress = E.progresspercentage;
Labelprogress.text = string. Format ("Completion {0}%", progress);
Labelprogress.left = (this. Width-labelprogress.width)/2;
}
void Bgworker_runworkercompleted (object sender, Runworkercompletedeventargs e)
{
if (Picturelist.count > 0)
{
Showpicture (Curindex);
}
Labelprogress.visible = false;
}
void Bgworker_dowork (object sender, DoWorkEventArgs e)
{
list<string> piclist = new list<string> ();
String[] Extlist = {". jpg", ". png"};
int index = 0;
foreach (string item in FileList)
{
if (Extlist.contains (System.IO.Path.GetExtension) (item). ToLower ()) = = True)
{
String backname = Addimagetodb (item);
Debug.WriteLine ("ImageName:" + backname);
if (string. IsNullOrEmpty (backname) = = False)
Piclist.add (Backname);
}
index++;
Bgworker.reportprogress (Index/filelist.count);
}
Curindex = Picturelist.count;
Picturelist.addrange (piclist);
}
C # uses background processes