Global ( sort of) event ?
-
Hi, I have a class library, which actuall is a little application itself. It does communication with our server, starts a download queue and autodownloader and does some background processing. This to provide data and files for the different host app's that are using this class library ( see it as a communications client) now in the library, i have some events that need to be received by the host app like for example: FileCountChanged. the problems is, this can be triggered from The AutoDownloader because it finished a download, from the TCP Client cause the server told him to delete a file etc etc.. Now my question is: is there a way to raise a global event from different classes ? The more or less same question applies to variables ( objects). Can i change the variable of my main class from its sub classes ? Something like for example:
public delegate void FileCountChangedEventHandler(FileType filetype,int count); public class MyClient { // Should be raised if: // File get's deleted, File is added, // This can be initiated from many different classes... // How do public event FilecountChanged(FileType filetype,int count); private DownloadQueue _dlqueue = new _dlqueue(); // I should be able to add and remove downloads in this queue from anywhere HOWEVER.. it must be this queue. Do i have to pass this to // all the classes that need to use it ? And do i use ref or just normal ? private AutoDownloader dl = null; public Myclient() { dl = new AutoDownloader(_dlqueue); dl.start(); dl.DownloadCompleted += new eventhandler(ondownloadcompleted); } }
Thanks !Do Or Don't, there is no "try catch ex as exception end try"
-
Hi, I have a class library, which actuall is a little application itself. It does communication with our server, starts a download queue and autodownloader and does some background processing. This to provide data and files for the different host app's that are using this class library ( see it as a communications client) now in the library, i have some events that need to be received by the host app like for example: FileCountChanged. the problems is, this can be triggered from The AutoDownloader because it finished a download, from the TCP Client cause the server told him to delete a file etc etc.. Now my question is: is there a way to raise a global event from different classes ? The more or less same question applies to variables ( objects). Can i change the variable of my main class from its sub classes ? Something like for example:
public delegate void FileCountChangedEventHandler(FileType filetype,int count); public class MyClient { // Should be raised if: // File get's deleted, File is added, // This can be initiated from many different classes... // How do public event FilecountChanged(FileType filetype,int count); private DownloadQueue _dlqueue = new _dlqueue(); // I should be able to add and remove downloads in this queue from anywhere HOWEVER.. it must be this queue. Do i have to pass this to // all the classes that need to use it ? And do i use ref or just normal ? private AutoDownloader dl = null; public Myclient() { dl = new AutoDownloader(_dlqueue); dl.start(); dl.DownloadCompleted += new eventhandler(ondownloadcompleted); } }
Thanks !Do Or Don't, there is no "try catch ex as exception end try"
Classes shouldn't be raising events that are defined in other classes. If a class
MyClient
contains some event then it should be up to thatMyClient
object to determine when to fire it. If other classes need to manipulate the internal members of theMyClient
then there should be some methods to allow controlled access to them. Some of those methods could in turn cause the internal events to be fired. If you want child classes to be able to add/remove/etc stuff fromMyClient
s Q, the first job is to make theMyClient
accessible by whatever objects need it, either by passing around a reference to it, making a static singleton, or by some other method. Then you can add methods to theMyClient
like this:class MyClient
{
public void AddToQueue(object thingToQ)
{
// do whatever you need to add the item to the Qif (FilecountChanged != null) { // fire the event to notify other clients }
}
}P.S.
code
tags make text red,pre
tags preserve formatting so they're better for code snippets -
Classes shouldn't be raising events that are defined in other classes. If a class
MyClient
contains some event then it should be up to thatMyClient
object to determine when to fire it. If other classes need to manipulate the internal members of theMyClient
then there should be some methods to allow controlled access to them. Some of those methods could in turn cause the internal events to be fired. If you want child classes to be able to add/remove/etc stuff fromMyClient
s Q, the first job is to make theMyClient
accessible by whatever objects need it, either by passing around a reference to it, making a static singleton, or by some other method. Then you can add methods to theMyClient
like this:class MyClient
{
public void AddToQueue(object thingToQ)
{
// do whatever you need to add the item to the Qif (FilecountChanged != null) { // fire the event to notify other clients }
}
}P.S.
code
tags make text red,pre
tags preserve formatting so they're better for code snippetsHi, Thanks for the reply. If i understand correctly, making a class a static singleton( have never done that before :s) would make it availble for calling in the Child class, but the instance of the singleton class that the child gets, would be the same instance as the one the host app would have right ? I'm a VB.net converty ANd relativly new to development so i am still discovering some of the (c#) syntax and possibilities...
Do Or Don't, there is no "try catch ex as exception end try"
-
Hi, Thanks for the reply. If i understand correctly, making a class a static singleton( have never done that before :s) would make it availble for calling in the Child class, but the instance of the singleton class that the child gets, would be the same instance as the one the host app would have right ? I'm a VB.net converty ANd relativly new to development so i am still discovering some of the (c#) syntax and possibilities...
Do Or Don't, there is no "try catch ex as exception end try"
Correct. Your most basic static singleton looks like this:
public class MyClass
{
private static MyClass instance = new MyClass();
public static MyClass Instance
{
get { return instance; }
}public void DoSomething() { Console.WriteLine("Something"); }
}
and then all of the objects in your program can access it like this:
MyClass.Instance.DoSomething();
If you have lots of multi-threading in your program then here are some tricks to handle thread-safety with the static object, but in simple programs the basic stuff should suffice.