Accessing form controls from WCF service
-
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
-
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
Can't be done. The WCF service is running in a seperate process, usually on a seperate machine. If the service needs access to the controls, it's up to your client app to pass the required values in. Seriously, no code outside of the form the controls sit on should ever need to access the controls of a form.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
You can't and shouldn't access them directly as has already been said. If your Forms app is receiving messages from the service then it can respond to those messages and update it's controls itself.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
I prefer to have the Service generate an XML file that the clients read.
-
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
Hi, First of all, are you sure that's what you want to do? Why? Sorry for asking this, but I believe it's not, and unless you provide a context, I don't know if i'm helping or throwing you a rope to hang yourself. But to answer your question, you can create an instance of the form in the service's class, and it will be available in that service's context. Now, I believe you want to publish the form data on the web. Here's how i'd go about it: 1. Keep both projects separated (not in the solution, just reference wise); 2. Implement methods on the web service that let you submit form data through them; 3. Make your forms application a client of the webservice; 4. As the controls on the form are filled, send changes to the web service; 5. All clients of the web service will be able to see those changes. Hope i helped. Regards
-
Hi, Can you please tell me how I can access the controls of a form from a WCF service? The forms application and the service reside in one solution as seperate projects and I added wcf service the necessary reference to forms project but still I'm not able to access the controls(controls are public). Your valuable suggestions are welcomed. Thanks!
It would be possible for you to use a duplex WCF service, and make your form implement a callback interface. Wouldn't put any UI types in your interface though. Generally this isn't a terribly scalable approach however.
Daniel Vaughan Twitter | Blog | Microsoft MVP | Projects: Calcium SDK, Clog | LinkedIn
-
Hi, First of all, are you sure that's what you want to do? Why? Sorry for asking this, but I believe it's not, and unless you provide a context, I don't know if i'm helping or throwing you a rope to hang yourself. But to answer your question, you can create an instance of the form in the service's class, and it will be available in that service's context. Now, I believe you want to publish the form data on the web. Here's how i'd go about it: 1. Keep both projects separated (not in the solution, just reference wise); 2. Implement methods on the web service that let you submit form data through them; 3. Make your forms application a client of the webservice; 4. As the controls on the form are filled, send changes to the web service; 5. All clients of the web service will be able to see those changes. Hope i helped. Regards
hi, thanks for detailed info. Here is my case: this is an internet cafe software and I want to send the time info to clients at specific intervals. The controls that I need to access are created and added to the mainform in form's load event. And the mainform is a singleton. Normally I can access it by; MainForm.Instance.Control I don't ask for ready code, please just show me the way Thanks in advance
-
hi, thanks for detailed info. Here is my case: this is an internet cafe software and I want to send the time info to clients at specific intervals. The controls that I need to access are created and added to the mainform in form's load event. And the mainform is a singleton. Normally I can access it by; MainForm.Instance.Control I don't ask for ready code, please just show me the way Thanks in advance
Hi again, Well, start by creating a method in your Forms project that does just that, recieves the time info, and updates the controls. You can do this with
public void updateTime(int timeSpan){
for each(Control c in this.Controls){
c.doSomething();
}
}then, all you need to do is spawn a thread that wakes up every few minutes (or whatever period of time you want) and access the web service and retrieve the time information from your server. When it wakes up, it calls the method mentioned earlier. The code i'm about to throw in here is by no means a product of good coding practices and will definitelly have bugs as i'm writing it directly on the reply from here at CodeProject, but might just do the trick.
public class Scheduler : Thread {
private Form parentForm;
private "WebServiceProxy" webServiceProxy;public Scheduler(Form mainForm){
this.parentForm = mainForm;
this.webServiceProxy = new "WebServiceProxy"();
}public override void Run() {
while(true) {
int timeSpan = webServiceProxy.getTimeSpan(); // get the time elapsed
sleep(5000); // will suspend thread execution for 5 seconds
}
}
}Now add an instance of the Scheduler to your main form. In the constructor of your form (or at the InitializeComponent method) initialize the Scheduler field and call the Run() method on it. Try it for yourself. Hope it helps
-
Hi again, Well, start by creating a method in your Forms project that does just that, recieves the time info, and updates the controls. You can do this with
public void updateTime(int timeSpan){
for each(Control c in this.Controls){
c.doSomething();
}
}then, all you need to do is spawn a thread that wakes up every few minutes (or whatever period of time you want) and access the web service and retrieve the time information from your server. When it wakes up, it calls the method mentioned earlier. The code i'm about to throw in here is by no means a product of good coding practices and will definitelly have bugs as i'm writing it directly on the reply from here at CodeProject, but might just do the trick.
public class Scheduler : Thread {
private Form parentForm;
private "WebServiceProxy" webServiceProxy;public Scheduler(Form mainForm){
this.parentForm = mainForm;
this.webServiceProxy = new "WebServiceProxy"();
}public override void Run() {
while(true) {
int timeSpan = webServiceProxy.getTimeSpan(); // get the time elapsed
sleep(5000); // will suspend thread execution for 5 seconds
}
}
}Now add an instance of the Scheduler to your main form. In the constructor of your form (or at the InitializeComponent method) initialize the Scheduler field and call the Run() method on it. Try it for yourself. Hope it helps
Thanks for your help si24803!