Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Global ( sort of) event ?

Global ( sort of) event ?

Scheduled Pinned Locked Moved C#
questioncsssysadmindata-structurestutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Noctris
    wrote on last edited by
    #1

    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"

    J 1 Reply Last reply
    0
    • N Noctris

      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"

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      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 that MyClient object to determine when to fire it. If other classes need to manipulate the internal members of the MyClient 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 from MyClients Q, the first job is to make the MyClient 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 the MyClient like this:

      class MyClient
      {
      public void AddToQueue(object thingToQ)
      {
      // do whatever you need to add the item to the Q

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

      N 1 Reply Last reply
      0
      • J Jimmanuel

        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 that MyClient object to determine when to fire it. If other classes need to manipulate the internal members of the MyClient 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 from MyClients Q, the first job is to make the MyClient 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 the MyClient like this:

        class MyClient
        {
        public void AddToQueue(object thingToQ)
        {
        // do whatever you need to add the item to the Q

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

        N Offline
        N Offline
        Noctris
        wrote on last edited by
        #3

        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"

        J 1 Reply Last reply
        0
        • N Noctris

          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"

          J Offline
          J Offline
          Jimmanuel
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups