Click here to Skip to main content
16,003,654 members
Home / Discussions / C#
   

C#

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder12-Jul-09 22:36
cofounderChris Maunder12-Jul-09 22:36 
PinnedHow to get an answer to your question Pin
Chris Maunder10-Nov-05 16:31
cofounderChris Maunder10-Nov-05 16:31 
QuestionHow do I check for existance or clear the contents of a SharePoint List from C# code? Pin
Xarzu19hrs 17mins ago
Xarzu19hrs 17mins ago 
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();
  }
}


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 henington21hrs 56mins ago
charles henington21hrs 56mins ago 
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 
GeneralRe: how to get image Thumbnail without open it? Pin
Dave Kreskowiak11-Sep-24 3:17
mveDave Kreskowiak11-Sep-24 3:17 
GeneralRe: how to get image Thumbnail without open it? Pin
OriginalGriff11-Sep-24 4:03
mveOriginalGriff11-Sep-24 4:03 
GeneralRe: how to get image Thumbnail without open it? Pin
lmoelleb12-Sep-24 19:38
lmoelleb12-Sep-24 19:38 
AnswerRe: how to get image Thumbnail without open it? Pin
jschell12-Sep-24 13:08
jschell12-Sep-24 13:08 
Questionhow set cutom paper size? Pin
Le@rner28-Aug-24 0:38
Le@rner28-Aug-24 0:38 

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.