"global" singleton

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pawel Janik

    #1

    "global" singleton

    Hello.

    I'd like to create utility, that writes something to a file, but this
    utility shoud be easy to access from several different applications.
    Because this utility have to open a file, it shoud do this only once (there
    is one class instance, but every application has a acces to it). What
    technology use here? How to do this?

    Pawel Janik


  • Daniel Bass

    #2
    "global&qu ot; singleton

    You've probably seen Jon Skeet around here, I found his
    website useful in this regard.



    Later.
    Dan.[color=blue]
    >-----Original Message-----
    >Hello.
    >
    >I'd like to create utility, that writes something to a[/color]
    file, but this[color=blue]
    >utility shoud be easy to access from several different[/color]
    applications.[color=blue]
    >Because this utility have to open a file, it shoud do[/color]
    this only once (there[color=blue]
    >is one class instance, but every application has a acces[/color]
    to it). What[color=blue]
    >technology use here? How to do this?
    >
    >Pawel Janik
    >
    >
    >.
    >[/color]

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: "global&qu ot; singleton

      Pawel,

      Personally, I would run an out-of-process serviced component that you
      only allow one instance of (by setting it to run in a pool, the max and min
      size of the pool being one). However, you can also use remoting to have one
      instance on a machine, and then access the remote object which accesses the
      file.

      I think that you might get better results though if you allow multiple
      instances of the class to be created, and place the appropriate locks on the
      file. Your class isn't the only thing that can potentially open the file,
      so placing locks on the file level would be a better way to go, IMO.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - nick(dot)paldin o=at=exisconsul ting<dot>com

      "Pawel Janik" <janos@gazeta.p l> wrote in message
      news:blhen0$akk $1@inews.gazeta .pl...[color=blue]
      > Hello.
      >
      > I'd like to create utility, that writes something to a file, but this
      > utility shoud be easy to access from several different applications.
      > Because this utility have to open a file, it shoud do this only once[/color]
      (there[color=blue]
      > is one class instance, but every application has a acces to it). What
      > technology use here? How to do this?
      >
      > Pawel Janik
      >
      >[/color]


      Comment

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

        #4
        Re: &quot;global&qu ot; singleton

        Hi,

        You could use a web service for this. or otherwise create a dedicated
        server, this can be for example a window service.

        To can communicate with this service either using remoting, a TCP
        connection or any other medium.

        Cheers,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation

        "Pawel Janik" <janos@gazeta.p l> wrote in message
        news:blhen0$akk $1@inews.gazeta .pl...[color=blue]
        > Hello.
        >
        > I'd like to create utility, that writes something to a file, but this
        > utility shoud be easy to access from several different applications.
        > Because this utility have to open a file, it shoud do this only once[/color]
        (there[color=blue]
        > is one class instance, but every application has a acces to it). What
        > technology use here? How to do this?
        >
        > Pawel Janik
        >
        >[/color]


        Comment

        • Mr.Tickle

          #5
          Re: &quot;global&qu ot; singleton

          Then make it a process and other apps can send a message to that process?



          "Pawel Janik" <janos@gazeta.p l> wrote in message
          news:blhen0$akk $1@inews.gazeta .pl...[color=blue]
          > Hello.
          >
          > I'd like to create utility, that writes something to a file, but this
          > utility shoud be easy to access from several different applications.
          > Because this utility have to open a file, it shoud do this only once[/color]
          (there[color=blue]
          > is one class instance, but every application has a acces to it). What
          > technology use here? How to do this?
          >
          > Pawel Janik
          >
          >[/color]


          Comment

          • Pawel Janik

            #6
            Re: &quot;global&qu ot; singleton

            Helo.

            Thanks for all answers![color=blue]
            > Personally, I would run an out-of-process serviced component that you
            > only allow one instance of (by setting it to run in a pool, the max and[/color]
            min[color=blue]
            > size of the pool being one). However, you can also use remoting to have[/color]
            one[color=blue]
            > instance on a machine, and then access the remote object which accesses[/color]
            the[color=blue]
            > file.[/color]
            Thanks, servised component shoud be OK....remoting? isn't it to slow while
            communicating on localhost?
            [color=blue]
            >
            > I think that you might get better results though if you allow multiple
            > instances of the class to be created, and place the appropriate locks on[/color]
            the[color=blue]
            > file. Your class isn't the only thing that can potentially open the file,
            > so placing locks on the file level would be a better way to go, IMO.[/color]
            Ok, it seems to be a good solution...but what this sample doesn't work (and
            does not throw any exception?)

            using System;
            using System.IO;
            using System.Text;

            class Test
            {
            public static void Main()
            {
            string path = "MyTest.txt ";

            Byte[] info1 = new UTF8Encoding(tr ue).GetBytes("T his is some text in
            the file. 1\r\n");
            Byte[] info2 = new UTF8Encoding(tr ue).GetBytes("T his is some text in
            the file. 2\r\n");

            FileStream fs1 = File.Open(path, FileMode.Append , FileAccess.Writ e,
            FileShare.Write );
            fs1.Write(info1 , 0, info1.Length);
            Console.WriteLi ne("zapisalem") ;
            FileStream fs2 = File.Open(path, FileMode.Append , FileAccess.Writ e,
            FileShare.Write );
            fs1.Write(info1 , 0, info1.Length);
            Console.WriteLi ne("zapisalem") ;
            fs2.Write(info2 , 0, info2.Length);
            Console.WriteLi ne("zapisalem") ;
            fs2.Write(info2 , 0, info2.Length);
            Console.WriteLi ne("zapisalem") ;
            fs2.Close();
            fs1.Close();
            }
            }

            Pawel Janik


            Comment

            Working...