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. Control.Invoke must be used to interact with controls created on a seperate thread

Control.Invoke must be used to interact with controls created on a seperate thread

Scheduled Pinned Locked Moved C#
data-structuresxmlhelp
5 Posts 2 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.
  • B Offline
    B Offline
    benjamin yap
    wrote on last edited by
    #1

    Hi, I get this error despite create a delegate..

        private delegate void ResumeChart();
        ResumeChart ChartResume;
    
        private void ResumeChartLayout()
        {
            this.m\_ChartControl.ResumeChartLayout();
        }
    
        void ResetChart()
        {
            this.Invoke(ChartResume);
        }
    

    Inside my constructor I have this.

        public frmGraph(String stockQuote,String\[\] args)
        {
            InitializeComponent();
            newArgs = new String\[args.Length\];
            Array.Copy(args, newArgs, args.Length);
            token = newArgs\[0\];
            
            this.stockQuote = stockQuote;
            //this.stockQuote = "MGM";
            label1.Text = stockQuote;
            this.Text = stockQuote + " Graph";
            **ChartResume = ResumeChartLayout;**
        }
    

    Inside my form load, i create a new thread to call another function

        private void frmGraph\_Load(object sender, EventArgs e)
        {   
            FillDataTable();
    
            //SetFilter(DateTime.Now.AddDays(-9), DateTime.Now);
            m\_ChartControl.Charts\[0\].Data = this.GenareteDataForCandle();
            m\_ChartControl.SuspendChartLayout();
            m\_ChartControl.LoadTheme("Manco.Chart.CF.Theme.xml");
            m\_ChartControl.Charts\[0\].ChartType = ChartType.Line;
    
            m\_ChartControl.Charts\[0\].ValueAxisList\[0\].Min = this.m\_dStokMin;
            m\_ChartControl.Charts\[0\].ValueAxisList\[0\].Max = this.m\_dStokMax;
    
            **workerThread2 = new Thread(new ThreadStart(AddAnnotations));
            workerThread2.Start();**
    
            m\_ChartControl.ResumeChartLayout();
        }
    

    This is my AddAnnotation function that will invoke the ResetChart() method

        public void AddAnnotations()
        {
            string toa = "calls";
            Annotation\[\] anno = null;
            int countA = 0;
            int arrayIndex = 0;
            while (true)
            {
    
                XmlDocument xmlPlogs = getAllPlogs(toa);
                XmlNodeList plogs = xmlPlogs.SelectNodes("/PlogList/Plog");
    
                foreach (XmlNode plog in plogs)
                {
                    XmlNodeList receivers = plog.SelectNodes("PlogUnique/Message/Receiver");
                    
                    foreach (DateTime dt in dateArray)
                    {
                        DateTime newDT = Convert.ToDateTime(plog.SelectSingleNode("PlogCommon/T
    
    V 1 Reply Last reply
    0
    • B benjamin yap

      Hi, I get this error despite create a delegate..

          private delegate void ResumeChart();
          ResumeChart ChartResume;
      
          private void ResumeChartLayout()
          {
              this.m\_ChartControl.ResumeChartLayout();
          }
      
          void ResetChart()
          {
              this.Invoke(ChartResume);
          }
      

      Inside my constructor I have this.

          public frmGraph(String stockQuote,String\[\] args)
          {
              InitializeComponent();
              newArgs = new String\[args.Length\];
              Array.Copy(args, newArgs, args.Length);
              token = newArgs\[0\];
              
              this.stockQuote = stockQuote;
              //this.stockQuote = "MGM";
              label1.Text = stockQuote;
              this.Text = stockQuote + " Graph";
              **ChartResume = ResumeChartLayout;**
          }
      

      Inside my form load, i create a new thread to call another function

          private void frmGraph\_Load(object sender, EventArgs e)
          {   
              FillDataTable();
      
              //SetFilter(DateTime.Now.AddDays(-9), DateTime.Now);
              m\_ChartControl.Charts\[0\].Data = this.GenareteDataForCandle();
              m\_ChartControl.SuspendChartLayout();
              m\_ChartControl.LoadTheme("Manco.Chart.CF.Theme.xml");
              m\_ChartControl.Charts\[0\].ChartType = ChartType.Line;
      
              m\_ChartControl.Charts\[0\].ValueAxisList\[0\].Min = this.m\_dStokMin;
              m\_ChartControl.Charts\[0\].ValueAxisList\[0\].Max = this.m\_dStokMax;
      
              **workerThread2 = new Thread(new ThreadStart(AddAnnotations));
              workerThread2.Start();**
      
              m\_ChartControl.ResumeChartLayout();
          }
      

      This is my AddAnnotation function that will invoke the ResetChart() method

          public void AddAnnotations()
          {
              string toa = "calls";
              Annotation\[\] anno = null;
              int countA = 0;
              int arrayIndex = 0;
              while (true)
              {
      
                  XmlDocument xmlPlogs = getAllPlogs(toa);
                  XmlNodeList plogs = xmlPlogs.SelectNodes("/PlogList/Plog");
      
                  foreach (XmlNode plog in plogs)
                  {
                      XmlNodeList receivers = plog.SelectNodes("PlogUnique/Message/Receiver");
                      
                      foreach (DateTime dt in dateArray)
                      {
                          DateTime newDT = Convert.ToDateTime(plog.SelectSingleNode("PlogCommon/T
      
      V Offline
      V Offline
      vivasaayi
      wrote on last edited by
      #2

      Try after changing the ResetChart function like this. void ResetChart() { this.m_ChartControl.Invoke(ChartResume); }

      B 1 Reply Last reply
      0
      • V vivasaayi

        Try after changing the ResetChart function like this. void ResetChart() { this.m_ChartControl.Invoke(ChartResume); }

        B Offline
        B Offline
        benjamin yap
        wrote on last edited by
        #3

        hmm.. i still get the same error :( could this be the error? under the AddAnnotations method. anno[countA] = m_ChartControl.Charts[0].AnnotationList.CreateAnnotation();

        V 1 Reply Last reply
        0
        • B benjamin yap

          hmm.. i still get the same error :( could this be the error? under the AddAnnotations method. anno[countA] = m_ChartControl.Charts[0].AnnotationList.CreateAnnotation();

          V Offline
          V Offline
          vivasaayi
          wrote on last edited by
          #4

          Yes. Previously I did not went through the AddAnnotation() function. Since you ChartControl is in the UI Thread, you can only access/modify the ChartControl properties by using a delegate. Just like you used a delegate to call the ChartResume method, use another Delegate to add annotation. This will surely solve the problem.

          B 1 Reply Last reply
          0
          • V vivasaayi

            Yes. Previously I did not went through the AddAnnotation() function. Since you ChartControl is in the UI Thread, you can only access/modify the ChartControl properties by using a delegate. Just like you used a delegate to call the ChartResume method, use another Delegate to add annotation. This will surely solve the problem.

            B Offline
            B Offline
            benjamin yap
            wrote on last edited by
            #5

            i have create another Delegate

                private delegate void AddAnnotation();
                AddAnnotation addNewAnnotation;
            
                private void CreateAnnotation()
                {
                    m\_ChartControl.Charts\[0\].AnnotationList.CreateAnnotation(); 
                }
                void cAnnotation()
                {
                    this.Invoke(addNewAnnotation);
                }
            

            and under the AddAnnoations method i change it to

            anno[countA] = cAnnotation();

            and i get this error Error 4 Cannot implicitly convert type 'void' to 'Manco.Chart.CF.Layout.Annotation' i tried change all the void to Annotation but still get error

            modified on Friday, December 11, 2009 12:00 PM

            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