?
Solved

use lock in thread

Posted on 2005-04-03
3
Medium Priority
?
148 Views
Last Modified: 2010-04-23
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
Comment
Question by:bwz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 14

Expert Comment

by:amyhxu
ID: 13691918
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
 

Author Comment

by:bwz
ID: 13695409
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
 
LVL 4

Accepted Solution

by:
nachiket earned 300 total points
ID: 13781404
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question