multithreadingnpapibrowser-pluginwindowless

Wait for lengthy operation without browser hang in NPAPI windowless plugin


I've written a windowless NPAPI plugin, and I am going to perform some long lasting operation (e.g.send a http post request with image data) in a plugin function called by web browser JavaScript. Web browser must wait for plugin's worker thread to complete its job before doing anything else.

To prevent web browser from hanging, I create a worker thread for the lengthy operation in my plugin function.

My question is that how to properly wait the thread to complete in the plugin function, and without browser hanging at the same time?

The following code seems only works in window mode applications as far as I know.

    // on Main thread
    while (WaitForSingleObject(hWorkerThread, 100) ==  TIMEOUT)
    {
        while (PeekMessage(...))
        {
            TranslateMessage(...);
            DispatchMessage(...);
        }
    }

Any help will be really appreciated.


Solution

  • To not hang the browser, you really should return from your plugin function immediately and non-blockingly do the work in background thread. When it is completed, you can invoke a callback to notify the content JavaScript.

    Every workaround like spinning message loops is just that - a workaround - and not guaranteed to work across browser-versions. The only proper solution is to do the work non-blockingly.
    You should always be able to handle your concerns without blocking the whole browser, like not processing any further actions on your web page while waiting for completion of the operation.