0

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
6
  • 2
    If you're only executing one thread at a time then why have multiple threads in the first place? Why not just have one thread process a queue of items? Commented Jan 9, 2019 at 1:25
  • Because I don't know whats going to be in the next queued routine. I updated the code to better show this. Every button click gets new measurements and sends the result to be plotted by XL. If I click the button a bunch (faster than XL can make the plots) I'll have a bunch of threads waiting at the synclock block.
    – Tony
    Commented Jan 9, 2019 at 15:43
  • You know what the data is when you click the 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. Commented Jan 9, 2019 at 15:48
  • Ok, I see what your driving at. I made changes to the code where it says 'EDITED'. I think this is going to do what what I want it to. My only concern is updating the 'XLwr_queue' variable while its being used in the thread. I'm pretty green when it comes to threading but I always thought that bad things would happen if you make changes to variables while another thread is using them.
    – Tony
    Commented Jan 10, 2019 at 1:14
  • 1
    Just as you should use a List(Of T) in preference to an ArrayList, so you should use a Queue(Of T) in preference to a Queue. That said, in a multi-threaded environment, you should use a ConcurrentQueue(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. Commented Jan 10, 2019 at 1:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.