Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 149
  • Last Modified:

use lock in thread

hi,

i want to use Interlocked in a function that called from a thread,
to prevent case that two threads will run at the same time.
this is my code:

    Public Sub Process1()
        If Threading.Interlocked.Exchange(UsingResorce, 1) = 0 Then
            iLog = New Log(iProgramName, iDeleteLogAfterDef)
            iLog.WriteToLog("Hello From process 1 " & Now)
        End If
    End Sub

the process1 fun called from this:

    Private Sub OnTimer(ByVal State As Object)
        Dim iMessageCenter As New MessageCenter
        Dim UsingResorce As Integer
        Select Case m_ThreadProcessNum
            Case eThreadProcess.Process1
                iMessageCenter.Process1()
            Case eThreadProcess.Process2
                iMessageCenter.Process2()
        End Select

and the thread call to onTimer like this (i'm use threading.timer):

            Dim callback As New Threading.TimerCallback(AddressOf OnTimer)

            myTimer = New Threading.Timer(callback, Nothing, 0, period)

i'm using it but it's not work , what can i do

thank's
0
bwz
Asked:
bwz
1 Solution
 
amyhxuCommented:
I don't understand -- if you don't want two threads run at the same time, why not just use one thread? Can you explain why you use two threads in this case?
Is it because you want to use threading.timer? Then you can change that to the timer you can find in the toolbox.
0
 
bwzAuthor Commented:
i need to use two thread for differenct cases
that run at the same time but not clash between them
(one reason is that i use the same log file in the threads and only one can write to the file
any time).
i hope it was more clear.

thank's
0
 
nachiketCommented:
U need to release the lock from Process1 after ur work is done

===================================
find the similar code below. which works fine
=========================================
Public Class MessageCenter

    Public Shared UsingResorce As Integer = 0
    Public Sub Process1()
        If Threading.Interlocked.Exchange(UsingResorce, 1) = 0 Then
            Console.WriteLine("Hello From process 1 " & Now.ToString())
            Threading.Interlocked.Exchange(UsingResorce, 0)
        End If
    End Sub

    Public Sub Process2()
        If Threading.Interlocked.Exchange(UsingResorce, 1) = 0 Then
            Console.WriteLine("Hello From process ##2## " & Now.ToString())
            Threading.Interlocked.Exchange(UsingResorce, 0)
        End If
    End Sub

    Private Shared Sub OnTimer(ByVal State As Object)
        Dim iMessageCenter As New MessageCenter
        Dim UsingResorce As Integer

        Dim rnd As New Random

        If rnd.Next(0, 2) = 0 Then
            iMessageCenter.Process1()
        Else
            iMessageCenter.Process2()
        End If
    End Sub

    Public Shared Sub Main()
        Dim callback As New Threading.TimerCallback(AddressOf OnTimer)

        Dim myTimer As New Threading.Timer(callback, Nothing, 0, 100)
        Console.ReadLine()
    End Sub

End Class
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now