PDA

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



bpzone
شنبه 01 اسفند 1388, 11:41 صبح
با سلام
من داخل یک برنامه از Thread استفاده کردم و داخل تابع مربوطه میخوام به یک لیست باکس مقدار بدم ، اما خطا میگیره

Error 3 An object reference is required for the non-static field, method, or property 'Server_Socket.Server_Main.lb_Log' D:\Project\Server_Socket\Server_Socket\Form1.cs 104 17 Server_Socket

SMRAH1
شنبه 01 اسفند 1388, 12:23 عصر
سلام
توی MSDN دنباله مقاله ای با این عنوان بگرد:

How to: Make Thread-Safe Calls to Windows Forms Controls
موفق باشی

bpzone
یک شنبه 02 اسفند 1388, 13:17 عصر
SMRAH1 عزیز
باتشکر از لطف شما
مقاله ای روکه گفتید خوندم و پیاده سازی کردم، اما به یک مشکل جدید مواجه شدم


System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'lb_Log' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, String lparam)
at System.Windows.Forms.ListBox.NativeAdd(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.AddI nternal(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.Add( Object item)
at Client_Server.Main.show1(String s) in D:\Project\Client_Server\Client_Server\Form1.cs:li ne 32
at Client_Server.DataRecive.Receve() in D:\Project\Client_Server\Client_Server\Form1.cs:li ne 155
at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: