Click here to Skip to main content
16,017,606 members
Home / Discussions / C#
   

C#

 
AnswerRe: Game Pin
OriginalGriff25-Sep-24 19:42
mveOriginalGriff25-Sep-24 19:42 
AnswerRe: Game Pin
charles henington29-Sep-24 21:45
charles henington29-Sep-24 21:45 
QuestionHow do I check for existance or clear the contents of a SharePoint List from C# code? Pin
Xarzu19-Sep-24 7:58
Xarzu19-Sep-24 7:58 
AnswerRe: How do I check for existance or clear the contents of a SharePoint List from C# code? Pin
charles henington20-Sep-24 9:26
charles henington20-Sep-24 9:26 
GeneralRe: How do I check for existance or clear the contents of a SharePoint List from C# code? Pin
Xarzu20-Sep-24 12:07
Xarzu20-Sep-24 12:07 
GeneralRe: How do I check for existance or clear the contents of a SharePoint List from C# code? Pin
charles henington21-Sep-24 18:22
charles henington21-Sep-24 18:22 
QuestionWould this be a valid Extension? Pin
charles henington18-Sep-24 21:17
charles henington18-Sep-24 21:17 
AnswerRe: Would this be a valid Extension? Pin
Pete O'Hanlon18-Sep-24 21:39
mvePete O'Hanlon18-Sep-24 21:39 
From what I can see, the code you've provided isn't going to achieve what you might expect it to achieve. I'm going to break the logic down so you can see where the confusion lies.
  1. The await operator here does not appear to affect the locking behaviour. It's only waiting for the Task.Run to complete.
  2. Inside the Task.Run, we have a lock statement. You can think of this as running on a different thread from the caller.
  3. Each call to this method will create a new task, which will attempt to acquire the lock independently.
The end result is multiple calls to this method could run concurrently, each in its own task, defeating the purpose of the lock. The lock will still work within each individual task, but it won't prevent multiple tasks from running simultaneously. To my mind, a better version of this would be this:
C#
private static readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);

internal static async Task Lock<T>(this T lockObject, Action DoAction) where T : class
{
  try
  {
    await _semaphore.WaitAsync();
    DoAction();
  }
  finally
  {
    _semaphore.Release();
  }
}


GeneralRe: Would this be a valid Extension? Pin
charles henington25-Sep-24 17:14
charles henington25-Sep-24 17:14 
AnswerRe: Would this be a valid Extension? Pin
Richard Deeming18-Sep-24 21:40
mveRichard Deeming18-Sep-24 21:40 
GeneralRe: Would this be a valid Extension? Pin
charles henington19-Sep-24 5:19
charles henington19-Sep-24 5:19 
GeneralRe: Would this be a valid Extension? Pin
charles henington25-Sep-24 17:05
charles henington25-Sep-24 17:05 
QuestionVisual Studio Weirdness Pin
Richard Andrew x6415-Sep-24 12:38
professionalRichard Andrew x6415-Sep-24 12:38 
AnswerRe: Visual Studio Weirdness Pin
jschell17-Sep-24 12:37
jschell17-Sep-24 12:37 
GeneralRe: Visual Studio Weirdness Pin
Richard Andrew x6418-Sep-24 11:11
professionalRichard Andrew x6418-Sep-24 11:11 
QuestionConverting ulaw to alaw Pin
charles henington14-Sep-24 14:26
charles henington14-Sep-24 14:26 
AnswerRe: Converting ulaw to alaw Pin
trønderen14-Sep-24 18:01
trønderen14-Sep-24 18:01 
GeneralRe: Converting ulaw to alaw Pin
charles henington15-Sep-24 13:39
charles henington15-Sep-24 13:39 
AnswerRe: Converting ulaw to alaw Pin
Peter_in_278015-Sep-24 14:07
professionalPeter_in_278015-Sep-24 14:07 
AnswerRe: Converting ulaw to alaw Pin
jeron115-Sep-24 14:11
jeron115-Sep-24 14:11 
GeneralRe: Converting ulaw to alaw Pin
charles henington18-Sep-24 16:10
charles henington18-Sep-24 16:10 
Questionhow to get image Thumbnail without open it? Pin
Le@rner10-Sep-24 18:34
Le@rner10-Sep-24 18:34 
AnswerRe: how to get image Thumbnail without open it? Pin
Richard Deeming10-Sep-24 21:28
mveRichard Deeming10-Sep-24 21:28 
AnswerRe: how to get image Thumbnail without open it? Pin
OriginalGriff10-Sep-24 23:05
mveOriginalGriff10-Sep-24 23:05 
AnswerRe: how to get image Thumbnail without open it? Pin
Le@rner10-Sep-24 23:29
Le@rner10-Sep-24 23:29 

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.