Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing form controls from WCF service

Accessing form controls from WCF service

Scheduled Pinned Locked Moved C#
csharpwcfquestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    teknolog123
    wrote on last edited by
    #1

    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!

    D D P S D 5 Replies Last reply
    0
    • T teknolog123

      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!

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • T teknolog123

        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!

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        • T teknolog123

          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!

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          I prefer to have the Service generate an XML file that the clients read.

          1 Reply Last reply
          0
          • T teknolog123

            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!

            S Offline
            S Offline
            si24803
            wrote on last edited by
            #5

            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

            T 1 Reply Last reply
            0
            • T teknolog123

              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!

              D Offline
              D Offline
              Daniel Vaughan
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • S si24803

                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

                T Offline
                T Offline
                teknolog123
                wrote on last edited by
                #7

                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

                S 1 Reply Last reply
                0
                • T teknolog123

                  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

                  S Offline
                  S Offline
                  si24803
                  wrote on last edited by
                  #8

                  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

                  T 1 Reply Last reply
                  0
                  • S si24803

                    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

                    T Offline
                    T Offline
                    teknolog123
                    wrote on last edited by
                    #9

                    Thanks for your help si24803!

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups