2013-11-22 28 views
0

我的問題有點難以解釋。在不掛winform的方法中返回東西

我有一個對象做的事情誰需要構造一個接口:

public class Engine 
{ 
    public Engine(IControler controler); 
} 

在這個界面中,還有誰需要直接從WinForm的東西的方法:

public class Controler : IControler 
{ 
    private MyForm _hook;  

    public Controler(MyForm hook) 
    { 
    _hook = hook; 
    } 

    // So, the method should be like this : 

    public int GetTheRightThing() // method from interface 
    { 
    // return here the right thing from clicking on the winform 
    return _hook.WaitForClickAndGetTheRightThing()   
    } 
} 

那麼,如何我可以實現「_hook.WaitForClickAndGetTheRightThing()」方法嗎? 我實在看不出如何實現它不掛形式...

+1

做什麼你的意思是「掛錶格」? –

+0

我的意思是,我不知道如何實現方法WaitForClickAndGetTheRightThing,因爲方法將不得不在中間等待用戶干預,然後返回信息。但在等待時,線程會卡住(對不起我的英語)。 –

+0

@RananLamour除非你的方法在背景中做了*重*,你的表單不能被掛出。用戶干預應該通過對話來完成,這意味着你的表單不能掛出,只是等待對話框返回。如果您在主UI線程中運行了其他代碼,則可以使用一些異步方法調用。 –

回答

0

那麼這個怎麼樣:

public void GetTheRightThing(Action<int> callback) // method from interface 
{ 
    // return here the right thing from clicking on the winform 
    _hook.WaitForClickAndGetTheRightThing(callback)   
} 

,並在形式上實施後完成調用

callback.Invoke(result); 
+0

這就是我所做的,但我的問題是在執行waitforclickandgettherthingthing。我不知道如何在方法中等待,並在用戶完成信息後繼續它 –

+0

你的意思是你想要使用線程或背景工作? – Ric

+0

是的。我不知道是否可以暫停等待用戶干預WinForm的方法。 –

相關問題