Results 1 to 7 of 7

Thread: help with progressbar and Thread

  1. #1

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    help with progressbar and Thread

    hi guys! i have a code below that supposedly, Set e.Cancel to true, set the LblProgressBar (a ToolStripStatusLabel) Text Property to "Performing Backup" and Visible property to true, set the pBar(a ToolStripProgressBar) Visible property to true and call the function FuncPeformBackup using Thread after the FuncPerformBackup is finished set the e.Cancel to false, for it to close the form, But it doest not work the way I expected the LblProgressBar and pBar does not show up. I inserted 200,000 rows in one of the tables in the database for the backup to take a while and to check if the label and the progress bar will show up..but i have no luck. I just want to have a progress bar to let the user know that the application is performing backup job in background. Any suggestion guys on how can i solve this problem..Thanks!

    Code:
            private void RCPSIndex_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (DBBackupRecovery.dbEnableBackup && DBBackupRecovery.dbBackupEverySysClose)
                {
                    e.Cancel = true;
                    LblProgressBar.Text = "Performing Backup";
                    LblProgressBar.Visible = true;
                    pBar.Visible = true;                
                    Thread t = new Thread(new ThreadStart(FuncPeformBackup));
                    t.Start();
                    Thread.Sleep(100);
    
                    t.IsBackground = true;
                    t.Join();
                    e.Cancel = false;
                }
            }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: help with progressbar and Thread

    try using the Backgroundworker component. It can report progress to a progressbar while doing the work
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Fanatic Member daimous's Avatar
    Join Date
    Aug 2005
    Posts
    657

    Re: help with progressbar and Thread

    Actually, I've tried that prior to creating this thread but got no luck.

    Code:
            private void RCPSIndex_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (DBBackupRecovery.dbEnableBackup && DBBackupRecovery.dbBackupEverySysClose)
                {
                    e.Cancel = true;
                    LblProgressBar.Text = "Performing Backup";
                    LblProgressBar.Visible = true;
                    pBar.Visible = true;
                    BackupBackgrndWorker.RunWorkerAsync();
                    e.Cancel = false;
    		//I know there is a problem here, It will exit without checking if BackupBackgrndWorker
    		//is Completed
                }
            }
    
            private void BackupBackgrndWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                FuncPeformBackup();
            }
    
            private void BackupBackgrndWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                LblProgressBar.Text  = "Backup Completed";
                LblProgressBar.Visible = false;
                pBar.Visible = false;
                BackupBackgrndWorker.Dispose();
            }
    
            public void FuncPeformBackup()
            {
                string BackFilePath = CommonFunctions.GenerateBackupFileName();
                bool backup = CommonFunctions.BackUpDB(DBBackupRecovery.dbServer, DBBackupRecovery.dbName, DBBackupRecovery.dbUserName,
                                         DBBackupRecovery.dbPassword, BackFilePath, DBBackupRecovery.dbUseWindowAuthentication);
            }

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: help with progressbar and Thread

    RunWorkerAsync() means start running on another thread so the maching initializes that thread and gets back to where it left which is "e.Cancel = false;" so it exits the application
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: help with progressbar and Thread

    call this to update your progressbar wouldn't be a bad idea either.

    vb Code:
    1. void bw_ProgressChanged (object sender,
    2.   ProgressChangedEventArgs e) {      }

    http://www.albahari.com/threading/pa...ckgroundWorker

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: help with progressbar and Thread

    Quote Originally Posted by superbovine
    call this to update your progressbar wouldn't be a bad idea either.

    vb Code:
    1. void bw_ProgressChanged (object sender,
    2.   ProgressChangedEventArgs e) {      }

    http://www.albahari.com/threading/pa...ckgroundWorker
    Actually there is a method called ReportProgress in the BackgroundWorker Class to do that
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: help with progressbar and Thread

    Quote Originally Posted by ComputerJy
    Actually there is a method called ReportProgress in the BackgroundWorker Class to do that
    you got the idea:P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width