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. Delegates and Threading Question

Delegates and Threading Question

Scheduled Pinned Locked Moved C#
graphicshelpquestionannouncement
3 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.
  • C Offline
    C Offline
    C Sharp Mage
    wrote on last edited by
    #1

    Hello guys, i am having a problem using Delegates, now i am working on an application where i have a label on a form and then i have a separate class in separate CS file (ie., Form1.cs and myClass.cs) what i am trying to do : i wanted to update label1.Text every tick of a timer with the format 00:00 next tick 00:01, then next tick 00:02 and so on so in my Class there is a timer(System.Timers.Timer/System.Threading.Timer/System.Windows.Forms.Timer) and i am trying to update the label1 in form1 using the following : Form1 : ----------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void setlabeltext(Label lbl, String strValue) { lbl.Text = strValue; } private void Form1_Load(object sender, EventArgs e) { Class1 myClass = new Class1(); myClass.startCall(); } } } myClass: -------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Timers = System.Windows.Forms.Timer; namespace WindowsApplication2 { class Class1 { Form1 form1 = new Form1(); Timers timoz = new Timers(); public void timoz_Tick(object sender, EventArgs e) { ThreadSafeSetLable(form1.label1, DateTime.Now.Subtract(DateTime.Now).ToString()); } private void ThreadSafeSetLable(Label lbl, string value) { if (form1.InvokeRequired) { form1.Invoke(new SetString(form1.setlabeltext), new object[] { lbl, value }); } else { form1.setlabeltext(lbl, value); } } private delegate void SetString(Label txtBox, string strValue); public void startCall() { timoz.Interval = 1000; timoz.Start(); timoz.Tick += new EventHandler(timoz_Tick); } } } this is not working i dont know why , it compiles no problem , in the debugging the label1.text is changing to 00:00:00 but runtime i dont see that , plus the

    C 1 Reply Last reply
    0
    • C C Sharp Mage

      Hello guys, i am having a problem using Delegates, now i am working on an application where i have a label on a form and then i have a separate class in separate CS file (ie., Form1.cs and myClass.cs) what i am trying to do : i wanted to update label1.Text every tick of a timer with the format 00:00 next tick 00:01, then next tick 00:02 and so on so in my Class there is a timer(System.Timers.Timer/System.Threading.Timer/System.Windows.Forms.Timer) and i am trying to update the label1 in form1 using the following : Form1 : ----------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void setlabeltext(Label lbl, String strValue) { lbl.Text = strValue; } private void Form1_Load(object sender, EventArgs e) { Class1 myClass = new Class1(); myClass.startCall(); } } } myClass: -------------- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Timers = System.Windows.Forms.Timer; namespace WindowsApplication2 { class Class1 { Form1 form1 = new Form1(); Timers timoz = new Timers(); public void timoz_Tick(object sender, EventArgs e) { ThreadSafeSetLable(form1.label1, DateTime.Now.Subtract(DateTime.Now).ToString()); } private void ThreadSafeSetLable(Label lbl, string value) { if (form1.InvokeRequired) { form1.Invoke(new SetString(form1.setlabeltext), new object[] { lbl, value }); } else { form1.setlabeltext(lbl, value); } } private delegate void SetString(Label txtBox, string strValue); public void startCall() { timoz.Interval = 1000; timoz.Start(); timoz.Tick += new EventHandler(timoz_Tick); } } } this is not working i dont know why , it compiles no problem , in the debugging the label1.text is changing to 00:00:00 but runtime i dont see that , plus the

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Tamer A.Rahman wrote:

      Form1 form1 = new Form1();

      I'm not sure what you hope this to do, but it will create a new instance of Form1, which is in no way related to the one that is visible on your screen. Your controls should also not be made public on the class, delegates are used to avoid such nastiness. A delegate defined in class1, should call a method in form1, and pass in the string to set. The method should assign the string to the label, and class1 calls the delegate whenever it likes, via your timer or whatever.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      C 1 Reply Last reply
      0
      • C Christian Graus

        Tamer A.Rahman wrote:

        Form1 form1 = new Form1();

        I'm not sure what you hope this to do, but it will create a new instance of Form1, which is in no way related to the one that is visible on your screen. Your controls should also not be made public on the class, delegates are used to avoid such nastiness. A delegate defined in class1, should call a method in form1, and pass in the string to set. The method should assign the string to the label, and class1 calls the delegate whenever it likes, via your timer or whatever.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        C Offline
        C Offline
        C Sharp Mage
        wrote on last edited by
        #3

        well, thank you for the reply, i got it already :) it was the wrong approach from the begining and i fixed it thanks for all the help anyway :)

        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