對於狂熱的C#開發人員來說,這應該是一個非常簡單的解決方案。我期望在一個類中改變一個字符串的值,所以在一個線程中,字符串可以在沒有我做任何事情的情況下改變。這是我的意思,簡化的一個小例子,但你應該明白。如何保存對另一個類中的對象的引用?
class A_CLass
{
string keptString;
void keepString(ref string theString)
{
keptString = theString;
}
// This will get called when the thread is triggered
void changeString(string theString)
{
keptString = theString;
}
}
void f1()
{
A_Class a = new A_Class();
string base_string = "asdf";
a.keepString(ref base_string);
...
// Thread is signaled
...
// Now base_string should be "fdsa"
}
void threadedFunction()
{
// When the thread is triggered ...
a.changeString("fdsa");
}
基本上我想保持「base_string」的參考在A_Class,因此螺紋的方法可以改變值,並且f1()中,我可以看到改變的值,在這種情況下「FDSA」。
謝謝!
如果我得到你想要的東西,你想存儲到'base_string'的引用,以便在'threadedFunction改變它() '在'f1()'中改變它? –
是的,這是我想要的,但不僅僅是爲了字符串,我還想節省一節課。 (字符串只是一個簡單的例子) – Tizz
然後看看我的答案。您需要編寫一個小包裝類來環繞要跟蹤的對象。 –