WCF, Accessing a windows forms controls from a service

后端 未结 5 829
南旧
南旧 2021-01-05 22:17

I have a WCF service that is hosted inside a Windows Form.

How can I access the controls of the form from the methods in my service?

for example I have

5条回答
  •  清歌不尽
    2021-01-05 22:55

    Use a delegate. Create a delegate in the codebehind of your form which references a method that writes to the textbox and pass it to the Service, the service can then invoke the delegate when it wants to print a message.

    You will have issues trying to update the textbox because the delegate will be invoked on a different thread to that which the textbox was created on. In the WPF world you would use Dispatcher.BeginInvoke to get around this, not sure what the WinForms equivalent is.

提交回复
热议问题