By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,465 Members | 1,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,465 IT Pros & Developers. It's quick & easy.

event in static class/method

P: n/a
I have a static method doing a long operation and i would like to
notify the user about the progress.

However when i try to use and event i does not compile...
public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;

public static void compute(float a,float b)
{
for (int i=0,i++;i<1000)
{
//bla,bla
if (ReportDDProgress!=null)
ReportDDProgress(i);
}
}

}

Thanks
John

Jan 24 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
The compile error is you "for" statement; should be for (int i = 0; i
< 1000; i++)

Other notes:
* I recommend sticking to the "sender, args" pattern. It is what is
generally expected. This will work, though.
* Note that this could still error in a threaded environment if
another thread unsubscribes the final handler between checking the
handler and invoking it; general practice is to either lock and/or to
take a snapshot of the handler:
ComputeProgress handler = ReportDDProgress;
if(handler!=null) handler(i);
* Note that this won't (by itself) make your UI update itself if the
UI thread is the thread driving the method (not sure if that is the
intent)
* In a win-form scenario, perhaps also look at BackgroundWorker -
handles a lot of this for you.

Marc
Jan 24 '07 #2

P: n/a
Hi,

What error you get?
Jan 24 '07 #3

P: n/a
Hi ,
I tried and it worked fine.

Your code had some syntax errors, probably of transcript, in any case this
is a running code:
As you will note I simply paste your code in the project I had open ( a win
app) I placed a breakpoint in the line string s = i.ToString(); and it
worked.

public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;

public static void compute(float a,float b)
{
for (int i=0;i<1000; i++)
{
//bla,bla
if (ReportDDProgress!=null)
ReportDDProgress(i);
}
}

}

public class Form1 : System.Windows.Forms.Form
{
void h(int i)
{
string s = i.ToString();
}
public Form1()
{
AWSTasks.ReportDDProgress += new AWSTasks.ComputeProgress( h);
AWSTasks.compute( 0,0);
}
}
"jweizman" <jw******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
|I have a static method doing a long operation and i would like to
| notify the user about the progress.
|
| However when i try to use and event i does not compile...
|
|
| public class AWSTasks
| {
| public delegate void ComputeProgress(int percentage);
| public static event ComputeProgress ReportDDProgress;
|
| public static void compute(float a,float b)
| {
| for (int i=0,i++;i<1000)
| {
| //bla,bla
| if (ReportDDProgress!=null)
| ReportDDProgress(i);
| }
| }
|
| }
|
| Thanks
| John
|
Jan 24 '07 #4

P: n/a
jweizman,

The compiler clearly states what your errors are. You have 2 syntax errors
int *for* loop

1. The three part of the for loop must be separate by ';' you use ',' in one
of the places
2. The three parts have to appeare in a predefined order -
initializer,condition and iterator. In your case you put condition and
iterator in wrong places.

Here is the fixed code

public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;

public static void compute(float a, float b)
{
for (int i = 0; i < 1000;i++)
{
//bla,bla
if (ReportDDProgress != null)
ReportDDProgress(i);
}
}

}
--
Stoitcho Goutsev (100)

"jweizman" <jw******@gmail.comwrote in message
news:11*********************@l53g2000cwa.googlegro ups.com...
>I have a static method doing a long operation and i would like to
notify the user about the progress.

However when i try to use and event i does not compile...
public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;

public static void compute(float a,float b)
{
for (int i=0,i++;i<1000)
{
//bla,bla
if (ReportDDProgress!=null)
ReportDDProgress(i);
}
}

}

Thanks
John

Jan 24 '07 #5

P: n/a
Thanks everyone for your answers.

The for error was a typing mistake since i did not copy/paste.
Ignacio gave the anwser.

Now what is the meaning of a static event ? how it differ from a non
static event ?

I do not feel cumfortable with this notion

On 24 jan, 16:29, "Stoitcho Goutsev \(100\)" <1...@100.comwrote:
jweizman,

The compiler clearly states what your errors are. You have 2 syntax errors
int *for* loop

1. The three part of the for loop must be separate by ';' you use ',' in one
of the places
2. The three parts have to appeare in a predefined order -
initializer,condition and iterator. In your case you put condition and
iterator in wrong places.

Here is the fixed code

public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;

public static void compute(float a, float b)
{
for (int i = 0; i < 1000;i++)
{
//bla,bla
if (ReportDDProgress != null)
ReportDDProgress(i);
}
}

}
--
Stoitcho Goutsev (100)

"jweizman" <jweiz...@gmail.comwrote in messagenews:11*********************@l53g2000cwa.go oglegroups.com...
I have a static method doing a long operation and i would like to
notify the user about the progress.
However when i try to use and event i does not compile...
public class AWSTasks
{
public delegate void ComputeProgress(int percentage);
public static event ComputeProgress ReportDDProgress;
public static void compute(float a,float b)
{
for (int i=0,i++;i<1000)
{
//bla,bla
if (ReportDDProgress!=null)
ReportDDProgress(i);
}
}
}
Thanks
John- Masquer le texte des messages précédents -- Afficher le textedes messages précédents -
Jan 24 '07 #6

P: n/a
As with other statics, the event is declared (once) for the type,
rather than for each individual instance. This is rarely a good idea;
apart from other considerations, it can lead to leaks if lots of
things subscribe but forget to unsubscribe (as they will then be
visible from the static event for all eternity and will never be
garbage collected unless a WeakReference is used as a break).

Marc
Jan 24 '07 #7

P: n/a

ok, but is there an alternative then ?

On 24 jan, 16:57, "Marc Gravell" <marc.grav...@gmail.comwrote:
As with other statics, the event is declared (once) for the type,
rather than for each individual instance. This is rarely a good idea;
apart from other considerations, it can lead to leaks if lots of
things subscribe but forget to unsubscribe (as they will then be
visible from the static event for all eternity and will never be
garbage collected unless a WeakReference is used as a break).

Marc
Jan 24 '07 #8

P: n/a
Also Mark, you quoted before :
"* Note that this won't (by itself) make your UI update itself if the
UI thread is the thread driving the method (not sure if that is the
intent) "

How to display in UI then ?

Thanks

On 24 jan, 17:07, "jweizman" <jweiz...@gmail.comwrote:
ok, but is there an alternative then ?

On 24 jan, 16:57, "Marc Gravell" <marc.grav...@gmail.comwrote:
As with other statics, the event is declared (once) for the type,
rather than for each individual instance. This is rarely a good idea;
apart from other considerations, it can lead to leaks if lots of
things subscribe but forget to unsubscribe (as they will then be
visible from the static event for all eternity and will never be
garbage collected unless a WeakReference is used as a break).
Marc- Masquer le texte des messages précédents -- Afficher le textedes messages précédents -
Jan 24 '07 #9

P: n/a

In this particular case since you can be running multiple
simulataneous computations you can either move the compute method to
be an instance method and use an instance event, or you can accept a
callback parameter on the compute method itself. Something like:

public static void compute(float a,float b, ComputeProgress callback)
{
for (int i=0; ;i<1000; i++)
{
//bla,bla
if (callback!=null)
callback(i);
}
}

That way each caller can have it's own callback, and it's still
optional. You could use either a delegate for the callback or an
interface.

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On 24 Jan 2007 08:07:48 -0800, "jweizman" <jw******@gmail.comwrote:
>
ok, but is there an alternative then ?

On 24 jan, 16:57, "Marc Gravell" <marc.grav...@gmail.comwrote:
>As with other statics, the event is declared (once) for the type,
rather than for each individual instance. This is rarely a good idea;
apart from other considerations, it can lead to leaks if lots of
things subscribe but forget to unsubscribe (as they will then be
visible from the static event for all eternity and will never be
garbage collected unless a WeakReference is used as a break).

Marc
Jan 24 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.