0

Possible Duplicate:
How to fix the flickering in User controls

I am currently working on a program that dynamically loads in an Image for the Background image of the form. I want the form to be visible as soon as the image is fully applied to the background.

I've tried the form event BackgroundImageChanged but this didn't work.

If the image is high resolution I can see the form showing up and the image is still building up itself.

The code snippet:

this.backgroundImage = loadedImage;
// Thread.Sleep(5000);
this.Show();

As you can see I am using Thread.Sleep() to win some time to fully load the image before showing the form. but is there a way like this :

this.backgroundImage = loadedImage;
while( imageIsNotFullyLoaded() ) { }
this.Show();

Or is there any other method or an event that I haven't seen yet?

EDIT:

my code:

forn1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace GameClient {
    public partial class FormMain:Form {
        getImage _getImage;
        public FormMain() {
            InitializeComponent();
        }

        private void FormMain_Load( object sender, EventArgs e ) {
            _getImage = new getImage();
            Image  splashLogo = _getImage.fromURL( "http://ace-acid.no-ip.org/GameClient/splash.png" );
            this.BackgroundImage = splashLogo;
            Thread.Sleep(3000);
            this.Show();
        }

        private void btnLogin_Click( object sender, EventArgs e ) {

            this.Hide();
        }
    }
}

getImage.cs :

using System;
using System.Drawing;
using System.IO;
using System.Net;

namespace GameClient {
    class getImage {
        public Image fromURL( string URL ) {
            Image tmpImg = null;
            try {
                HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create( URL );
                request.AllowWriteStreamBuffering = true;
                request.Timeout = 20000;
                WebResponse response = request.GetResponse();
                Stream stream = response.GetResponseStream();
                tmpImg = Image.FromStream( stream );
                response.Close();
            } catch( Exception ex) {
                return null;
            }
            return tmpImg;
        }
    }
}
Community
  • 1
  • 1
Ace
  • 1,437
  • 6
  • 28
  • 46

2 Answers2

0

There isn't a way to do it like that. The form's actual invalidation/painting is carried out by an interop call to unmanaged code, and there's no callback or hook for when it's finished. You'll have to live with your current procedure.

If you want it to be at least non-blocking, then depending on your .NET target you can use await Task.Delay(5000) (or something equivalent).

Jeff
  • 7,504
  • 3
  • 25
  • 34
  • what is the diference to Thread.Sleep(); ? – Ace Oct 29 '12 at 21:49
  • `Thread.Sleep()` will block (literally suspend) the calling thread, while `await Task.Delay()` won't block your UI thread. See [here](http://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx) for more info on `async`/`await`. – Jeff Oct 29 '12 at 21:52
  • Also I don't think waiting an *unknown* amount of time (`Task.Delay(5000)`) is a good idea – L.B Oct 29 '12 at 21:52
  • 5000 isn't unknown, it's arbitrary. I don't think it's a good idea either, but in lieu of a proper callback... – Jeff Oct 29 '12 at 21:55
0

You can do a simple thing like invoking a delegate, if the method called by a delegate has a return value, the thread will wait for it to finish, so you can try this code:

        delegate Image getImageDelegate(string url);
        private void Form1_Load(object sender, EventArgs e)
        {
            _getImage = new getImage();
Image splashLogo = (Image)this.Invoke(new getImageDelegate(_getImage.fromURL), new object[] { "http://ace-acid.no-ip.org/GameClient/splash.png" });
            this.BackgroundImage = splashLogo;
            this.Show();

        }
Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33