Unity3d handling multiple Webclient.DownloadAsync in a nice way - multithreading

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?

Related

In an isomorphic Redux app, is it better practice to keep API calls small, or to send over all information in one go?

I am building a sports data visualization application with server-side rendering in React (ES6)/Redux/React-Router-Redux. At the top, there is a class-based App component, and there are two different class-based component routes. (everything under those is a stateless functional component), structured as follows:
App
|__ Index (/)
|__ Match (/match/:id)
When a request is made for a given route, one API call is dispatched, containing all information for the given route. This is hosted on a different server, where we're using Restify and Sequelize ORM. The JSON object returned is roughly 12,000 to 30,000 lines long and takes anywhere from 500ms to 8500ms to return.
Our application, therefore, takes a long time to load, and I'm thinking that this is the main bottleneck. I have a couple options in mind.
Separate this huge API call into many smaller API calls. Although, since JS is single-threaded, I'd have to measure the speed of the render to find out if this is viable.
Attempt lazy loading by dispatching a new API call when a new tab is clicked (each match has several games, all in new tabs)
Am I on the right track? Or is there a better option? Thanks in advance, and please let me know if you need any more examples!
This depends on many things including who your target client is. Would mobile devices ever use this or strictly desktop?
From what you have said so far, I would opt for "lazy loading".
Either way you generally never want any app to force a user to wait at all especially not over 8 seconds.
You want your page send and show up with something that works as quick as possible. This means you don't want to have to wait until all data resolves before your UI can be hydrated. (This is what will have to happen if you are truly server side rendering because in many situations your client application would be built and delivered at least a few seconds before the data is resolved and sent over the line.)
If you have mobile devices with spotty networks connections they will likely never see this page due to timeouts.
It looks like paginating and lazy loading based on accessing other pages might be a good solution here.
In this situation you may also want to look into persisting the data and caching. This is a pretty big undertaking and might be more complicated than you would want. I know some colleagues who might use libraries to handle most of this stuff for them.

In an Asynchronous Pluggable Protocol thread - how can this be associated/resolved to a particular COM object implementing ::SetSite()?

In IE11, multiple threads will fire into the APP when visiting a site such as www.yahoo.com - and only a very small subset of those threads can be associated with the browser window (by querying the service provider, et cetera) that was passed through in the ::SetSite() call when the tab was created.
This doesn't work for the majority of threads on modern sites (e.g. www.evernote.com)
How can I figure out which IE tab a thread is acting on the behalf of - or is this impossible?
I would love to know that there's some way to match those threads up to the pUnkSite passed into ::SetSite() (or something similar) - but that seems unlike the IE model as I've experienced it so far.
Thanks.
Try the Switch/Continue trick. Your APP would call IInternetProtocolSink::Switch like this, e.g. in Start:
PROTOCOLDATA data = {0};
data.grfFlags = PD_FORCE_SWITCH; // important
pProtocolSink->Switch(&data);
Eventually, the client will turn around and call IInternetProtocol::Continue on you, on the main UI thread. Once on the main thread, things like IServiceProvider::QueryService(IID_IWindowForBindingUI) should work, and help you connect back to the requesting browser and/or document.

Output something once per request

I'm creating a module that exports a method that can may be called several times by any code in node.js using it. The method will be called usually from views and it will output some html/css/js. Some of this html/css/js however only needs to be output once per page so I'd like to output it only the first time the module is called per request. I can accomplish doing it the first time the module is called ever but again the method of my module can be called several times across several requests for the time the server is up so I specifically want to run some specific code only once per page.
Furthermore, I want to do this while requiring the user to pass as little to my method as possible. If they pass the request object when creating the server I figure I can put a variable in there that will tell me if my method was already called or not. Ideally though I'd like to avoid even that. I'm thinking something like the following from within my module:
var http = require('http');
http.Server.on('request', function(request, response){
console.log('REQUEST EVENT FIRED!');
// output one-time css
});
However this doesn't work, I assume it's because I'm not actually pointing to the Server emitter that was/may have been created in the script that was originally called. I'm new to node.js so any ideas, clues or help is greatly appreciated. Thanks!
Setting a variable on the request is an accepted pattern. Or on the response, if you don't even want to pass the request to your function.
One more thing you can do is indeed, like you write, have the app add a middleware and have that middleware either output that thing.
I'm not sure if I completely understand your "problem" but what you are trying to achieve seems to me like building a web application using Node.js. I think you should use one of the web frameworks that are available for Node so you can avoid reinventing the wheel (writing routing, static files serving etc. yourself).
Express framework is a nice place to start. You can find tons of tutorials around the internet and it has strong community: http://expressjs.com/

Multithreading using Wcf

HI i am new to the the whole programming thing, i have been given a task to multithread 4 stored procedures where each thread runs asynchronously so that the user can get output real quick i have to do it using WCF can anyone help me out with this. Initially what i am trying to do is taking each procedure and getting how much time it takes to execute using parametrizedthreadstart, but i am not sure how to go about it.
Considering you are new to the whole programming thing, you can follow these very basic steps to get thing done.
Create a new WCF service.
Add 4 methods each calling one stored procedure.
Add parameters to the methods which are required by stored procedures.
For Example if your stored procedure is - MySP(varchar name) then your WCF method will
be - MySP(string name);
Now depoly your service in IIS or windows service or Console App or wherever you want.
Create a client application, again it could be anything ConsoleApp or Win Form etc.
Add a reference to your service.
Instantiate service class and call there Async version. By Async I mean there you'll
see all of the four methods with Async attached.
For Example you will find your MySP(string name) method as MySPAsync(string name)
Also there will be MySPCompleted event, subscribe to it.
Now all of your methods are running asynchronously whenever they finish execution they'll call your subscribed methods.
I hope this helps you get started :)
There are a couple of different ways to do this. At the highest level, you can place each service request in it's own service endpoint. This could be defining endpoints for each method, or if you are hosting in IIS, placing each service it's own website. At the lower level, you could define callbacks for each method so that WCF will not block while the method calls are taking place.

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