PDA

View Full Version : مشکل با تایمر و هنگ کردن برنامه



c-sharp_South
پنج شنبه 01 تیر 1391, 08:20 صبح
سلام.
من یک برنامه نوشتم که در اون از 10 تایمر استفاده شده و با زدن Button هر ده تایمر فعال شده.
interval تایمر ها رو هم اینجوری زدم حدودا:
200 350 460 700 880 1000 1220 ...
برنامه بعد از زدن Button بعد از سه چهار ثانیه هنگ میکنه به طوری که
حتی بستن برنامه هم به سختی انجام میشه.
میخواستم بپرسم راهی برای جلو گیری از این هنگ کردن است؟
یا راه بهتری را برای کاری که من خوام انجام بدم سراغ دارید؟
ممنون میشم راهنماییم کنین
یا حق

kebriya
پنج شنبه 01 تیر 1391, 08:47 صبح
قراره با این 10 تایمر چیکار کنی؟

Saeed_m_Farid
پنج شنبه 01 تیر 1391, 11:39 صبح
سلام.
من یک برنامه نوشتم که در اون از 10 تایمر استفاده شده و با زدن Button هر ده تایمر فعال شده.
interval تایمر ها رو هم اینجوری زدم حدودا:
200 350 460 700 880 1000 1220 ...
برنامه بعد از زدن Button بعد از سه چهار ثانیه هنگ میکنه...
...یا راه بهتری را برای کاری که من خوام انجام بدم سراغ دارید؟

همونطورکه دوستمون گفت، کاری که شما دورن این تایمرها انجام میدین، مهمه و به احتمال زیاد همون هست که باعث هنگ کردن سیستم میشه.
اولاً چند نوع تایمر داریم : Threading.Timer (http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx) ، Timers.Timer (http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx) و Windows.Forms.Timer (http://msdn.microsoft.com/en-us/magazine/cc164015.aspx#S1)


http://i.msdn.microsoft.com/cc164015.fig01(en-us).gif

Windows.Forms.Timer فقط برای کارهایی که یک تایمر براتون کافیه و استفاده زیادی از کنترل های روی فرم میشه، مورد استفاده قرار میگیره (معمولاً!) ولی Timers.Timer رو می تونید تو هرکلاسی استفاده کنید و با توجه به اینکه اونهم از ComponentModel.Component منشعب شده، به شما اجازه میده که به کامپوننت ها دسترسی داشته باشید و می تونید ازش تو ویندوز فرم و ... استفاده کنید.
به احتمال زیاد شما دارین کارهای سیستمی یا سخت افزاری/الکترونیکی و ... میکنید والّا اینهمه تایمر برای یه پروژه ویندوز فرم غیر معمول هست؛ اگه اینطور هست ادامه مطلب رو بخونید، والا همون دومی رو استفاده کنید.
و اما! اگه شما با عناصر روی فرم کاری ندارین بهتره با Threading.Timer کار کنید، این یکی تا حدودی پیچیده تر هست، اما سطح بالاتری از کنترل رو به شما میده و بهترین گزینه (در صورت عدم استفاده از BachgroundWorker و Callback و ... که خیلی در موردشون صحبت کردیم و اگه لازم شد، بازم صحبت میکنم!) برای یک برنامه نویس سیستم (Embedded، الکترونیک، سخت افزار و ...) هست؛ و خوب بالطبع نسبت به امکاناتی که در اختیار یه برنامه نویس حرفه ای قرار میده، پیچیدگی بیشتری هم خواهد داشت! اگه احساس می کنید که به اون نیاز خواهید داشت، بهتره این مقاله رو بخونید:

Comparing the Timer Classes in the .NET Framework Class Library (http://msdn.microsoft.com/en-us/magazine/cc164015.aspx)

http://i.msdn.microsoft.com/cc164015.fig02(en-us).gif
تو ویرایش سوم کتاب CLR via C#‎‎‎‎‎‎‎ (http://www.amazon.com/CLR-via-C-Third-Pro-Developer/dp/0735627045)‎‎‎ نوشته جفری ریچر؛‌ در این مورد توضیحات کوتاهی داده که عین مطلبش رو براتون میذارم، چون میدونم که 99% برنامه نویسای ما توانایی/رغبت/حوصله و ... خرید اینجور کتاب ها رو ندارن (متاسفانه تو ترجمه فارسی رایگان و روان (http://barnamenevis.org/showthread.php?313713) که جناب آقای محسن افشین از این کتاب کردند، این فصول نیستند و تا فصل 20 ترجمه شده و الّا ارجاع میدادم به اونجا و فصل 26)


So Many Timers, So Little Time
Unfortunately, the FCL actually ships with several timers, and it is not clear to most programmers what makes each timer unique. Let me attempt to explain:
System.Threading’s Timer class This is the timer discussed in the previous section, and it is the best timer to use when you want to perform periodic background tasks on a thread pool thread.
System.Windows.Forms’s Timer class Constructing an instance of this class tells Windows to associate a timer with the calling thread (see the Win32 SetTimer function). When this timer goes off, Windows injects a timer message (WM_TIMER) into the thread’s message queue. The thread must execute a message pump that extracts these messages and dispatches them to the desired callback method. Notice that all of the work is done by just one thread—the thread that sets the timer is guaranteed to be the thread that executes the callback method. This also means that your timer method will not be executed by multiple threads concurrently.
System.Windows.Threading’s DispatcherTimer class This class is the equivalent of the System.Windows.Forms’s Timer class for Silverlight and WPF applications.
System.Timers’s Timer class This timer is basically a wrapper around System. Threading’s Timer class that causes the CLR to queue events into the thread pool when the timer comes due. The System.Timers.Timer class is derived from System. ComponentModel’s Component class, which allows these timer objects to be placed on a design surface in Visual Studio. Also, it exposes properties and events, allowing it to be used more easily from Visual Studio’s designer. This class was added to the FCL years ago while Microsoft was still sorting out the threading and timer stuff. This class probably should have been removed so that everyone would be using the System. Threading.Timer class instead. In fact, I never use the System.Timers.Timer class, and I’d discourage you from using it, too, unless you really want a timer on a design surface.


راه دیگه هم استفاده از برنامه‌نویسی Multithread و System.Threading هست، امیدوارم حوصله مطالعه اش رو داشته باشید:
Multithreaded Programming for Components with System.Threading (http://msdn.microsoft.com/en-us/library/tak05yx0.aspx) (+ (http://msdn.microsoft.com/en-us/library/3es4b6yy(v=vs.100).aspx))

c-sharp_South
پنج شنبه 01 تیر 1391, 21:32 عصر
مرسی از راهنمایبتون.
من یک برنامه در ضمینه نیمباز نوشتم.
در نیمباز اگه یک آیدی با شش Resouce مختلف هم زمان هم آنلاین بشن آیدی اولی DC میشه.
من تامیر ها رو ده تا قرار دادم که این ده تایمر اون آیدی رو مرتب آن کنن و در نتیجه آیدی های قبل تر به صورت مکرر DC میشن.
این روش برای Change ID در نیمباز کاربرد بسیار فراوانی داره که کسی نتونه آیدی رو از کسی بخوره.