Click here to Skip to main content
15,912,897 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Which runtimes are required Pin
Abhinav S3-May-10 19:33
Abhinav S3-May-10 19:33 
Questionneed clarification about gacutil.exe Pin
prasadbuddhika2-May-10 23:24
prasadbuddhika2-May-10 23:24 
AnswerRe: need clarification about gacutil.exe Pin
Peace ON2-May-10 23:51
Peace ON2-May-10 23:51 
AnswerRe: need clarification about gacutil.exe Pin
Abhinav S3-May-10 7:54
Abhinav S3-May-10 7:54 
AnswerRe: need clarification about gacutil.exe Pin
prasadbuddhika6-May-10 0:54
prasadbuddhika6-May-10 0:54 
QuestionHow to add launch condition for my deployment to insure that the required registry tree exists in client machine i.e AutoCAD 2008 or higher version is installed ? Pin
TARAK NATH ROY2-May-10 21:34
TARAK NATH ROY2-May-10 21:34 
QuestionJquery in asp.net Pin
siya patel2-May-10 4:53
siya patel2-May-10 4:53 
AnswerRe: Jquery in asp.net Pin
Pete O'Hanlon2-May-10 5:26
mvePete O'Hanlon2-May-10 5:26 
Questionhttp Basic authentication(RFC2617) Pin
kunaltilak29-Apr-10 20:27
kunaltilak29-Apr-10 20:27 
AnswerRe: http Basic authentication(RFC2617) Pin
T M Gray3-May-10 7:44
T M Gray3-May-10 7:44 
GeneralRe: http Basic authentication(RFC2617) Pin
The Man from U.N.C.L.E.4-May-10 3:29
The Man from U.N.C.L.E.4-May-10 3:29 
QuestionMessage Removed Pin
29-Apr-10 4:52
professionalN_tro_P29-Apr-10 4:52 
AnswerRe: lock Pin
Luc Pattyn29-Apr-10 4:55
sitebuilderLuc Pattyn29-Apr-10 4:55 
GeneralMessage Removed Pin
29-Apr-10 5:02
professionalN_tro_P29-Apr-10 5:02 
GeneralRe: lock Pin
Luc Pattyn29-Apr-10 5:07
sitebuilderLuc Pattyn29-Apr-10 5:07 
GeneralMessage Removed Pin
29-Apr-10 5:38
professionalN_tro_P29-Apr-10 5:38 
GeneralRe: lock Pin
Luc Pattyn29-Apr-10 5:48
sitebuilderLuc Pattyn29-Apr-10 5:48 
GeneralMessage Removed Pin
29-Apr-10 5:50
professionalN_tro_P29-Apr-10 5:50 
GeneralRe: lock PinPopular
Luc Pattyn29-Apr-10 5:57
sitebuilderLuc Pattyn29-Apr-10 5:57 
GeneralMessage Removed Pin
29-Apr-10 6:07
professionalN_tro_P29-Apr-10 6:07 
GeneralRe: lock Pin
Covean29-Apr-10 6:23
Covean29-Apr-10 6:23 
GeneralRe: lock Pin
Alex Manolescu29-Apr-10 11:34
Alex Manolescu29-Apr-10 11:34 
GeneralRe: lock Pin
Dave Kreskowiak29-Apr-10 7:01
mveDave Kreskowiak29-Apr-10 7:01 
GeneralRe: lock Pin
Covean29-Apr-10 6:13
Covean29-Apr-10 6:13 
In addition to Luc's postings.
In your example it really doesn't matter if you use the collection itself or create an object for locking.

But how about this example (I just typed it here, not tested, hope you got what I mean):

private object m_objVideoPlayerSync = new object();
private List<string> m_lstVideos = new List<string>();
private EnumPlaybackState m_playbackState = EnumPlaybackState.Stopped;

private void AddVideoToList(string szVideoFileName)
{
    lock(m_objVideoPlayerSync)
    {
        m_lstVideos.Add(szVideoFileName);
    }
}

private void PlayNextVideo()
{
    lock(m_objVideoPlayerSync)
    {
        string szVideo = m_lstVideos[0];
        m_lstVideos.RemoveAt(0);
        m_playbackState = EnumPlaybackState.Playback;
        StartPlayBack();
    }
}

private void StopVideo()
{
    lock(m_objVideoPlayerSync)
    {
        StopPlayBack();
        m_playbackState = EnumPlaybackState.Stopped;
    }
}


In the function StopVideo() locking the list of videos would be crude, cause you never do anything on this list, but you need some object to lock.
But doing 2 locks in PlayNextVideo() like

string szVideo = string.Empty;
lock(m_lstVideos)
{
    szVideo = m_lstVideos[0];
    m_lstVideos.RemoveAt(0);
}

lock(m_objVideoPlayerSync)
{
    m_playbackState = EnumPlaybackState.Playback;
    StartPlayBack();
}


raises the chance that two threads, both starting the function PlayNextVideo() at the same, would play the same video.
In my first example this would never happen!

Here are some guidelines from MSDN how to use and not to use locks:


In general, avoid locking on a public type, or instances beyond your code's control. The common constructs lock (this), lock (typeof (MyType)), and lock ("myLock") violate this guideline:

lock (this) is a problem if the instance can be accessed publicly.

lock (typeof (MyType)) is a problem if MyType is publicly accessible.

lock(“myLock”) is a problem since any other code in the process using the same string, will share the same lock.

Best practice is to define a private object to lock on, or a private static object variable to protect data common to all instances.



I hope this helps you to understand locking a little bit better.
Greetings
Covean

GeneralRe: lock Pin
supercat929-Apr-10 9:11
supercat929-Apr-10 9:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.