English 中文(简体)
How to handle this Multithread situation and don t lock?
原标题:

I have this situation: a Form with a System.Timer in it (with AutoReset = False). The form has its main thread and the timer its own thread too (nothing new here).

When the user press a button I need to stop the timer, wait until the timer thread has stopped its execution and do something more.

On the other side, the timer updates an item at the form so BeginInvoke is used. The code looks like this:

Button Code:

 Private Sub ButtonStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonStop.Click
        SyncLock (m_stopLock)
            m_stopProcessTimer = True
            Threading.Monitor.Wait(m_stopLock)
        End SyncLock
          #Do more things here after the timer has end its execution and is stopped
    End Sub

Timer code:

Private Sub m_processTimer_Elapsed(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_processTimer.Elapsed
    Dim auxDelegate As EventHandler

    SyncLock (m_stopLock)
        If Not m_stopProcessTimer Then
            If Me.InvokeRequired Then
                auxDelegate = New EventHandler(AddressOf m_processTimer_Elapsed)
                Me.BeginInvoke(auxDelegate, New Object() {sender, e})
            Else
                DoFormStuf()
                m_processTimer.Start()
            End If
        Else
            Threading.Monitor.Pulse(m_stopLock)
        End If
    End SyncLock
End Sub

The point is that I wait the main thread to let the timer thread to end its work.

The problem is that this code deadlocks when the user clicks the button when the BeginInvoke is going to be called. How a simple thing like this one can be done? Looks like I cannot find a good solution to this problem :(

最佳回答

Don t use locks at all, just make sure to do everything on the UI thread, and you can guarantee that nothing will be corrupted. Remember that dispatcher items run on the UI thread, so you know that if you re doing everything either in a dispatcher item or an event handler, only one thing is executing at a time.

问题回答

1) Perhaps a little more code would be helpful. Do you create a new thread and put the timer ON that thread?

2) Have you tried using ManualResetEvent (.WaitOne() and .Set() instead?)

3) In your event, if invoke is required, you are re-calling your event again. Confusing...

4) Are you supposed to wait until the other thread is done? Then Thread.Join()?





相关问题
Is Shared ReadOnly lazyloaded?

I was wondering when I write Shared ReadOnly Variable As DataType = New DataType() Or alternatively Shared ReadOnly Variable As New DataType() Is it lazy loaded or as the instance initializes? ...

Entertaining a baby with VB.NET

I would like to write a little application in VB.NET that will detect a baby s cry. How would I get started with such an application?

Choose Enter Rather than Pressing Ok button

I have many fields in the page and the last field is a dropdown with list of values. When I select an item in a dropdown and press Enter, it doesn t do the "Ok". Instead I have to manually click on Ok ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

Set Select command in code

On button Click I want to Set the Select command of a Gridview. I do this and then databind the grid but it doesn t work. What am i doing wrong? protected void bttnView_Click(object sender, ...

Hover tooltip on specific words in rich text box?

I m trying to create something like a tooltip suddenly hoovering over the mouse pointer when specific words in the richt text box is hovered over. How can this be done?

热门标签