2010-08-16 155 views
1

我有這樣的:C# - 如何讓這個線程等待?

public class ServiceLibrary 
{ 
    public object result = null; 
    private bool finished = false; 

    public void testar() 
    { 
     ServiceReference.Service1SoapClient serviceReference = new ServiceReference.Service1SoapClient(); 
     serviceReference.updateUserCompleted += new EventHandler<ServiceReference.updateUserCompletedEventArgs>(serviceReference_updateUserCompleted); 
     serviceReference.updateUserAsync(); 
     ManualResetEvent m = new ManualResetEvent(true); 

    } 

    void serviceReference_updateUserCompleted(object sender, ServiceReference.updateUserCompletedEventArgs e) 
    { 
     result = e.Result; 
     finished = true; 
    } 
} 

外我有這樣的:

public Home() 
{ 
    InitializeComponent(); 

    ServiceLibrary serviceLibrary = new ServiceLibrary(); 
    serviceLibrary.testar(); 
    lblCharName.Text = Convert.ToString(serviceLibrary.result); 

} 

我應該怎麼做的線程等待,所以當我ASIGN文本,它包含的價值嗎? 謝謝

+0

? – 2010-08-16 20:57:04

回答

-2

我也有類似的情況。我使用了一種稱爲 polling的技術,這聽起來很像。取決於你的情況,它可能適合你,也可能不適合你。

public class ServiceLibrary 
{ 
public object result = null; 
private bool finished = false; 

public void testar() 
{ 
    ServiceReference.Service1SoapClient serviceReference = new ServiceReference.Service1SoapClient(); 
    serviceReference.updateUserCompleted += new EventHandler<ServiceReference.updateUserCompletedEventArgs>(serviceReference_updateUserCompleted); 
    serviceReference.updateUserAsync(); 
    ManualResetEvent m = new ManualResetEvent(true); 

while !finished 
Thread.Sleep(100); 

doStuffWithResult(result); 
} 

void serviceReference_updateUserCompleted(object sender, ServiceReference.updateUserCompletedEventArgs e) 
{ 
    result = e.Result; 
    finished = true; 
} 
+0

調用者想要強制服務調用是同步的(這是您的更改會執行的操作)並不清楚。 – LBushkin 2010-08-16 21:01:02

+0

他還想說什麼,他希望它等到價值可用? – 2010-08-16 21:02:18

+0

(-1)絕不應該爲這類事情使用睡眠。只是浪費用戶和處理器時間。 – Mike 2010-08-16 21:57:48

0

使您的ManualResetEvent變量成爲類的成員變量。 並在您的線程方法中:serviceReference_updateUserCompleted,請務必調用 m.WaitOne();

1

難道你不能用你的ManualResetEvent?或創建一個新的。

我相信ManualResetEvent是線程安全的....

public class ServiceLibrary 
{ 
    public object result = null; 
    private bool finished = false; 
    public ManualResetEvent m; 

    public void testar() 
    { 
     ServiceReference.Service1SoapClient serviceReference = new ServiceReference.Service1SoapClient(); 
     serviceReference.updateUserCompleted += new EventHandler<ServiceReference.updateUserCompletedEventArgs>(serviceReference_updateUserCompleted); 
     serviceReference.updateUserAsync(); 
     m = new ManualResetEvent(false); 
    } 

    void serviceReference_updateUserCompleted(object sender, ServiceReference.updateUserCompletedEventArgs e) 
    { 
     result = e.Result; 
     finished = true; 
     m.Set(); 
    } 
} 

public Home() 
{ 
    InitializeComponent(); 

    ServiceLibrary serviceLibrary = new ServiceLibrary(); 
    serviceLibrary.testar(); 

    serviceLibrary.m.WaitOne(); 

    lblCharName.Text = Convert.ToString(serviceLibrary.result); 
} 

+0

這件事很棒,但沒有工作! = X 我使用Windows Phone 7模擬器... – Alan 2010-08-16 21:16:08

+0

我知道...... ManualResetEvent是真的有用....我不知道爲什麼它不會工作。你確定你把初始狀態設置爲「false」嗎? – Lewray 2010-08-16 21:20:13

+0

這是要走的路。不知道爲什麼它不適合你,但這是實現這一目標的標準方式。 – Mike 2010-08-16 22:00:22

0

什麼

public class ServiceLibrary 
{ 
    public object result = null; 

    public void testar() 
    { 
    var serviceReference = new ServiceReference.Service1SoapClient(); 
    using(var m = new ManualResetEvent(false)) 
    { 
     Action<object, ServiceReference.updateUserCompletedEventArgs> handler = 
     (sender, e) => 
     { 
     result = e.Result; 
     m.Set(); 
     }; 
     serviceReference.updateUserCompleted += handler; 
     serviceReference.updateUserAsync(); 
     m.WaitOne(); 
     serviceReference.updateUserCompleted -= handler; 
    } 
    } 
} 
是否使用的是.NET版本