event in static class/method

J

jweizman

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
 
M

Marc Gravell

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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);
}
}


|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
|
 
S

Stoitcho Goutsev \(100\)

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);
}
}

}
 
J

jweizman

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
 
M

Marc Gravell

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
 
J

jweizman

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
 
S

Samuel R. Neff

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top