@@ -310,6 +310,24 @@ Sleep(int milli)
310
310
#endif /* MS_WINDOWS */
311
311
#endif /* WITH_THREAD */
312
312
313
+ /* Wait up to 1s for the mainloop to come up. */
314
+
315
+ static int
316
+ WaitForMainloop (TkappObject * self )
317
+ {
318
+ int i ;
319
+ for (i = 0 ; i < 10 ; i ++ ) {
320
+ if (self -> dispatching )
321
+ return 1 ;
322
+ Py_BEGIN_ALLOW_THREADS
323
+ Sleep (100 );
324
+ Py_END_ALLOW_THREADS
325
+ }
326
+ if (self -> dispatching )
327
+ return 1 ;
328
+ PyErr_SetString (PyExc_RuntimeError , "main thread is not in main loop" );
329
+ return 0 ;
330
+ }
313
331
314
332
315
333
static char *
@@ -1138,11 +1156,8 @@ Tkapp_Call(PyObject *_self, PyObject *args)
1138
1156
marshal the parameters to the interpreter thread. */
1139
1157
Tkapp_CallEvent * ev ;
1140
1158
PyObject * exc_type , * exc_value , * exc_tb ;
1141
- if (!self -> dispatching ) {
1142
- PyErr_SetString (PyExc_RuntimeError ,
1143
- "main thread is not in main loop" );
1159
+ if (!WaitForMainloop (self ))
1144
1160
return NULL ;
1145
- }
1146
1161
ev = (Tkapp_CallEvent * )ckalloc (sizeof (Tkapp_CallEvent ));
1147
1162
ev -> ev .proc = (Tcl_EventProc * )Tkapp_CallProc ;
1148
1163
ev -> self = self ;
@@ -1427,11 +1442,8 @@ var_invoke(PyObject *_self, char* arg1, char* arg2, char* arg3, int flags,
1427
1442
the call to the interpreter thread, then wait for
1428
1443
completion. */
1429
1444
1430
- if (!self -> dispatching ) {
1431
- PyErr_SetString (PyExc_RuntimeError ,
1432
- "main thread is not in main loop" );
1445
+ if (!WaitForMainloop (self ))
1433
1446
return NULL ;
1434
- }
1435
1447
ev -> cond = NULL ;
1436
1448
ev -> ev .proc = (Tcl_EventProc * )var_proc ;
1437
1449
Tkapp_ThreadSend (self , (Tcl_Event * )ev , & ev -> cond , & var_mutex );
@@ -1943,11 +1955,8 @@ Tkapp_CreateCommand(PyObject *_self, PyObject *args)
1943
1955
}
1944
1956
1945
1957
if (self -> threaded && self -> thread_id != Tcl_GetCurrentThread () &&
1946
- !self -> dispatching ) {
1947
- PyErr_SetString (PyExc_RuntimeError ,
1948
- "main thread is not in main loop" );
1958
+ !WaitForMainloop (self ))
1949
1959
return NULL ;
1950
- }
1951
1960
1952
1961
data = PyMem_NEW (PythonCmd_ClientData , 1 );
1953
1962
if (!data )
@@ -2423,12 +2432,22 @@ Tkapp_WantObjects(PyObject *self, PyObject *args)
2423
2432
return Py_None ;
2424
2433
}
2425
2434
2435
+ static PyObject *
2436
+ Tkapp_WillDispatch (PyObject * self , PyObject * args )
2437
+ {
2438
+
2439
+ ((TkappObject * )self )-> dispatching = 1 ;
2440
+
2441
+ Py_INCREF (Py_None );
2442
+ return Py_None ;
2443
+ }
2426
2444
2427
2445
2428
2446
/**** Tkapp Method List ****/
2429
2447
2430
2448
static PyMethodDef Tkapp_methods [] =
2431
2449
{
2450
+ {"willdispatch" , Tkapp_WillDispatch , METH_NOARGS },
2432
2451
{"wantobjects" , Tkapp_WantObjects , METH_VARARGS },
2433
2452
{"call" , Tkapp_Call , METH_OLDARGS },
2434
2453
{"globalcall" , Tkapp_GlobalCall , METH_OLDARGS },
0 commit comments