Results 1 to 3 of 3

Thread: Animated (.gif) image on the form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    11

    Animated (.gif) image on the form

    I have a pictureBox on my windows form with the animated .GIF image. Form calls some methods that can take minute or two to run, so I thought I could change the visible property of my picture box to true and let the animation to run before method returns. The problem is that the image will remain still or will barely move. Is there any way to keep the animation going while method runs?

    Thanks for your help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Animated (.gif) image on the form

    Your process is too busy performing the other operation to bother with updating the PictureBox. If you want two things to happen simultaneously then you need to use multiple threads. Your main thread will animate the image while a worker thread performs the long-running operation. .NET 2.0 makes this quite simple with the addition of the BackgroundWorker component. Add a BackgroundWorker to your form in the designer and add code along the following lines:
    Code:
    private void StartCalculation()
    {
        this.pictureBox1.Visible = true;
    
        // Raise the DoWork event in a worker thread.
        this.backgroundWorker1.RunWorkerAsync();
    }
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Perform your long-running operation here.
    }
    
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        // The long-running operation has finished.
        // This event is raised in the main thread.
        this.pictureBox1.Visible = false;
    }
    Note that the DoWork event is raised in a worker thread so there may be issues with accessing controls or synchronising data, depending on exactly what you're doing. If you want to know more about multi-threading then I suggest that you follow the Articles -> Advanced .NET link in my signature and read the Managed Threading section.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2006
    Posts
    11

    Re: Animated (.gif) image on the form

    Thanks jmcilhinney;
    I will try your solution tomorrow and will get back with the feedback. Thanks again.

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