How can I show a dialog message, similar to MessageBox, which have just the "Cancel" button and can be closed by the application.
The idea is to show the dialog while the application retrieves data from a server, allowing the user to cancel this request, and closing the dialog once the request is completed.
I recall having a very similar problem in the past. I don't think there is a dialog message "out of the box" that works like that. The way I solved this was by writing a class that modeled this sort of behavior in a window and having the application spawn an instance of the window.
The silverlight message box blocks code execution while it is open so it is not possible to close it. However you can use an XNA messagebox in Silverlight which is asynchronous
This explains its use in depth
You can probably then call EndShowMessageBox for your purposes.
if your intention is to let the user know that something is loading and that they should wait. you should use a progressbar instead.
How to: Create a Custom Indeterminate Progress Bar
Related
I have an application that monitors bit states from external socket/Ethernet connections, known as a PLC device.
These states trigger database calls and UI updates, some of which require Modal dialogs, others require Modeless dialogs.
The issue is that the process needs to continually monitor even when a Modal dialog is displayed.
I have tried using a DispatcherTimer, but this will block when a Modal dialog is displayed. I understand the reason why.
I tried a standard Timer, but I get an exception stating something like 'Some UI components require the thread to run in STA mode.'
So I create a standard Thread and set the ApartmetState to STA. The thread runs just fine after adding DispatcherInvoke around the part of the processing loop that requires UI interaction, but whenever a Modal dialog box is displayed, the thread is blocked until the Modal dialog is closed.
Now I really do not want to get into inter-thread messaging and over-complicating the whole thing.
I have also tried putting the Modal dialog into its own thread whenever a Modal dialog is needed. Everything works fine and it was almost acceptable, but the owner window of the application is still accessible, essentially making the Modal dialog a Modeless dialog. I tried setting the IsEnabled flag on the owner window and at first I thought that would be okay, but it is still active when clicked and hides the dialog.
Any suggestions?
I have solved the issue! I reverted back to a standard Timer and was able to get around not requiring STA mode; this reguired another judicial placement of Dispatcher.Invoke.
Now everything works as I expect.
I am trying to hide all the windows of my app when a users session time's out. That works great; however, when a MessageBox is left open when the app times out the message box still is there. I want to be able to close the message box or at least hide it. I've looked up hwnds and hinstances and I don't quite know if thats what I am looking for to accomplish this. I thought this would be fairly easy as I thought MessageBox inherited from Window and of course it doesn't. And a top of that you cannot create an instance of message box to keep track of it. At this point I am not sure what to actually do or what to actually search for.
I am in the process of trying to figure some stuff out using reflection. I can create an instance of MessageBox using reflection but don't think I can call Show() using an instance.
Short answer: just create your own window that looks like a message box
Long answer:
Message boxes are normal WinAPI windows (they have an HWND and can be manipulated by the native API windowing functions) however they are not WPF windows and don't have an associated Window object.
Since they are normal windows you can get their HWND (using FindWindow/ FindWindowEx/ EnumWindows) and trick them into closing (for example, by simulating a Cancel button click)
However, because message boxes are not designed to be manipulated like that this trick has some nasty pitfalls you have to take care of.
And, because message boxes are so simple and you can easily create a message box clone it's just easier to create the close and not take care of all the message boxes corner cases.
In my project I have a MessageBox that pops up from time to time.
When I'm playing a game and the MessageBox appears, the game is minimized and I'm back to the desktop.
This might depend from app to app, but this specific app/game minimizes when MessageBox appears.
How to avoid this behavior? Is there anything I can do to the MessageBox to make it lose focus/not activated ? I tried to look at the MessageBox methods but no luck.
It sounds like you want the message box to be able to display while the rest of your code is still running. Type of messagebox you are using is modal and needs to be closed until it allows you to interact with the other open window.
I suggest you start a new thread for the message box so that the thread can continue to run allowing you to be able to interact with the other windows.
I am having a very difficult time trying to debug/fix an application.
Briefly:
- I created a "wizard" type app that starts with the user taking a photograph (using the common dialog for photos)
If the user tries to use the text input window (SIP) (the little keyboard input window) after a photo is taken the event loop seems to hang - the event is not processed or is delayed for a while.
If the user does not take a picture the SIP keyboard works great.
This only happens on some of my devices. Specifically it is not a problem on an MC65 but is a problem on an ES400.
It appears that the app's event loop gets screwed up with the way I am displaying forms and taking photos.
If created a simple test app with single form containing a button (Event handler takes a photo) and a text box that accepts input. That works fine. But it is only a single form app that does nothing else.
When I combine the photo taking with my form displaying (making a "wizard" ) things go badly.
I wonder what kind of event loop should I be running?
Essentially the user takes a photo then goes through some forms (I hide one form and show another when they click the "next" button.)
The Form.Show is called from the main form after a picture is taken and then I have something like:
while(UserNotFinished)
{
Application.DoEvents()
}
Where UserNotFinished is a flag set from my wizard/forms after the "submit" button is pressed.
I will be happy to provide more code but not sure what would be useful.
I am new to C# and CF development (lots of years of C++/Win32)
The real confusing part is that this works on one device but not on another. In fact, the device hangs completely. It ends the activesync connection and sometimes I have to hard reset by removing the battery.
I think your problem stems from the while(true) { DoEvents(); } and perhaps how you are trying to go between forms. The only time I've used the DoEvents() method is when I'm already in the scope of a windows event and I need to be sure something in the message queue is processed so screen updates are correct. I'd suggest making a controller class to manage the screen flow for your wizard. You can control the screen flow by either using ShowDialog() and execute the flow control directly in the scope of a single call, or you'll have to use Show() and an asynchronous mechanism such as subscribing to and handling specific form and control events in the controller class. Also saw the comment about introducing another thread, beware that Forms belong to the thread they were created in and you must Invoke(...) all Form members in the context of the creating thread.
Hmm. Very strange
I started a new thread and basically call Application.DoEvents() in in as well and it seems to fix the problem...
I don't know why the
while(true)
{
DoEvents()
}
in the main thread doesn't work.
I'm trying to make a video recording application for a project and was wondering if there was anyway to make the OpenFileDialog open up in a modeless diaglog box or would I have to make my own custom version? The reason I ask is the ShowDialog() function freezes my video. Thanks in advance for the help.
Yes, you can prevent a file dialog from blocking the UI thread, but it can be rather non-trivial depending on the details of how you want it to behave. If you're open to buying a component that does this automatically, ComponentAge offers one. Otherwise, you can roll your own by opening the dialog on a spawned thread. If you want the dialog to appear modal to the form that opened it, you'll need to do some extra work to trap the handle of the opened dialog so that you can set focus to it when the parent form is activated. An example is available at http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx.