2011-12-20 38 views
0

我在課堂上有一個方法Process(Progressbar)黑名單如何創建一個線程,並分析在C#中的參數2.0

我試圖用這樣的:

Thread thread = new Thread(() => Blacklist.Process(pgImportProcess));

過程中出現錯誤

C#3.0語言功能

So how can i create a thread and parse progressbar as a parameter?

預先感謝

+0

線程沒有一個構造採取委託,你有一個ThreadStart傳遞或ParameterizedThreadStart像︰Thread thread = new Thread(new ThreadStart(()=> Blacklist.Process(pgImportProcess)); – Polity 2011-12-20 02:22:01

+0

@Polity:我試着如你所說,它提醒一個錯誤** C#3.0語言功能**(注意:我在C #2.0)VS2005 – 2011-12-20 02:26:52

+1

,因爲你使用的lambda表達式在C#2.0中是不可用的。 })); – Polity 2011-12-20 02:34:49

回答

1

你嘗試過:

void Invoker(){ 
    ParameterizedThreadStart pts = Start; 
    Thread thread = new Thread(pts); 
    thread.Start(new object()); 
} 
public void Start(object o) 
{ 
    //do stuff 
} 
+0

來強制中止這個線程。llchev:不,我沒有嘗試過,但它不會爲我工作......,我確定。 – 2011-12-20 03:05:45

1

比上創建無法從不同的線程訪問UI對象。每個 Control都有一個 Invoke方法,它將在UI線程上執行委託。例如,如果您需要更新進度條進度:

progressBar.Invoke(new Action(){()=> progressBar.Value = updateValue;});

所以你只需要使用Thread constructor that takes a ParameterizedThreadStart委託。

Thread thread = new Thread(StartProcess); 
thread.Start(pgImportProcess); 

... 

private static void StartProcess(object progressBar) { 
    Blacklist.Process((ProgressBar)progressBar); 
} 
+0

該問題不是進度條的值,但是它是一個作爲進度條控件進入線程的解析參數。 – 2011-12-20 03:06:47

1

你可以創建一個類來傳遞你的參數一樣

public class Sample 
{ 
    object _value; 

    public Sample(object value) 
    { 
     this._value = value; 
    } 

    public void Do() 
    { 
     // dosomething 
     // Invoke the Process(value) 
    } 
} 

然後

Sample p = new Sample("your parameter : Progressbar"); 
new Thread(new ThreadStart(p.Do)).Start(); 
相關問題