我有一个可以访问XL并在单击按钮时创建图的UI。
因为XL操作需要一段时间,所以我为每个调用将它们运行在单独的线程上,所以程序运行平稳。
我使用Synclock来防止新线程在上一次调用完成之前启动例程。
问题在于,当我多次单击UI并让所有线程在synclock块中等待时,它们将以随机顺序执行。
是否有一种聪明的方法来控制线程进入同步锁的顺序?我可以想出一种使用全局线程列表或类似方法的困难方法。
Public Class Main_UI
Private myXLapp As New XL_OPERATIONS
Private Sub button_click( ...) handles button_click_event
Dim new_params as New Object
new_params =get_new_measurement_from_instruments() '<-- gets new data on button click
myXLapp.write_XL_plot(params) '<-- makes XL plot without holding up the program
End Sub
End Class
Public Class XL_OPERATIONS
Public Sub New()
End Sub
'********* EDITED CODE ****** EDITED CODE ****** EDITED CODE ******
'*******************************************************************
'*******************************************************************
Private static XLwr_queue as New Queue()
Private static XLwr_thread as New Thread(Address of Send_Queued_Plots_to_XL())
'--- adds new params to queue to be used by thread below ----
'
Property write_XL_plot()
Set(params as Object)
XLwr_queue.Enqueue(params)
if XLwr_thread.ThreadState<> ThreadState.Running Then
XLwr_thread.start()
End Set
End Property
'---- creates XL plots in the order of params in Queue ---
'
Private Sub Send_Queued_Plots_to_XL()
For i as Integer=0 to XL_queue.Count
write_XL_plot_routine(XL_queue.Dequeue())
Next
End Sub
'*******************************************************************
'*******************************************************************
'********* ORIGINAL CODE ****** ORIGINAL CODE ****** ORIGINAL CODE ******
'*************************************************************************
'*************************************************************************
'----- starts a thread that writes plot to XL ----------
'
Public Sub write_XL_plot(params as object)
Dim thread as New Thread(
Sub()
write_XL_plot_routine(params as object)
End Sub
)
thread.start()
End Sub
'----- routine for making XL plot -----------------------
'
Private XLWRsynclock As New Object
Private Sub write_XL_plot_routine(params As object)
synclock XLWRsynclock
'makes XL plot
'takes a long time
End synclock
End Sub
'*************************************************************************
'*************************************************************************
End Class