Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Problem is, that i want to show something like a Splashscreen while the Application is Busy. I wanted to Create a Thread starting a new form with a progess bar and a little information that the user has to wait.
So far so good, but the form freezes as it show's up. (If I don't do the update, I commented out, the form not even builds properly).

What is the problem here?

What I have tried:

public class WaitFormManager
    {
        private Thread waitFormThread;

        public WaitFormManager()
        {
            waitFormThread = new Thread(new ThreadStart(OpenWaitform));
        }

        public void ShowWaitForm()
        {
            if (!waitFormThread.IsAlive)
            {
                waitFormThread.Start();
            }
        }

        public void HideWaitForm()
        {
            if (waitFormThread.IsAlive)
            {
                waitFormThread.Abort();
            }
        }

        private void OpenWaitform()
        {
            WaitForm wf = new WaitForm();
            wf.Show();
            //wf.Update();
        }
    }


I called the Form this way:
public static WaitFormManager wfm = new WaitFormManager();
wfm.ShowWaitForm();
//Here we get some stuff done
System.Threading.Thread.Sleep(2000);
wfm.HideWaitForm();
Posted
Updated 29-Sep-16 5:05am
v3
Comments
[no name] 17-Sep-16 12:04pm    
Well, you didn't format your code so that it's readable. And, you didn't debug the Update method to find out what it's doing.
buffedcheesy 17-Sep-16 12:16pm    
... So I activated google's xss to format the code. The Update Function is part of the Form. It just redraws the areas for the user. I wanted to illustrate, that the thread does'nt work the way I want it to.
Ralf Meier 17-Sep-16 13:02pm    
I think (but I'm not quiet sure) that your idea for the approach is complete wrong. Please write more about the thing you want to do.

To your issue : to keep your UI (User-Interface) alive your need to keep your Application responsiv - but this don't happens because you send your Application to Sleep (2000 Miiliseconds). Your Thread itself also don't make anything - so what do you think what happens ?
Dave Kreskowiak 17-Sep-16 20:23pm    
You can't put the form code in a background thread. It won't work. ALL UI elements, including your form MUST be on the UI (startup) thread.

You put the work on the background thread, not the UI.
Philippe Mori 29-Sep-16 12:36pm    
Usually, you should do the opposite. Display the splash on the UI thread and do the app'ication stuff in a background thread.

1 solution

Open UI screen as showdialog then put your long running task under the Task.Run or create a background thread once your task completed then close your UI. You can't open the UI under a new thread UI should be open by main thread.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900