PDA

View Full Version : تغییر متن label در ترد



pouria_bayat
شنبه 19 مرداد 1392, 09:40 صبح
سلام دوستان
سوالی داشتم که مدتی هست فکر منرو به خودش مشغول کرده و مطمئنم همه شما حداقل یکبار درگیراین مسئله بودید.
مشگل من از اینجا شروع میشه که نمیتونم داخل متد static مربوط به تردم که از Threadstart استفاده میکنم بتونم متن داخل یکی از label های فرم برنامه رو تغییر بدم چرا که متد مربوطه static باید باشه چون threadstart از اون استفاده میکنه و در حالی که label من استاتیک نیست و توی اون متد شناخته شده نیست و وقتی یک نمونه از فرم میگیرم داخل متد تا از طریق همون نمونه بتونم متن label رو تغییر بدم هیچ تغییری باز اعمال نمیشه.

tooraj_azizi_1035
شنبه 19 مرداد 1392, 11:06 صبح
تو این مثال متد ترد استاتیک نیست:

using System;
using System.Threading;

// The ThreadWithState class contains the information needed for
// a task, and the method that executes the task.
//
public class ThreadWithState
{
// State information used in the task.
private string boilerplate;
private int value;

// The constructor obtains the state information.
public ThreadWithState(string text, int number)
{
boilerplate = text;
value = number;
}

// The thread procedure performs the task, such as formatting
// and printing a document.
public void ThreadProc()
{
Console.WriteLine(boilerplate, value);
}
}

// Entry point for the example.
//
public class Example
{
public static void Main()
{
// Supply the state information required by the task.
ThreadWithState tws = new ThreadWithState(
"This report displays the number {0}.", 42);

// Create a thread to execute the task, and then
// start the thread.
Thread t = new Thread(new ThreadStart(tws.ThreadProc));
t.Start();
Console.WriteLine("Main thread does some work, then waits.");
t.Join();
Console.WriteLine(
"Independent task has completed; main thread ends.");
}
}