TIMER ', כיצד עובד? ונעילות LOCK

yosi44441

New member
TIMER ', כיצד עובד? ונעילות LOCK

אני משתמש ב TIMER על מנת לבצע פעולה מסויימת כל X שניות
לדוגמא
קוד:
Timer _timer = new System.Timers.Timer(1000);
_timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
void timer_Elapsed(object sender, ElapsedEventArgs e)
{

}

ולכן הפעולה timer_Elapsed תתבצע כל שניה
כמה שאלות על הנושא הזה שלא ברורות לי
1- הTIMER פועל ב THREAD נפרד? אם כן במידה והגיע הזמן של הפונקציה לפעול שוב אבל הקודמת לא הסתיימה יפתח עוד THREAD ויפעלו במקביל או שהקריאה הבאה תתעכב?

2- איך אפשר להגדיר שאכן יהיה קריאה כל שניה לפונקציה הנ"ל אבל אם היא לא הסתיימה לא יהיה קריאה שוב?

3- למה הפונקציה שפועלת כל X זמן מקבלת object sender, ElapsedEventArgs e
מה כבר יהיה שם?


לגבי LOCK
כאשר אני עושה LOCK על OBJECT כלשהו ועושה קטע
קוד:
object obj=new object()
lock (obj)

{


}
האם ה LOCK ינהל תור על ה OBJ הזה? יש מקום להרבה THREAD לחכות עליו לפי התור או רק ל 1?
 

Royi Namir

New member
מממ

System.Timers wraps threading.timer :

http://referencesource.microsoft.com/#System/services/timers/system/timers/Timer.cs,37

Albahari says in his book :

The following use the thread pool implicitly:
• WCF, Remoting, ASP.NET, and ASMX Web Services
application servers
System.Timers.Timer and System.Threading.Timer
• The parallel programming constructs that we describe in
Chapter 23
• The (now redundant) BackgroundWorker class
• Asynchronous delegates (also now redundant)



In order to do what you want to do - you have to set a timer for one call , with no repeat and at the end of it - start it again :
https://stackoverflow.com/a/684208/859154


Regarding :ElapsedEventArgs and object : this is a convention for all based events. It also exposes other useful timer related stuff :
http://referencesource.microsoft.co...m/timers/ElapsedEventArgs.cs,fa59a445f56b7851

Regarding lock - there will be a queue. ( not in the length of one but dependes of number of threads which wants to enter).
 
למעלה