简体   繁体   中英

C# splash screen using a new thread

I have a big winform application that takes long time to load so I wrote a splash screen for it.

The problem is that when I show the splash form from a new thread, the progress bar will freeze 2 or 3 times while loading. But when I do it using a separated process I haven't any problem and it has a smooth motion.

I want to know that what's the difference between a new thread and a separated process in such a situation.

Thanks

Simply think of it as the Main form has it's own thread. When you are doing something on that thread that is task intensive, it doesn't get a chance to update the UI. However, when you create a new thread, you are in effect creating a new worker that can update the splash screen UI, while the Main form's thread is performing its workload.

A process is an executing instance of an application. For example, when you double-click the Microsoft Word icon, you start a process that runs Word. A thread is a path of execution within a process. Also, a process can contain multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process.

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not.

It could be because the UI for the splash screen needs to be in a completely separate thread from the main window with a totally separate Windows message queue.

To run some UI in a different thread from the main thread you will need to start a new message pump for it, because message queues cannot be shared between threads.

To start a new message pump, call Application.Run(yourSplashScreen); from the separate thread. Create your splash screen form from the separate thread too.

Note that you cannot directly manipulate controls in one form from code executing in a different form that you started in a separate thread. You would have to use Control.Invoke() to do so, just like you normally do with multiple threads.

Anyway, if you use a separate message queue like this it could help to prevent the stalling that you're seeing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM