Skip to content

Commit 73dd662

Browse files
author
Mihail Slavchev
committed
dispatch runScript on the main thread when needed
1 parent 359c0f1 commit 73dd662

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

src/src/com/tns/Platform.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,58 @@ public static Object runScript(File jsFile) throws NativeScriptException
215215

216216
if (jsFile.exists() && jsFile.isFile())
217217
{
218-
String filePath = jsFile.getAbsolutePath();
219-
result = runScript(filePath);
218+
final String filePath = jsFile.getAbsolutePath();
219+
220+
boolean isWorkThread = threadScheduler.getThread().equals(Thread.currentThread());
221+
222+
if (isWorkThread)
223+
{
224+
result = runScript(filePath);
225+
}
226+
else
227+
{
228+
final Object[] arr = new Object[2];
229+
230+
Runnable r = new Runnable()
231+
{
232+
@Override
233+
public void run()
234+
{
235+
synchronized (this)
236+
{
237+
try
238+
{
239+
arr[0] = runScript(filePath);
240+
}
241+
finally
242+
{
243+
this.notify();
244+
arr[1] = Boolean.TRUE;
245+
}
246+
}
247+
}
248+
};
249+
250+
boolean success = threadScheduler.post(r);
251+
252+
if (success)
253+
{
254+
synchronized (r)
255+
{
256+
try
257+
{
258+
if (arr[1] == null)
259+
{
260+
r.wait();
261+
}
262+
}
263+
catch (InterruptedException e)
264+
{
265+
result = e;
266+
}
267+
}
268+
}
269+
}
220270
}
221271

222272
return result;

0 commit comments

Comments
 (0)