473,398 Members | 2,427 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,398 software developers and data experts.

event in static class/method

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
9 3987
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
Hi,

What error you get?
Jan 24 '07 #3
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
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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: SunshineGirl | last post by:
In my code, class A instanciates classes B and C. I would like class B to connect an event handler to a method in class A, and for class C to disconnect that event handler. I think I've done...
18
by: Elder Hyde | last post by:
Hey all, A class of mine needs to tell the outside world when its buffer is not empty. The problem is that C# seems to force you to put the event-raising code in the base class. To illustrate,...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
29
by: Patrick | last post by:
I have the following code, which regardless which works fine and logs to the EventViewer regardless of whether <processModel/> section of machine.config is set to username="SYSTEM" or "machine" ...
8
by: Karsten Schramm | last post by:
Hi, when I run the following code: using System; namespace ConsoleApplication22 { class Program {
3
by: =?Utf-8?B?TkVXMi5ORVQ=?= | last post by:
I have a static event declared in a C++ ref class, that can then be handled in a VB app. I'm trying to expose the static event through the interface that the C++ ref class implements so the VB app...
9
by: None | last post by:
Hi, I'm facing a problem with static instances. I have created a class called CustomList by deriving the class List<int>. Inside the CustomList i have created Remove event (when any item is...
12
by: Steve | last post by:
Hi All, I'm a newbee in C# and have a Windows form application. The main form has a progress bar. In one of the methods of the main form, I call a class derived from Object which processes a...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.