Simplified version of what I'm trying to achieve:
- I have a WinForms app that runs hidden (Visible = false) in the background.
- It's got only one Form, and I kept the default name - Form1
- This WinForms app hosts a WCF Service. For now we'll call it the Listener service.
- This Listener service has a function called "DisplayAlert()" that's exposed as a service function
- An app sits on another machine sends a message to the Listener Service via a standard WCF service call
I've got all of the above working just fine. I can step through the code and watch the flow of messages as the DisplayAlert() function is called.
What I can't figure out, and I can't believe it's so hard to find how to do something this simple:
- I'd like the DisplayAlert() function in the hosted service interact directly with the WinForm that's hosting it to make the form visible.
All I want to do is set the Visibility to true, and call another function on the WinForm.
This seems to me like it should be as simple as adding a reference to the form, or making a public function on the form and calling it from the service class, but I can't even figure out how to reference Form1 from within the service class.
Am I missing something obvious? How do I even reference the instance of Form1, which is hosting the service?
I've gone down the path of....
- Creating an event in the ListenerService (AlertReceived, virtual void OnAlertReceived), thinking that on the Form, I could add an event handler.
- No dice. I'm not instantiating a ListenerService class directly, it's running in the ServiceHost.
- Trying to reference the Application object from within the class, thinking I could reference it as Application.Form1, but nope. I can't even see the Application object from within the service class.
- I'm probably missing something obvious here, but I'm not sure.
Any other suggestions?
I can add code if it helps.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…