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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Multi-Form Set Correct Output

Multi-Form Set Correct Output

Scheduled Pinned Locked Moved C#
questionjsontutorial
17 Posts 4 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.
  • S Offline
    S Offline
    share_holder
    wrote on last edited by
    #1

    Hi again! This is my scenario: I have an API that opens several forms (instances of the class frmPpal.cs) I save a FormList such as:

    FormsList = new List<frmPpal>();

    Once all forms are opened, user can push in a Connect button and also in a Receive Data button. When Receive Data is clicked, there is also an Output Window (in each form) which shows sequentially all the received data (each 5 seconds, aprox) My doubt is how can I set the proper Output Window (chosing from all the Forms that are opened) for showing the received data. For example, if I have 3 Forms, each one must show in its Output window the Data that it requested. All the Forms are opened and can be closed by user, and user can also click on Connect and Receive Data whenever he wants. Any ideas? Ty in advance. Regards, D.

    W V B 3 Replies Last reply
    0
    • S share_holder

      Hi again! This is my scenario: I have an API that opens several forms (instances of the class frmPpal.cs) I save a FormList such as:

      FormsList = new List<frmPpal>();

      Once all forms are opened, user can push in a Connect button and also in a Receive Data button. When Receive Data is clicked, there is also an Output Window (in each form) which shows sequentially all the received data (each 5 seconds, aprox) My doubt is how can I set the proper Output Window (chosing from all the Forms that are opened) for showing the received data. For example, if I have 3 Forms, each one must show in its Output window the Data that it requested. All the Forms are opened and can be closed by user, and user can also click on Connect and Receive Data whenever he wants. Any ideas? Ty in advance. Regards, D.

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      You want the same data to be displayed in each Form?

      Live for today. Plan for tomorrow. Party tonight!

      S 1 Reply Last reply
      0
      • S share_holder

        Hi again! This is my scenario: I have an API that opens several forms (instances of the class frmPpal.cs) I save a FormList such as:

        FormsList = new List<frmPpal>();

        Once all forms are opened, user can push in a Connect button and also in a Receive Data button. When Receive Data is clicked, there is also an Output Window (in each form) which shows sequentially all the received data (each 5 seconds, aprox) My doubt is how can I set the proper Output Window (chosing from all the Forms that are opened) for showing the received data. For example, if I have 3 Forms, each one must show in its Output window the Data that it requested. All the Forms are opened and can be closed by user, and user can also click on Connect and Receive Data whenever he wants. Any ideas? Ty in advance. Regards, D.

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        It is not clear from your explanation what the problem is. Can you elaborate?

        V.

        S 1 Reply Last reply
        0
        • W Wayne Gaylard

          You want the same data to be displayed in each Form?

          Live for today. Plan for tomorrow. Party tonight!

          S Offline
          S Offline
          share_holder
          wrote on last edited by
          #4

          No, I will try to be more clear now. If I have, for example, 3 forms opened, the user can connect and receive data independently. The data tha is going to receive is different in time and size. Form1: Opened and Connected. Receiving Data = data1 from t=0; Form2: Opened and Connected. Receiving Data = data2 from t=1; Form3: Opened and Connected. Receiving Data = data3 from t=2; Once the three of them are receiving data, the 3 forms´ output windows must show the data that they are receiving, not syncronized between them and not the same data. I hope it´s better to understand now. Thanks, D.

          W 1 Reply Last reply
          0
          • V V 0

            It is not clear from your explanation what the problem is. Can you elaborate?

            V.

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

            The main problem is that the API was not prepared for being used as multi-thread, and now if I connect and receive data in different forms, it shows all the data in the Output Window of the last Form that the user clicks on Receive Data Thanks, D.

            V 1 Reply Last reply
            0
            • S share_holder

              No, I will try to be more clear now. If I have, for example, 3 forms opened, the user can connect and receive data independently. The data tha is going to receive is different in time and size. Form1: Opened and Connected. Receiving Data = data1 from t=0; Form2: Opened and Connected. Receiving Data = data2 from t=1; Form3: Opened and Connected. Receiving Data = data3 from t=2; Once the three of them are receiving data, the 3 forms´ output windows must show the data that they are receiving, not syncronized between them and not the same data. I hope it´s better to understand now. Thanks, D.

              W Offline
              W Offline
              Wayne Gaylard
              wrote on last edited by
              #6

              How does each form receive the data? Is it from the same source ? Is it the same class that processes the requests for data and then sends to the form or how does it happen? I think you should explain your application some more.

              Live for today. Plan for tomorrow. Party tonight!

              S 1 Reply Last reply
              0
              • S share_holder

                The main problem is that the API was not prepared for being used as multi-thread, and now if I connect and receive data in different forms, it shows all the data in the Output Window of the last Form that the user clicks on Receive Data Thanks, D.

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                I saw the post of Wayne and I agree. You need to explain how data is received. Don't post ALL your code, just the bits that are necessary for explaining how this works.

                V.

                S 1 Reply Last reply
                0
                • S share_holder

                  Hi again! This is my scenario: I have an API that opens several forms (instances of the class frmPpal.cs) I save a FormList such as:

                  FormsList = new List<frmPpal>();

                  Once all forms are opened, user can push in a Connect button and also in a Receive Data button. When Receive Data is clicked, there is also an Output Window (in each form) which shows sequentially all the received data (each 5 seconds, aprox) My doubt is how can I set the proper Output Window (chosing from all the Forms that are opened) for showing the received data. For example, if I have 3 Forms, each one must show in its Output window the Data that it requested. All the Forms are opened and can be closed by user, and user can also click on Connect and Receive Data whenever he wants. Any ideas? Ty in advance. Regards, D.

                  B Offline
                  B Offline
                  BillWoodruff
                  wrote on last edited by
                  #8

                  I'll join with the questions posed by Wayne and V: really need to know what 'entity' now receives the incoming data, and processes it. Why isn't the incoming data 'separated out' in some way where it is clear what query it is a result of ? This certainly sounds like a scenario where you would want multiple threads running. best, Bill

                  "In the River of Delights, Panic has not failed me." Jorge Luis Borges

                  S 1 Reply Last reply
                  0
                  • B BillWoodruff

                    I'll join with the questions posed by Wayne and V: really need to know what 'entity' now receives the incoming data, and processes it. Why isn't the incoming data 'separated out' in some way where it is clear what query it is a result of ? This certainly sounds like a scenario where you would want multiple threads running. best, Bill

                    "In the River of Delights, Panic has not failed me." Jorge Luis Borges

                    S Offline
                    S Offline
                    share_holder
                    wrote on last edited by
                    #9

                    Well, sorry for the missed out information, I´ll try to post a bit of code now: Here is the code for opening several forms (3 in this case). By now its just using a "for" loop, but it will be managed with an UI control for opening as many forms as the user needs:

                    static void Main()
                    {

                           FormsList = new List<frmPpal>();
                           Conn\_control = new ConnectionController();
                           for (int i = 0; i < 3; i++)
                           {
                               frmPpal form = new frmPpal(new DayTradeFacade());
                               FormsList.Add(form);
                               form.Show();
                    
                           }
                           Application.Run();
                    

                    Now the code of the frmPPal.cs relative to Connect button and Subscribe button (Receive Data):

                    private void buttonReconnect_Click(object sender, EventArgs e)
                    {
                    if (!Connected)
                    {
                    ThreadPool.QueueUserWorkItem(delegate
                    {
                    //These lines are for focusing the proper TextBox when connecting,
                    DayTradeFacade.SetMessagesTextBox(textBoxMessages);
                    DayTradeFacade.SetTesting1TextBox(textBoxOutput1);
                    DayTradeFacade.SetTesting2TextBox(textBoxOutput2);

                                    Facade.Account.AccountValueUpdated += UpdateListViewAccountValues;
                                    Facade.Portfolio.PortfolioChanged += UpdateListViewPortfolio;
                                    
                                    Facade.ConnectToTws();
                    
                                }, null);
                            }
                            else
                            {
                               textBoxMessages.WriteLine("Already connected.");
                            }
                        }
                    

                    And now Receive Data:

                    private void buttonSubscribeToPresetFutures_Click(object sender, EventArgs e)
                    {
                    if (!Connected)
                    {
                    textBoxMessages.WriteLine(NotConnectedErrorMessage);
                    return;
                    }
                    if (!FuturesTradingHours.NowIsInsideTradingHours())
                    {
                    textBoxMessages.WriteLine(
                    "Can only subscribe to futures between " + FuturesTradingHours.MarketOpenTime
                    + " and " + FuturesTradingHours.FiveSecondPriceBarSubscriptionUnsubscribeTime + ".");
                    return;
                    }
                    //DPT
                    textBoxOutput1.Clear();
                    //FIN

                            Facade.SubscriptionManager.SubscribeToFiveSecondPriceBars(\_pres
                    
                    V 1 Reply Last reply
                    0
                    • V V 0

                      I saw the post of Wayne and I agree. You need to explain how data is received. Don't post ALL your code, just the bits that are necessary for explaining how this works.

                      V.

                      S Offline
                      S Offline
                      share_holder
                      wrote on last edited by
                      #10

                      Hello V. I´ve post a bit of code in the replay to Bill. Hope this helps, Regards, D.

                      1 Reply Last reply
                      0
                      • W Wayne Gaylard

                        How does each form receive the data? Is it from the same source ? Is it the same class that processes the requests for data and then sends to the form or how does it happen? I think you should explain your application some more.

                        Live for today. Plan for tomorrow. Party tonight!

                        S Offline
                        S Offline
                        share_holder
                        wrote on last edited by
                        #11

                        Hello Wayne, I´ve just post some code in Bill´s answer, I hope it´s enough information; otherwise please tell me and I´ll post more code and explanations. Ty in advance, regards, D.

                        1 Reply Last reply
                        0
                        • S share_holder

                          Well, sorry for the missed out information, I´ll try to post a bit of code now: Here is the code for opening several forms (3 in this case). By now its just using a "for" loop, but it will be managed with an UI control for opening as many forms as the user needs:

                          static void Main()
                          {

                                 FormsList = new List<frmPpal>();
                                 Conn\_control = new ConnectionController();
                                 for (int i = 0; i < 3; i++)
                                 {
                                     frmPpal form = new frmPpal(new DayTradeFacade());
                                     FormsList.Add(form);
                                     form.Show();
                          
                                 }
                                 Application.Run();
                          

                          Now the code of the frmPPal.cs relative to Connect button and Subscribe button (Receive Data):

                          private void buttonReconnect_Click(object sender, EventArgs e)
                          {
                          if (!Connected)
                          {
                          ThreadPool.QueueUserWorkItem(delegate
                          {
                          //These lines are for focusing the proper TextBox when connecting,
                          DayTradeFacade.SetMessagesTextBox(textBoxMessages);
                          DayTradeFacade.SetTesting1TextBox(textBoxOutput1);
                          DayTradeFacade.SetTesting2TextBox(textBoxOutput2);

                                          Facade.Account.AccountValueUpdated += UpdateListViewAccountValues;
                                          Facade.Portfolio.PortfolioChanged += UpdateListViewPortfolio;
                                          
                                          Facade.ConnectToTws();
                          
                                      }, null);
                                  }
                                  else
                                  {
                                     textBoxMessages.WriteLine("Already connected.");
                                  }
                              }
                          

                          And now Receive Data:

                          private void buttonSubscribeToPresetFutures_Click(object sender, EventArgs e)
                          {
                          if (!Connected)
                          {
                          textBoxMessages.WriteLine(NotConnectedErrorMessage);
                          return;
                          }
                          if (!FuturesTradingHours.NowIsInsideTradingHours())
                          {
                          textBoxMessages.WriteLine(
                          "Can only subscribe to futures between " + FuturesTradingHours.MarketOpenTime
                          + " and " + FuturesTradingHours.FiveSecondPriceBarSubscriptionUnsubscribeTime + ".");
                          return;
                          }
                          //DPT
                          textBoxOutput1.Clear();
                          //FIN

                                  Facade.SubscriptionManager.SubscribeToFiveSecondPriceBars(\_pres
                          
                          V Offline
                          V Offline
                          V 0
                          wrote on last edited by
                          #12

                          At first sight I would think that the Facade object is the same for all three forms.

                          V.

                          S 1 Reply Last reply
                          0
                          • V V 0

                            At first sight I would think that the Facade object is the same for all three forms.

                            V.

                            S Offline
                            S Offline
                            share_holder
                            wrote on last edited by
                            #13

                            Yes, you are right. But, I think it´s not relevant for setting the proper output window. I mean, facade is just the class for events, but each event is triggered to the proper form. I just need to focus in the proper form. I´ve got a FormList, so for example what I´ve done is:

                            TTS_RealTime.FormsList[0].textBoxOutput1.WriteLine("Normal: " + openTime);

                            Just need to set FormList[index] to the form from where the subcription was made. Any idea? Ty, D.

                            V 1 Reply Last reply
                            0
                            • S share_holder

                              Yes, you are right. But, I think it´s not relevant for setting the proper output window. I mean, facade is just the class for events, but each event is triggered to the proper form. I just need to focus in the proper form. I´ve got a FormList, so for example what I´ve done is:

                              TTS_RealTime.FormsList[0].textBoxOutput1.WriteLine("Normal: " + openTime);

                              Just need to set FormList[index] to the form from where the subcription was made. Any idea? Ty, D.

                              V Offline
                              V Offline
                              V 0
                              wrote on last edited by
                              #14

                              Maybe I misunderstood your code, but if the Facade object (the instance, not the class) is the same for each form it will receive the data of all three forms. If that's the case you need to : - or initate a different instance for each form - or find a way to find out who requested the information (maybe it is in the response) and dispatch the information to the correct form.

                              V.

                              S 1 Reply Last reply
                              0
                              • V V 0

                                Maybe I misunderstood your code, but if the Facade object (the instance, not the class) is the same for each form it will receive the data of all three forms. If that's the case you need to : - or initate a different instance for each form - or find a way to find out who requested the information (maybe it is in the response) and dispatch the information to the correct form.

                                V.

                                S Offline
                                S Offline
                                share_holder
                                wrote on last edited by
                                #15

                                V. wrote:

                                - or find a way to find out who requested the information (maybe it is in the response) and dispatch the information to the correct form.

                                That´s why I´ve post here, looking for some ideas to do this ;) But, each form has it´s facade, as I wrote in the Main() call:

                                frmPpal form = new frmPpal(new DayTradeFacade());

                                V 1 Reply Last reply
                                0
                                • S share_holder

                                  V. wrote:

                                  - or find a way to find out who requested the information (maybe it is in the response) and dispatch the information to the correct form.

                                  That´s why I´ve post here, looking for some ideas to do this ;) But, each form has it´s facade, as I wrote in the Main() call:

                                  frmPpal form = new frmPpal(new DayTradeFacade());

                                  V Offline
                                  V Offline
                                  V 0
                                  wrote on last edited by
                                  #16

                                  mmm, personally I would rethink your solution in giving the request/response responsibility towards the forms and let them handle it. (= solution 1) In case you want solution 2, you need to find out if the component that responds actually supports this. Is there some sort of communication Id you need to provide when sending a request? If not, there is no way in telling for sure which response is for which request. If there is a request/response Id all you need is a dictionary linking your form with that Id. 1. Add Id/Form to dictionary when sending 2. On receive find form with key = Id. 3. Remove from dictionary. If you don't have an Id, I'm not sure you can solve it via solution 2 unless you are sure the request/response sync is in order. hope this helps.

                                  V.

                                  S 1 Reply Last reply
                                  0
                                  • V V 0

                                    mmm, personally I would rethink your solution in giving the request/response responsibility towards the forms and let them handle it. (= solution 1) In case you want solution 2, you need to find out if the component that responds actually supports this. Is there some sort of communication Id you need to provide when sending a request? If not, there is no way in telling for sure which response is for which request. If there is a request/response Id all you need is a dictionary linking your form with that Id. 1. Add Id/Form to dictionary when sending 2. On receive find form with key = Id. 3. Remove from dictionary. If you don't have an Id, I'm not sure you can solve it via solution 2 unless you are sure the request/response sync is in order. hope this helps.

                                    V.

                                    S Offline
                                    S Offline
                                    share_holder
                                    wrote on last edited by
                                    #17

                                    Thank you, I will try first option. Regards, D.

                                    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