PDA

View Full Version : سوال: دسترسی به اشياء در BackGroundWorker



hamid67fathi
دوشنبه 18 خرداد 1388, 12:57 عصر
سلام
چطور ميشه از داخل Do_Worker به اشياء داخل فرم دسترسی پيدا کرد ؟؟
من نتونستم با e.result يک آبجکت رو بر گردونم !!
چطور ميشه اين کار رو کرد ؟؟
همه تاپيک ها رو هم خوندم ، همشون از اين شاخه به اون شاخه ميپرن !!

adinochestva
دوشنبه 18 خرداد 1388, 16:05 عصر
Windows Forms base classes make extensive use of ISynchronizeInvoke. The Control class (and every class derived from Control) relies on the underlying Windows messages and a message-processing loop (the message pump) to process them. The message loop must have thread affinity, because messages to a window are delivered only to the thread that created that window. In general, you must use ISynchronizeInvoke to access a Windows Forms window from another thread. Unfortunately, that often results in a cumbersome programming model when accessing windows and controls from multiple threads. Consider the code in Example 8-19 (http://barnamenevis.org/forum/#pnetcomp2-CHP-8-EX-19). If multiple threads need to update the text of the m_Label control, instead of merely setting the Text property, you are required to use a helper method (an anonymous method, in this example
(http://barnamenevis.org/forum/#pnetcomp2-CHP-8-FNOTE-1)) and a delegate dedicated to the task of setting the text of a label. Real-life examples will of course get much more complex and messy, with a high degree of internal coupling, because any changes to the user-interface layout, the controls on the forms, and the required behavior are likely to cause major changes to the code.


partial class MyForm : Form { delegate void SetLabel(Label label,string str); Label m_Label = new Label( ); //UpdateLabel is called by multiple threads void UpdateLabel(string text) { ISynchronizeInvoke synchronizer = m_Label; if(synchronizer.InvokeRequired == false) { m_Label.Text = text; return; } SetLabel del = delegate(Label label,string str) { label.Text = str; }; synchronizer.Invoke(del,new object[]{m_Label,text}); } //Rest of the class }

hamid67fathi
یک شنبه 24 خرداد 1388, 16:57 عصر
دقيقاً متوجه نشدم که چی کار بايد بکنم !!
من ميخوام يک گيريد رو با اطلاعاتی که از ديتا بيس ميخونم پر کنم که زمان ميبره!
و ميخوام اين کار رو با backgroundworker يا thread يا delgate انجام بدم که در هر سه
زمانی که ميخوام به گيريد ويوور دسترسی داشته باشم خطا م ميده که : شی مورد نظر در thread ديگری
قرر دارّد !!!!!!!!!!!

mohsen_csharp
یک شنبه 24 خرداد 1388, 17:23 عصر
راه ساده اینه که تو سازنده فرم این دستور را بنویسی

Control.CheckForIllegalCrossThreadCalls = false;

hamid67fathi
یک شنبه 24 خرداد 1388, 21:20 عصر
ممنون که جواب داديد .
اما اين راه هم جواب نداد !
البته بزرگان هم اصلاً اين راه رو توصيه نکردن ...

hamid67fathi
شنبه 06 تیر 1388, 09:49 صبح
از دوستان کسی نميتونه کمک کنه ؟؟

meysam_pro
پنج شنبه 25 تیر 1388, 00:23 صبح
دستکاری یک آبجکتی که توی یک نخ دیگه درست شده غیر مجازه.