SharedValue Win32Host::InvokeMethodOnMainThread( SharedKMethod method, const ValueList& args, bool synchronous) { Win32Job* job = new Win32Job(method, args, synchronous); if (thread_id == GetCurrentThreadId() && synchronous) { job->Execute(); } else { Poco::ScopedLock<Poco::Mutex> s(this->GetJobQueueMutex()); this->jobs.push_back(job); // Enqueue job } // send a message to tickle the windows message queue PostThreadMessage(thread_id, tickleRequestMessage, 0, 0); if (!synchronous) { return Value::Undefined; // Handler will cleanup } else { // If this is the main thread, Wait() will fall // through because we've already called Execute() above. job->Wait(); // Wait for processing SharedValue r = job->GetResult(); ValueException e = job->GetException(); delete job; if (!r.isNull()) return r; else throw e; } }