PDA

View Full Version : DoEvents



odiseh
پنج شنبه 28 دی 1385, 13:36 عصر
سلام



Application.DoEvents()


کی استفاده میشه و چیکار میکنه؟

programmermp
پنج شنبه 28 دی 1385, 14:07 عصر
سلام

مطالب زیر رو یک نگاه بنداز با مثال کامل از help وی بی هست کاملا توضیح

داده اگه خودت خواستی ببینی توی محیط دات نت f1 رو بزن و دنبال application.doevents بگرد

همین ها رو ان تو نوشته

ولی از همین خط اولش می شه فهمید (البته از نظر ترجمه متن) که تمام

پیام های ویندوز رو که در حال حاظر در صف پیام قرار گرفته اند پردازش می کنه مثالش رو ببین متوجه می شی


Processes all Windows messages currently in the message queue.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)


Syntax


Visual Basic (Declaration)
Public Shared Sub DoEvents
Visual Basic (Usage) Application (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_Application.htm).DoEvents


Remarks

When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.
If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_ListBox.htm) and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.
Typically, you use this method in a loop to process messages.

Caution Calling this method can cause code to be re-entered if a message raises an event.


Example

The following code example demonstrates using the DoEvents method. When the example runs, a user can select graphics files from an OpenFileDialog (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_OpenFileDialog.htm). The selected files are displayed in the form. The DoEvents method forces a repaint of the form for each graphics file opened. To run this example, paste the following code in a form containing a PictureBox (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_PictureBox.htm) named PictureBox1, an OpenFileDialog named OpenFileDialog1, and a button named fileButton. Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method. The example also requires that the Control.Click (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/E_System_Windows_Forms_Control_Click.htm) event of the Button (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_Button.htm) control and the FileOk (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/E_System_Windows_Forms_FileDialog_FileOk.htm) event of the OpenFileDialog are connected to the event handlers defined in the example. When
the example is running, display the dialog box by clicking the button.

Visual Basic



Private Sub InitializePictureBox()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.PictureBox1.BorderStyle = _
System.Windows.Forms.BorderStyle.FixedSingle
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Me.PictureBox1.Location = New System.Drawing.Point(72, 112)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 136)
Me.PictureBox1.TabStop = False
End Sub

Private Sub InitializeOpenFileDialog()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

' Set the file dialog to filter for graphics files.
Me.OpenFileDialog1.Filter = _
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"

' Allow the user to select multiple images.
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Title = "My Image Browser"
End Sub

Private Sub fileButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FileButton.Click
OpenFileDialog1.ShowDialog()
End Sub


' This method handles the FileOK event. It opens each file
' selected and loads the image from a stream into PictureBox1.
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles OpenFileDialog1.FileOk

Me.Activate()
Dim file, files() As String
files = OpenFileDialog1.FileNames

' Open each file and display the image in PictureBox1.
' Call Application.DoEvents to force a repaint after each
' file is read.
For Each file In files
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(file)
Dim fileStream As System.IO.FileStream = fileInfo.OpenRead()
PictureBox1.Image = System.Drawing.Image.FromStream(fileStream)
Application.DoEvents()
fileStream.Close()

' Call Sleep so the picture is briefly displayed,
'which will create a slide-show effect.
System.Threading.Thread.Sleep(2000)
Next
PictureBox1.Image = Nothing
End Sub

programmermp
پنج شنبه 28 دی 1385, 15:19 عصر
اگه بخواهم یک توضیح مختصری درباره کد بالا بدم

کدهای بالا به این صورت عمل می کنه که شما یک عکس رو باز می کنی با کامپوننت

open dialog بعد متد fileok مربوط به openfialog بعد از باز کردن فایل عمل می کنه بدین

صورت که فایل باز شده به صورت یک استریم خوانده شده و در picturebox ریخته میشه

بعد دستور application.doevent اجرا می شه که باعث می شه دستورات

بعدی که احتمالا در صف دستوارتی که باید اجرا بشند قرار می گیرند یعنی در

صف انتظار برای اجرا شدن ، اجرا بشند

بعد دستور threading کنترل اجرای دستورات را تا 2000میلی ثانیه یا همون 2

ثانیه به حالت suspend یا تعلیق در میاره و بعد از این مدت زمان دستور اخر که

باعث خالی شدن picturebox میشه اجرا می شه

نتیجه اینکه متد doevent از application برای اجرا کردن دستورات موجود در

صف دستورات اجرایی هست

همین من که کف کردم این همه توضیح دادم

امیدوارم که هم متوجه شده باشی و هم مفید واقع بشه برای همه :کف: :کف:


با ارزوی موفقیت برای همه:تشویق: :تشویق: :تشویق:

programmermp
پنج شنبه 28 دی 1385, 16:37 عصر
البته یه جور دیگه می شه با توجه به متن زیر که در بالا هم اومده این قضیه رو برداشت کرد


When you run a Windows Form, it creates the new form, which then waits for events to handle. Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top.
If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox (http://www.barnamenevis.org/forum/ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/T_System_Windows_Forms_ListBox.htm) and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.

متن بالا می گه اگه شما یک فرم ویندوزی رو اجرا کنید یک فرم جدید ایجاد می شه که بعد

از ایجاد منتظر میشه تا برای در دست گرفتن event یا رویدادهای موجود در خودش . هر زمان

یا هر باری که فرم رویداد ها رو برای اجرا در دست می گیره تمام کدهای اختصاص داده شده

به انها رو اجرا می کنه و تمام رویدادهای دیگه در یک صف انتظار قرار می گیرند یعنی منتظر

می شن تا زمان اجرا شدنشون فرا برسه و تا زمانی که کدهای شما رویداد ها رو در کنترل

خودشون گرفتند فرم به شما جوابگو نیست تا زمانیکه رویداد مورد نظر تماما اجرا بشه

برای مثال اگه روی فرمی که باز کردید یک فرم دیگه رو باز کرده و درگ کنید دیگه مجدد فرم قبلی ساخته نمیشه

ولی اگه توی کدهاتون از doevent استفاده کنید در این حالت رویدادها منتظر نمی مونند و

یکی یکی اجرا می شند

تمام چیزی که من فهمیدم همین بود که گفتم

حالا از اساتید دیگه خواهشمندم که اگه چیز بیشتر ی در این رابطه میدونند بگن تا من هم

یاد بگیرم چون واقعا با پیدا کردن این مطلب خودم هم مشتاق یادگیری بیشتر شدم

با تشکر:تشویق: :تشویق:

Sub Zero
جمعه 29 دی 1385, 16:41 عصر
اگه توی کدهاتون از doevent استفاده کنید در این حالت رویدادها منتظر نمی مونند و
یکی یکی اجرا می شند


نتیجه اینکه متد doevent از application برای اجرا کردن دستورات موجود در
صف دستورات اجرایی هست

نکات اصلی همین دوتاست .
جناب صالحی اگه ممکنه کدهاتون رو توی تگ [code] بنویسید .

odiseh
شنبه 30 دی 1385, 11:13 صبح
programmermp
سلام و تشکر فراوان از لطف شما

ولی یه سوال : خوب بقیه دستورات برای اجرا به ترتیب ظاهر شدنشان در متن برنامه اجرا میشوند و زمانیکه مفسر به اون سطر از دستور میرسه اون رو اجرا میکنه حالا چه doevents باشه چه نباشه. رویدادها هم زمانیکه کاربر بخواهد ان ها را اجرا کنه اجرا میشن یعنی اجرای اونا دست کاربره . درسته؟ بنابراین هر event زمانی اجرا میشه که کاربر بخواد نه اینکه همه event ها با هم بخوان اجرا بشن. کاربر که نمیتونه چند تا event رو باهم اجرا کنه پس این جمله All other events wait in the queue یعنی چی؟

programmermp
شنبه 30 دی 1385, 11:56 صبح
سلام و خواهش می کنم

ببین من گفتم که وقتی یک فرم در حال اجرای رویدادها هست اگه یک فرم دیگه روی اون

قرار بگیره یعنی یک رویداد جدید اجرا بشه باعث توقف انجام رویدادهای دیگه می شه

تا زمانی که رویداد جدید کارش تمام بشه از doevent می شه برای جلوگیری از توقف انجام

رویداد های دیگه استفاده کرد در زمان اجرای رویدادهای دیگه

اصلا این متد کارش از اسمش تقریبا معلومه do events انجام بده رویدادها را

همین و من خودم کار نکردم با این قضیه بخاطر همین مثال و همه چیز رو با توضیحات کامل

گذاشتم که خودت بری دنبالش و از دوستان دیگه هم که در این زمینه تجربه داشتن
خواهش کردم که کمک کنند

بنابراین هیچی دیگه بسه چقدر حرف می زنم من اه.....:لبخند: :لبخند: :لبخند: :لبخندساده: :لبخندساده: