Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animated Gif in form using C#

Tags:

c#

In my project, whenever a long process in being executed, a small form is displayed with a small animated gif file. I used this.Show() to open the form and this.Close() to close the form. Following is the code that I use.

public partial class PlzWaitMessage : Form
{
   public PlzWaitMessage()
   {
      InitializeComponent();
   }

   public void ShowSpalshSceen()
   {
      this.Show();
      Application.DoEvents();
   }

   public void CloseSpalshScreen()
   {
      this.Close();
   }
}

When the form opens, the image file do not immediately start animating. And when it does animate, the process is usually complete or very near completion which renders the animation useless. Is there a way I can have the gif animate as soon as I load the form?

like image 283
Rabin Avatar asked Jun 18 '10 10:06

Rabin


People also ask

Can you put a GIF in a Google form?

Now that your GIF is created, you can head on over to Google Drive and start creating your Google Form. Once that is underway, you can insert your GIF into the form by adding it as an image.


1 Answers

Why not using threads? It's always good idea to learn something new.

You could simply put your "long process" in background thread, and use events to report to presentation layer, for example:

// in your "long process" class
public event Action<double> ReportCompletition;

// this method will start long process in separate background thread
public void Start()
{ 
    Thread thread = new Thread(this.LongProcess);
    thread.IsBackground = true;
    thread.Start();
}

private void LongProcess()
{
    // do something
    // report 10% completition by raising event
    this.ReportCompletition(0.1);
    // do something more
    this.ReportCompletition(0.5);
    // ... and so on
}

This way, all you have to do is implement simple method in your Form/UI, which will consume this information.

public partial class MainApplicationWindow : Form
{
    private LongProcessClass _longProcess;

    public MainApplicationWindow
    {
        this.InitializeComponent();
        this._longProcess = new LongProcessClass();
        // bind UI updating method to long process class event
        this._longProcess.ReportCompletition += this.DisplayCompletitionInfo;
    }

    private void DisplayCompletitionInfo(double completition)
    {  
        // check if control you want to display info in needs to be invoked
        // - request is coming from different thread
        if (control.InvokeRequired)
        {
            Action<double> updateMethod = this.DisplayCompletitionInfo;
            control.Invoke(updateMethod, new object[] { completition });
        }
        // here you put code to do actual UI updating, 
        // eg. displaying status message
        else
        {
            int progress = (int) completition * 10;
            control.Text = "Please wait. Long process progress: " 
                + progress.ToString() + "%";
        }
    }

Of course, you can report anything you like from within long process. Be it completition rate, ready to display string messages, anything. You can also use events to report that long process has finished, broke, or any long process data you wish.

For more detailed information on this topic you might want to check MSDN tutorials on Threading and Events.

like image 135
k.m Avatar answered Oct 25 '22 06:10

k.m