开发者

Show a Animation during a loading

开发者 https://www.devze.com 2023-02-02 12:50 出处:网络
Well, what I am tryi开发者_运维技巧ng to do is show a animated gif while it reads a directory full of files, however the UI freezes, which is ok, but i would like to keep the gif running till the oper

Well, what I am tryi开发者_运维技巧ng to do is show a animated gif while it reads a directory full of files, however the UI freezes, which is ok, but i would like to keep the gif running till the operation is finished. Any ideas?

I am doing it on a Windows Form using VS2010 C#


Here is some example code how you can Load your files aysnchronous. Maybe it helps you. I like this way more than using DoEvents. With DoEvents I had already have some ungood side-effects, therefore I try not to use it.

BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true}; 
bgWorker.DoWork += (s, e) => {     
    // Load here your file/s     
    // Use bgWorker.ReportProgress(); to report the current progress 
}; 
bgWorker.ProgressChanged+=(s,e)=>{     
    // Here you will be informed about progress and here it is save to change/show progress. You can access from here savely a ProgressBars or another control. 
}; 
bgWorker.RunWorkerCompleted += (s, e) => {     
// Here you will be informed if the job is done.
// Use this event to unlock your gui
}; 
bgWorker.RunWorkerAsync(); 


You have two options.

  1. Start a separate thread to handle the file operations.

  2. periodically call Application.DoEvents() within the file loop. That will cause your app to process pending messages (thus updating your UI), but will negatively impact the speed of the file processing loop.

Posting from my phone so no example links.


Run enumeration is a separate thread and update the GUI in the main thread.


Would something like this work with backgroundWorker?

    private void buttonRename_Click(object sender, EventArgs e)
    {
         backgroundWorker1.RunWorkerAsync();
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        foreach (ListViewItem myitem in listView.Items)
            {
                 try
                 {
                       //Rename
                 }
                 catch
                 {
                 }
            }    
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消