Executing a non ui blocking task in MPS and changing the model after task completion - mps

In Jetbrains MPS I want to execute a web request when the user hits a shortcut or an action from the context menu.
After successfully executing this request I want to show the user the response by manipulating the MPS model.
When I execute the web request within the UI Thread, the ui will be blocked until I get a response or the request times out.
This is of cause a bad ux experience.
How can I execute the request asynchronously?

The web request has to be executed in a separate thread - afaik MPS doesn't have full JDK8 support for ForkJoinPools s; I use the old fashioned approach in this example.
Furthermore, you need a reference to the ModelAccess in order to change the MPS model.
The model changing code has to executed in the UI Thread.
The SwinUtilities.invokeAndWait is used to execute the code within the UI thread and the modelAccess.executeCommand wraps the model changing code.
The this.asyncUpdate is a property of the concept and displayed by the concept editor.
The task in action:
<iframe src='https://gfycat.com/ifr/DisfiguredAdeptAmericanbittern' frameborder='0' scrolling='no' allowfullscreen width='640' height='213'></iframe><p> via Gfycat</p>

Related

Unity3d handling multiple Webclient.DownloadAsync in a nice way

I am writing a managed Unity3d plugin which handles some web requests, registration and downloading content from the web. I am using the .Net WebClient class and the corresponding Async calls for down/uploading content in a separate thread to avoid laggy UI.
I am now trying to find an easy way to retrieve the callbacks from those async calls on the "unity3d side". Something like the follwing line of code does not work, obviously:
MyManagedPlugin.RequestDataAsync(Callback);
I also would like to avoid any thread specific code on the unity3d side (meaning in the scripts) so that all the async calls are encapsulated in the managed plugin. I am not sure if that is possible at all since there need to be some kind of polling mechanism to on the main thread (unit3d) to check if a thread is done right?

ASP.NET WEBAPI MVC service return to page and keep processing asynchronous multthreading?

We have a WEBAPI service running on a windows asp.net MVC solution. There is a load method that takes about 40 minutes to complete and return status on the called page. During that time the browser window is tied up. What design options do we have if we want the web page to come back with submitted and the process to continue to run and complete. I don't care if page never shows complete, we can pull that from another status page.
I've done something similar in the past, even though in my case the delay was shorter - 40-50 seconds of loading of fresh data from multiple backend servers in a VPN. It was also in ASP.NET back then, but I believe that the approach is still feasible and you can get some ideas if I share my experience. I remember an old thread that I had favourited in the past and used the insight from it. You can check it out.
Here are some tips, but in short, because I don't remember the details anymore (excuse my google-assisted memory!):
You should start the task in a new thread and not wait for it in your main thread.
You should also make sure that the task is started only once and cannot be initiated infinite number of times by the user via refresh or via the UI. So, you better persist the state in the database, so at refresh, the new thread is created only if the database says that it has not been executed recently or it is not in progress.
Your page will be loaded and show its contents and you can display a .gif representing a progress bar, a loading wheel or something similar to the user.
The task you started will continue on the server. When it completes you can push and update the UI via ajax from within the code-behind to make the experience even smoother if you like.
On subsequent requests, you can just retrieve the state of your task from the database in order to display something like update completed at hh:mm:ss.
Hope this helps you and I wish you the best of luck!

Method(s) used to pass data to and from WebKit based browser

I'm using WebKitGTK+ ( WebKit ) application that will be a very simple web browser running in a Linux environment as a separate executable that will need to exchange data between another application. The system is described in the following image:
Example scenario:
Event such as smart card insertion detected in Backend processing Application
smart card data is read and passed to the WebKitGTK+ GUI Application
User interacts with "web page" displayed in WebKitGTK+ Application
Data is passed from WebKitGTK+ Application back to the Backend processing Application
What are the common methods of passing data between the WebKitGTK+ Application and the Backend processing Application?
Does `WebKitGTK+ provide some hooks to do this sort of thing? Any help at all would be appreciated.
I know this is a old question, but will try to answer it to best of my abilities so that it can be useful to someone else.
Webkit is basically rendering a html page. You can connect to various signals that webkit provides and act on these signals. More details at http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html
For your back end application, if something interesting happens, you can update page either by navigating to a new url, new page contents or just by setting text of desired element. Webkit provides DOM api functions and all of these are possible.
It becomes interesting when getting data from webkit and sending it to your back end system. Again, this depends upon your specific design, but generally you can hook up signals such as navigation signals when user clicks on a button and get contents. Another alternative is to use alert handler and simply use javascript alert and process the alert data on backend side.
Shameless plug for example : https://github.com/nhrdl/notesMD. It uses a simple database backend, probably can be good use case as it sends data back and forth between database and webpage. Even manipulates the links so that desired actions take place.

Let an server-side Beanshell script interact with the user in a JavaServer Faces environment

first I have to admit this might be a pretty specific problem. In a JavaServer Faces Application I want to be able to let a script run on the server-side interact with the user.
To give you more of a detail I give you an example of what I want to do:
In my application a user can input data and on the server side there is a script which can check/modify this data. I don't know the exact script because the user is able to make a custom one. All in all so far there is no problem, but in the script it is possible to do interactions with the user in the way of a question dialog. In the following example, the further script execution depends on the answer of the user.
...
if( inputData.equals("Something") ){
if( askUser("Question?","Some question...") )
{
doSomething();
}
else
{
doSomethingElse();
}
}
...
As you can see the method "askUser" has to wait for the result before the script can continue. The script itself is a Beanshell-script and the method "askUser" is a Java method which is called by the Beanshell interpreter.
I had in my mind to use threads and Java synchronization methods to let the script pause, but thread creation is not allowed by EJB. Still I tried that way, but it failed due to the FacesContext not being available in user-created threads.
I hope I could make my intention clear and you have any idea how this could be done... Thanks in advance. :)
Your question demonstrates a complete misunderstanding of how not only JSF technology works, but also the inherently stateless nature of HTTP communication and web servers.
Typically in a desktop based application this simple algorithm you listed above can be easily implemented, probably with modal dialogs and events. Web applications do not work this way because they are inherently stateless. A user or browser agent makes an HTTP request to a web server, the server processes this request and formulates a response, then returns that HTTP response back to the browser agent to be rendered. Typically this data can be textual, HTML, XML, Javascript, CSS, images or even a file.
The problem with incorporating the algorithm above on a server is that the functionality for askUser cannot possibly be done by the server, because the server cannot just ask the user a question and wait for the response. That would be backwards. The user is the client and asks questions of the server.
So how is this common problem solved in a web application? In JSF, the server can send as a response a Javascript to the browser that will allow the browser to ask the question, then when the user makes the choice, the Javascript can then post back the selection of the user to the server in an HTTP request. If that selection involves any changes to the DOM or page elements at all then that information will be sent in the response.
JSF bridges the gap between desktop and web applications by allowing giving the illusion of desktop and event driven functionality to an otherwise stateless technology, and it does this by having an event lifecycle on the serverside and using Ajax to tightly control changes to document elements.
A good tutorial on JSF technology can be found here, however I recommend becoming more familiar with HTTP and Java Servlets before you delve too deep: http://www.mkyong.com/tutorials/jsf-2-0-tutorials/

How does calling an updating UI thread from a Work thread works?

If i have a function in a thread that is processing some data, then it calls a callback function to update the status in the UI.
If the UI takes time to process the callback function then it is not so much usefull.
Instead of just handling the update in the callback function, should it send some kind of message to the UI that way it doesnt block?
I mean when in the processing function and i call the update status function, this should return immediately to the procesing function, then in the update it can wait all it wants for the UI update to finish.
Or do I need a 3rd thread to handle the sending update data to the UI ?
Usually there's a way of posting the callback to the UI thread without blocking.
For instance:
On Win32 there's PostMessage
In .NET Windows Forms there's Control.BeginInvoke (which implements ISynchronizeInvoke.BeginInvoke)
In Java there's EventQueue.invokeLater
I'm sure if you look at the docs for the UI toolkit you're using, you'll find something similar.
In .NET (WinForms, WPF, Silverlight) you just need to use the Dispatcher object on the Thread of the UI to call the update method for the User Interface.
Dispatchers can be called either synchronously (using Invoke) either async (using BeginInvoke/EndInvoke). Please note that in .NET there is a requirement to call EndInvoke for each BeginInvoke (becuase .NET doesn't gives you the warranty that the async handles will be freed), so Fire and Forget isn't an option by default (unless you implement your own FireAndForget)
Mapad posted a link to on UI and threads here which you may find useful. You didn't mention which UI toolkit and which language so I can't give you any specifics.

Resources