I have a UI that will access XL and create plots on button clicks.
Because the XL operations take a while I have them run on a separate thread for each call so the program runs smoothly.
I use Synclock to prevent new threads from starting the routine until the previous call is completed.
The problem is that when I click the UI a bunch of times and have all the threads waiting at the synclock block they execute in a random order.
Is there a smart way to control the order that the threads enter into the synclock? I can think of a hard way w/ using a global thread list or something along those lines.
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
Button
now, otherwise you couldn't be passing that data to the method you're executing. Simply put that same data into an object and add that object to the queue. Your one and only thread will then take those objects off the queue in the same order you added them.List(Of T)
in preference to anArrayList
, so you should use aQueue(Of T)
in preference to aQueue
. That said, in a multi-threaded environment, you should use aConcurrentQueue(Of T)
, which addresses the potential issues you're concerned about. Be sure to READ THE RELEVANT DOCUMENTATION before using it, to see what members it has and what each does.