473,324 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problems with Delegates

Hello guys,

i am having a problem using Delegates, i am pretty new to C# , i am
starting to fall in love with it its really good language (coming from
Java and Delphi),

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 form1.InvokeRequired never gets to "true" , i do
realize that i am missing something but i dont know what , can i have
help on that ? thanx

May 13 '07 #1
7 1458
On Sun, 13 May 2007 08:51:54 -0700, <tr*****@gmail.comwrote:
[...]
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 form1.InvokeRequired never gets to "true" , i do
realize that i am missing something but i dont know what , can i have
help on that ? thanx
Well, the most obvious issue that I see is that the form on which you're
setting the text isn't the same form that you're displaying. You need to
pass "this" in to the Class1 somehow (perhaps via the constructor, for
example) so that it can use *that* reference, rather than creating a new
form (as it does now). This for sure explains why the text on the
displayed form isn't updated, and probably explains why
form1.InvokeRequired is never true (since form1 isn't shown yet,
cross-thread invocation may not be a problem yet).

One other thing I'll point out is that the mechanism you're using,
checking InvokeRequired, is only necessary if you use the same method for
the actual Invoke. That is, if in ThreadSafeSetLabel your delegate that
you Invoke on the form is created from ThreadSafeSetLabel itself, then you
need the check. But if the thread-safe method is invoking some other
method, then you don't need to check InvokeRequired. You can just always
create the delegate and then invoke it on the form.

Pete
May 13 '07 #2
On May 13, 12:24 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Sun, 13 May 2007 08:51:54 -0700, <traa...@gmail.comwrote:
[...]
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 form1.InvokeRequired never gets to "true" , i do
realize that i am missing something but i dont know what , can i have
help on that ? thanx

Well, the most obvious issue that I see is that the form on which you're
setting the text isn't the same form that you're displaying. You need to
pass "this" in to the Class1 somehow (perhaps via the constructor, for
example) so that it can use *that* reference, rather than creating a new
form (as it does now). This for sure explains why the text on the
displayed form isn't updated, and probably explains why
form1.InvokeRequired is never true (since form1 isn't shown yet,
cross-thread invocation may not be a problem yet).

One other thing I'll point out is that the mechanism you're using,
checking InvokeRequired, is only necessary if you use the same method for
the actual Invoke. That is, if in ThreadSafeSetLabel your delegate that
you Invoke on the form is created from ThreadSafeSetLabel itself, then you
need the check. But if the thread-safe method is invoking some other
method, then you don't need to check InvokeRequired. You can just always
create the delegate and then invoke it on the form.

Pete
Thank you very much for the fast reply , OMG i am really loving this
language and its support i can tell its booming :)
anyway would mind if you show me an example of how to pass "this"
using Class1?

like should i put

public void Class1(Form1 frm)
{

}

and then in form1 on load i put,
private void Form1_Load(object sender, EventArgs e)
{
Class1 myClass = new Class1(form1);
myClass.startCall();
}
is that how it is ?
can you please provide me with examples?

thanks

May 13 '07 #3
"CSharper" <tr*****@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
[...]
anyway would mind if you show me an example of how to pass "this"
using Class1?
[...]
and then in form1 on load i put,
private void Form1_Load(object sender, EventArgs e)
{
Class1 myClass = new Class1(form1);
[...]
No, you would do it like this:

Class1 myClass = new Class1(this);

Since this sentence is written inside your executing Form1, the "this"
will pass the specific instance of the form1 that Class1 needs to receive.

May 13 '07 #4
On May 13, 2:41 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"CSharper" <traa...@gmail.comwrote in message

news:11**********************@k79g2000hse.googlegr oups.com...
[...]
anyway would mind if you show me an example of how to pass "this"
using Class1?
[...]
and then in form1 on load i put,
private void Form1_Load(object sender, EventArgs e)
{
Class1 myClass = new Class1(form1);
[...]

No, you would do it like this:

Class1 myClass = new Class1(this);

Since this sentence is written inside your executing Form1, the "this"
will pass the specific instance of the form1 that Class1 needs to receive.
yea, but how in Class1 i would handle form1?

i mean how should i adjust Class1 then ?

May 13 '07 #5
"CSharper" <tr*****@gmail.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
yea, but how in Class1 i would handle form1?

i mean how should i adjust Class1 then ?
using Timers = System.Windows.Forms.Timer;

public class Class1
{
private Form1 theForm;

public void Class1(Form1 frm)
{
theForm = frm;

Timers timoz = new Timers();
timoz.Interval = 1000;
timoz.Tick += new EventHandler(timoz_Tick);
timoz.Start();
}
....
public void timoz_Tick(object sender, EventArgs e)
{
theForm.setlabeltext(theForm.Label1, theValue);
}
}
Note that you don't have to use Invoke or BeginInvoke. This is because you
are using a System.Windows.Forms.Timer, which fires the event in the same
thread as the form is running. If you were using a System.Timers.Timer or a
System.Threading.Timer you would have to do the thread marshalling with one
of those methods.

May 13 '07 #6
On May 13, 3:59 pm, "Alberto Poblacion" <earthling-
quitaestoparacontes...@poblacion.orgwrote:
"CSharper" <traa...@gmail.comwrote in message

news:11*********************@w5g2000hsg.googlegrou ps.com...
yea, but how in Class1 i would handle form1?
i mean how should i adjust Class1 then ?

using Timers = System.Windows.Forms.Timer;

public class Class1
{
private Form1 theForm;

public void Class1(Form1 frm)
{
theForm = frm;

Timers timoz = new Timers();
timoz.Interval = 1000;
timoz.Tick += new EventHandler(timoz_Tick);
timoz.Start();
}
...
public void timoz_Tick(object sender, EventArgs e)
{
theForm.setlabeltext(theForm.Label1, theValue);
}

}

Note that you don't have to use Invoke or BeginInvoke. This is because you
are using a System.Windows.Forms.Timer, which fires the event in the same
thread as the form is running. If you were using a System.Timers.Timer or a
System.Threading.Timer you would have to do the thread marshalling with one
of those methods.
thanx much you are the best it works now :)

May 13 '07 #7
Hi,

about the InvokeRequired question.
Your using the Windows.Forms.Timer. This uses the GUI message loop to notify
ticks. So the Tick event gets called on the UI-Thred like other GUI events.
That's why Invoke is never required in your example.

Christof
May 14 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.