Click here to Skip to main content
Click here to Skip to main content

Converting Icons to Images with a Background Worker

, 25 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Using a background worker thread to execute a time consuming task

Introduction

This article is an example of using a background worker thread to execute a time consuming task.

Background

I like creating software and for this I need some images for buttons, etc. There are many images you can find for downloading from the internet, but unfortunately, many are in icon format (.ico, .icl, etc) and must be converted before use. You can use many image editors such as PhotoShop, Gimp, Axialis, etc., but you must convert the images one by one. Being kind of lazy, of course I don't like that, so I created an application to automate the process. The function for converting is time consuming, however, so it makes user interface (UI) unresponsive. To resolve that problem, the function must execute in a background thread.

Using the Code

To convert icons, I use the iconlib library from this article. [^]

The following struct is used for passing an argument to the background worker. You need two of these, one for passing to background worker and one for updating the UI.

public struct PassedObject //for passing to background worker
{
    public List<string> fileListing;
    public string initDirectory;         
    public PassedObject(List<string> listFilenya, string initDirnya)
    {
        fileListing = listFilenya;
        initDirectory = initDirnya;
     }
}
public struct ItemToReport //for passing from background worker
{
    public string strToReport ;
    public int id;
    public ItemToReport(string reportString, int ID)
    {
        strToReport = reportString;
        id = ID;
    }
}

The following function executes in background, converting the icons. Be careful, because any exception that occurs in this code is hard to debug. (I'm using the Express Edition.)

private void convertIcon(BackgroundWorker worker, DoWorkEventArgs e)
{
    PassedObject objectToPass = (PassedObject)e.Argument; //perform unboxing
    //initial folder to create new folders of converted image
    string initDirectory = objectToPass.initDirectory ; 
    ///hold list of files
    List<string> fList = objectToPass.fileListing ;
    ...
    foreach (string item in fList )
    {
        FileInfo iconFile = new FileInfo(item);
        ItemToReport pesan = new ItemToReport("processing " + iconFile.Name + "...\n", 0);
        curIconProg = 0; //reset icon counter, prevent overflow at progressbar        
        worker.ReportProgress(0, pesan); //report : processing----        
	...
    }
}

The function that calls the background function and begins execution of the background thread is

private void bgw_DoWork(object sender, DoWorkEventArgs e)
{            
     BackgroundWorker worker = sender as BackgroundWorker;
     convertIcon(worker, e);
}

This code is called when the background function wants to update the UI on the main thread. In my application, it is used for updating the ProgressBar. To allow the UI to be updated, you need to set workerReportProgress to true.

private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    ItemToReport report = (ItemToReport)e.UserState; //perform unboxing message
    switch (report.id )
    {
         case 0:
            rtbReport.AppendText(report.strToReport);
             break;
         ...            
    }
}

The following function is called when the background worker has complete its work.

private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    btnConvert.Enabled = true; 
}

That's all there is to it. It should be easy to implement.

Points of Interest

It took me a long time to finally understand this process. Actually, most of the information comes from help, but some points are missing so it's not as easy to implement as it should be.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

asugix
Engineer Donatus Inc.
Indonesia Indonesia
I'm currently a student of Brawijaya University. I work on programming just for hobby. I learn programming since 2nd year of my college. That's when I first bought my PC on year 2004.

Comments and Discussions

 
GeneralA broken link PinmemberQing Jiang3-Nov-10 7:47 
GeneralRe: A broken link Pinmemberasugix5-Nov-10 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 25 Oct 2010
Article Copyright 2009 by asugix
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid