0

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.

1 Answer 1

1

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.

1
  • Yep; blocking on the main thread is a big no-no in plugin land
    – taxilian
    Nov 16, 2013 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.