0
votes

I have a splash screen that i am using it when i am loading data from a smart card because it takes about 35 seconds to get the data my loading screen have a white backcolor and i set the TransparencyKey to white color to make the screen transparent. and it works fine but after about 6 seconds the back color turns to black Here is the code of the loading screen:

partial class LoadingForm : Form
{
    int tickcount = 0;
    public bool CloseIt = false;
    public string Message = "من فضلك إنتظر قليلا ...";
    public Point LocationPoint;
    public LoadingForm()
    {
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);
        InitializeComponent();
        SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor | ControlStyles.DoubleBuffer, true);
        LocationPoint = new Point();
        LocationPoint.X = -300;
        LocationPoint.Y = -300;
        lblMessage.Text = Message;

    }

    private void LoadingForm_Load(object sender, EventArgs e)
    {
        Left = LocationPoint.X;
        Top = LocationPoint.Y;
        timer1.Start();

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (Created)
        {
            if (tickcount++ == 1)
            {
                LocationPoint.X = Screen.PrimaryScreen.Bounds.Width / 2 - 240;
                LocationPoint.Y = Screen.PrimaryScreen.Bounds.Height / 2 - 140;
                lblMessage.Text = Message;

                Left = LocationPoint.X;
                Top = LocationPoint.Y;
                Width = 480;
                Height = 185;
            }
            if (CloseIt)
            {
                pictureBox1.Image = null;
                Close();
                Application.ExitThread();
            }
        }
    }

    private void LoadingForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        timer1.Stop();
        timer1.Dispose();
    }
}

And this is the class that create a thread to run the form on it:

public class LoadingProgress
{
    LoadingForm frm = new LoadingForm();
    string Message = "من فضلك إنتظر قليلا ...";
    Thread th;

    public void StartProgress()
    {
        th = new Thread(new ThreadStart(ShowForm));
        if (frm == null)
            frm = new LoadingForm();
        frm.Message = Message;
        th.Start();
    }

    public void Set_Message(string msg)
    {
        Message = msg;
        frm.Message = Message;
    }

    void ShowForm()
    {
        frm.ShowDialog();

        frm.Dispose();
        frm = null;

        if (th.ThreadState == ThreadState.Running)
            th.Abort();
    }

    public void Stop()
    {
        frm.CloseIt = true;
    }

    public void Set_Position(System.Drawing.Point p)
    {
        frm.LocationPoint = p;
    }
} 
2

2 Answers

2
votes

This is a guess, but I think you're better off creating all your forms on the main application thread (where the message-pump is), and spin the actual work up on separate threads.

My guess is that because your form is not handling to windows events (because it's on the wrong thread), that Windows is essentially marking it as "not responding" and halting any further rendering of it.

0
votes

The Tick event on the System.Windows.Forms.Timer class happens on the UI thread. That means that if you have long-running code in the tick method, it will hang up the application's ability to redraw the interface. When this happens, Windows may draw it as black.