I am trying to display a message like 'Please Wait...' in a window, while an event is taking place. I don't want a message box, because that requires a button press. I just want this to be message that I can display and erase.
I've tried adding a TextBlock in the window (WPF), with an initial value of "", then changing the value right before I start the event (in C#), but the message doesn't appear until after the event happens. The event I am doing is creating a window with removable drives, which takes a short amount of time. I think the message appears when the ShowDialog for the new window happens. How do I redraw/refresh the current window with the TexBlock before my event starts so that I get the 'Please Wait...' message immediately? Or, is there a different way of doing this?
Sutton
Loading
Sam HobbsPosted Dec 10, 2011, 1:17 AM
In your situation, the main thread that "owns" the UI must execute to the extent that it is able to update the window, but the other processing prevents that. It is not done automatically instantaneously at the moment you change the text for the window. Correspondingly, and this is more important, when the user does something in the UI, the thread is not able to process that if it is doing something else so the user might click a button but nothing happens.
If you have an opportunity to understand how Windows works at the API/SDK level, you will learn about the message loop and how that is used to process events. In Windows, UI events do not interrupt the UI thread; the thread that owns the window. Something else that you will quickly learn is that windows are not thread-safe; many (most) things cannot be done to a window by any thread other than its owner.
Sutton MehaffeyPosted Dec 9, 2011, 7:38 PM
Sutton
Sam HobbsPosted Dec 9, 2011, 5:36 PM
I said that you need another thread so you can keep the UI responsive. Computers can do many things at the same time, or at least appear to. It requires however that developers do something so that computers know how to keep thing separate. I am being vague because "create a thread to do the other work so that your UI remains responsive" is more specific but I need to explain that in another way. If you want the UI to be able to do things such as put the text in itself (the form; the UI) at the same time that your program does other things, then you need to make those two separate threads. Windows cannot do both at the same time; you, the developer, must make it possible for Windows to do both.
Some programmers use shortcuts such as using a DoEvents function to avoid creating another thread but once you learn about threads, you will have no problem doing that many more times in the future and you will be able to use the same knowledge many more times in the future.
It is something you need to know how to do as a programmer; the concept is relevant to other operating systems too such as Linux which includes Android and WebOS.
Sutton MehaffeyPosted Dec 9, 2011, 5:06 PM
Sutton
Sam HobbsPosted Dec 9, 2011, 2:59 PM