Show a message dialog form outside the main UI thread - firemonkey

I want to create a dialog utility unity that could be called in an asynchronous way from different threads and show my dialog message on the active form, and I was sure that TDialogServiceAsync was the perfect way to do it but I can't call the MessageDialog method from outside the Main UI Thread.
Is it possible to achieve what I want without having to actually create a method in my main form that shows the dialog?
I'm developing for Windows right now but a method that could work on multiple plataforms would be appreciated.
Thanks in advance.

no, everything that touch the ui must be done in the main ui thread (quite logic). the only think you can do in your background thread
TThread.queue(nil,
procedure
begin
showdialog...
end);

Related

What is the best approach to handle a Outlook add-in Inspector UI contorl null reference exception caused by cancelling a long running async call?

When user opens Outlook email item, my Outlook add-in creates an new Inspector in Outlook.Inspectors collection. To fill out Inspector form region user controls, few long running async DB calls are called. Generally, user can close email/Inspector window at any time while the async calls are running. The problem is the form region Close event occurs before async call completion. As a result, when async call is completed, my Inspector code tries to update form region user control but, by this moment, the control is null and disposed in form region Close event. Hence, null reference exception is thrown.
I added some code in Inspector Close event where I set up an ad-hoc flag indicating that Inspector is closed. The flag value is checked up in a catch(NullReferenceException) block. If flag shows that email is closed, the null reference exception is ignored and a new OperationCancelledException is thrown to cancel other related running async calls. Although the code works, I am not sure that this is the best approach to handle the error this way. Firstly, although the flag indicates that email is closed, the null reference exception might not be neccessary related to disposed UI user control. Secondly, I need to add catch(NullRefernceException) block to try-catch block in all relevant methods.
If you've had a similar problem, would you be able to advise your solution, please?
Thank you.
Instead of throwing and handling exceptions, I'd recommend keeping a dictionary of opened items, so you could easily detect whether a particular window is still opened. For example, you may develop an inspector wrapper - see Implement a wrapper for inspectors and track item-level events in each inspector.

Xamarin MvvmCross - MvxFragment communicating with MvxActionBarActivity

I currently have a project in Xamarin and I am using MvvmCross. I have an MvxActionBarActivity that hosts MvxFragments. Now when I want to close the entire MvxActionBarActivity, the event begins in the Fragment and I want to tell the MvxActionBarActivity to close. Calling Close(this) in the fragment viewmodel is not working.
I have considered using MvxMessegner to send message from one viewmodel to the other but due to the relationship of a fragment and an activity I am wondering if there is a better way to do this.
Any help would be much appreciated.
Thank you!
When you want to close an Activity you need to call Finish(). This will close that activity and go back to that what is on the backstack.
If you want to call that from your viewmodel you should use a custom presenter: http://gregshackles.com/presenters-in-mvvmcross-navigating-android-with-fragments/

How to show a Carbon dialog as part of a plugin for an application with an existing event loop?

I'm writing a plugin for an application and need to use Carbon to show a dialog. I have everything set up including the event handler, but I cannot possibly call RunApplicationEventLoop() because this would stall the host application.
How can I fix this? Will I need to create a separate thread and call RunApplicationEventLoop() from there?
-Joe
What makes you think you need to call RunApplicationEventLoop? The host app is presumably running an event loop, probably either using RunApplicationEventLoop or NSApplicationMain. By the way, would your dialog be modal? Modal is easier.

How can I write an automated unit test of a GUI in Xcode?

I want to write a unit test of just the GUI part of my Cocoa application.
In the textbook unit test, there's a test framework and test case that calls the unit under test. All the code below that unit is mocked out. So, both the input and the output are controlled and monitored; only the code in the unit under test is tested.
I want to do the same thing where the unit under test is my GUI:
1) Set up some kind of framework where I can write code that will manipulate and inspect GUI controls.
2) Connect my GUI controls to mocks of my actual code, not to the real instances.
3) Run the test, which manipulates the controls and then checks the mock object to see whether the correct methods were called with the correct parameters and checks the GUI to see whether the responses from the mock object causes the correct changes in the widgets.
Anyone doing this? If so, how? Any ideas on how I could do this?
Thanks,
Pat
(Edit) To give a very specific example, I want to:
1) Write a test case that will select the menu item 'MyMenu' -> 'MyItem'. In this test case, I want to check to see that the method [AppDelegate doMyItem] gets called precisely once and that no other methods in AppDelegate get called.
2) Generate a mock object of AppDelegate. (I know how to do this)
3) Somehow (handwaving here) link my application so that a mock instance of AppDelegate is linked in instead of the real one.
4) Run the test. Watch it fail because 1) I haven't created MyMenu yet. 2) I haven't created MyItem yet. 3) I haven't done the IB work to connect MyItem to [AppDelegate doMyItem], or 4) because I haven't written the 'doMyItem' method yet.
5) Fix the above four issues (one at a time if I'm feeling really pedantic that day).
6) Run the test again and watch it succeed.
Does this make the question clear?
Two principles, two links:
Make the view as dumb as possible, with the passive view pattern: this makes GUI easier to test
Trust but verify: Trust Cocoa implementation of buttons, menus, ... But verify that target and action are correctly connected, that bindings are as expected.
Here are a couple of popular ways of doing this in general (should work with most if not all cocoa compatible languages).
1 - create a callback interface. One of the inputs when creating your GUI elements is an implementation of this interface. When there's a user interaction, the GUI element calls an update function on that interface. Have a real implementation and a test implementation.
2 - Use event-handlers. Register all of your GUI elements with one or more event-handlers, and have the GUI generate events on user interaction. Have an event handler interface with two implementations, again one for real use and one for testing.
Edit: whoops, missed requirement #1. Never done this with OSX specific controls, but in general there are two approaches.
1 - create a script or app that generates user-like input. Has the drawback of not being easy to actually inspect the GUI. You instead need to generate good test cases to make sure that everything that should be there is, and nothing extra is there.
2 - create an interface with a test implementation that replaces the rendering and interface layer. This is easier with libraries like SDL or directFB and less so with with things like the OSX API, win32 API, etc.
Edit: responding to edit in question.
In the case of your example, using a seperate testing app and event handlers here's how it'd look:
Your test application is a simple app or script that starts up your GUI and then generates mouse / keyboard events based on input files. As I've said, never done this in OSX (only QNX). With any luck you'll be able to generate mouse and keyboard events with the API, but you'll have to ask someone else if it's possible.
So create an input for your test-case. The test app will parse this to know what to do. It may be simple XML like this:
<testcase name="blah"><mouseevent x="120" y="175" type="click"/></testcase>
or whatever the mouse sequence may actually be.
When your script executes that command it will click the mouse on that button. Your event handler will pick up on this. But now you should be running your app with a --test flag or somesuch so that it's actually using the test event handler. Instead of doing whatever your app normally does, the test event handler can do some custom action. For instance it may do some of the normal actions (you still need the GUI to respond) and then send a message (via socket, pipe, whatever) to your test app.
Your test app will pick up this message and compare it to what it expects to see. So now maybe your testcase XML looks like this:
<testcase name="blah">
<mouseevent x="120" y="175" type="click"/>
<response>doMyItem() called</response>
</testcase>
If the response generated from the event handler is different, then the test case has failed. You can print out the actual response to help in debugging.
Have you looked into the accessibility framework? It should let one application inspect the UI of another application and generate user-like interaction events.
Accessibility Overview

Session 0 Isolation

Vista puts out a new security preventing Session 0 from accessing hardware like the video card, and the user no longer logs into session 0. I know this means that I cannot show the user a GUI, however, does that also mean I can't show one at all? The way my code is set up right now, it would be more work to make it command line only, however if I can use my existing code and just programmatically manage the GUI it would take a lot less code.
Is this possible?
The article from MSDN says this:
• A service attempts to create a user interface (UI), such as a dialog box, in Session 0. Because the user is not running in Session 0, he or she never sees the UI and therefore cannot provide the input that the service is looking for. The service appears to stop functioning because it is waiting for a user response that does not occur.
Which makes me think it is possible to have an automated UI, but someone told me that you couldn't use SendKeys with a service because it was disabled in Session 0.
EDIT: I don't actually need to show the user the GUI
You can show one; it just doesn't show up.
There is a little notification in the taskbar about there being a GUI window and a way to switch to it.
Anyway, there actually is a TerminalServices API command to switch active session that you could call if you really needed it to show up.
You can write a separate process which provides the UI for your service process. The communication between your UI and service process can be done in various ways (search the web for "inter process communication" or "IPC").
Your service can have a GUI. It's simply that no human will ever see it. As the MSDN quote suggests, a service can display a dialog box. The call to MessageBox won't fail; it just won't ever return — there won't be anyone to press its buttons.
I'm not sure what you mean by wanting to "manage the GUI." Do you actually mean pretending to send input to the controls, as with SendInput? I see no reason that it wouldn't be possible; you'd be injecting input into your own program's queue, after all, and SendInput's Vista-specific warnings don't say anything about that. But I think you'd be making things much more complicated than they need to be. Revisit the idea to alter your program to have no UI at all. (That's not the same as having a console program. Consoles are UI.)
Instead of simulating the mouse messages necessary to click a button, for instance, eliminate the middle-man and simply call directly the function that the button-click event would have called.

Resources