Efficiently display file status when using background thread

Posted by schmoopy on Stack Overflow See other posts from Stack Overflow or by schmoopy
Published on 2010-05-11T21:30:41Z Indexed on 2010/05/11 21:34 UTC
Read the original article Hit count: 237

Filed under:
|
|

How can i efficiently display the status of a file when using a background thread?

For instance, lets say i have a 100MB file:

when i do the code below via a thread (just as an example) it runs in about 1 min:

foreach(byte b in file.bytes)
{
   WriteByte(b, xxx);
}

But... if i want to update the user i have to use a delegate to update the UI from the main thread, the code below takes - FOREVER - literally i don't know how long im still waiting, ive created this post and its not even 30% done.

int total = file.length;
int current = 0;
foreach(byte b in file.bytes)
{
   current++;
   UpdateCurrentFileStatus(current, total);
   WriteByte(b, xxx);
}

public delegate void UpdateCurrentFileStatus(int cur, int total);
public void UpdateCurrentFileStatus(int cur, int total)
{
        // Check if invoke required, if so create instance of delegate
        // the update the UI

        if(this.InvokeRequired)
        {

        }
        else
        {
          UpdateUI(...)
        }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading