Class used by multiple threads to write to Table

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

    Class used by multiple threads to write to Table

    I want to make a class that will write data to a in memory table. This
    class will be called by multiple threads. I'm hoping the table can stay
    in memory between calls and all the call by the thread will have to do
    is pass some data to be added to the table. Kinda like this class I
    have to write to a log file.

    Public Class MyStringLogger
    Private Shared m_loglock As New Object

    Public Shared Sub Write(ByVal fileName As String, ByVal strToWrite
    As String)
    SyncLock (m_loglock)
    Try
    Dim sw As New System.io.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    sw.Close()
    Catch
    End Try
    End SyncLock
    End Sub
    End Class

    Any idea how I'd do this?
  • Cor Ligthert [MVP]

    #2
    Re: Class used by multiple threads to write to Table

    cj,

    Are you sure that you don't need the queue class which is very standard for
    this?

    Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.


    I hope this helps,

    Cor

    "cj" <cj@nospam.nosp amschreef in bericht
    news:OenEEPunGH A.4892@TK2MSFTN GP03.phx.gbl...
    >I want to make a class that will write data to a in memory table. This
    >class will be called by multiple threads. I'm hoping the table can stay in
    >memory between calls and all the call by the thread will have to do is pass
    >some data to be added to the table. Kinda like this class I have to write
    >to a log file.
    >
    Public Class MyStringLogger
    Private Shared m_loglock As New Object
    >
    Public Shared Sub Write(ByVal fileName As String, ByVal strToWrite As
    String)
    SyncLock (m_loglock)
    Try
    Dim sw As New System.io.Strea mWriter(fileNam e, True)
    sw.WriteLine(st rToWrite)
    sw.Close()
    Catch
    End Try
    End SyncLock
    End Sub
    End Class
    >
    Any idea how I'd do this?

    Comment

    • Jeffrey Tan[MSFT]

      #3
      RE: Class used by multiple threads to write to Table

      Hi Cj,

      It seems Cor Ligthert [MVP] provided an incorrect URL for queue class, I
      think he is referring the URL below:
      "Queue Class"

      frlrfsystemcoll ectionsqueuecla sstopic.asp

      There are several container classes in .Net may fit your need, such as
      ArrayList, Hashtable, Queue etc..., they all exist under System.Collecti ons
      namespace. They all have different usages, you may refer to MSDN for their
      difference and choose the suitable for using as memory container table.

      Hope this helps!

      Best regards,
      Jeffrey Tan
      Microsoft Online Community Support
      =============== =============== =============== =====
      When responding to posts, please "Reply to Group" via your newsreader so
      that others may learn and benefit from your issue.
      =============== =============== =============== =====
      This posting is provided "AS IS" with no warranties, and confers no rights.

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Class used by multiple threads to write to Table

        Jeffrey,

        Thank you, I did not copy it this was from a previous answer, mine was



        A little bit shorter than your URL.

        By the way, the queue class (FIFO) is extremely sufficient with multi
        threading.

        Cor

        ""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.comsch reef in bericht
        news:IIsQlK1nGH A.4612@TK2MSFTN GXA01.phx.gbl.. .
        Hi Cj,
        >
        It seems Cor Ligthert [MVP] provided an incorrect URL for queue class, I
        think he is referring the URL below:
        "Queue Class"

        frlrfsystemcoll ectionsqueuecla sstopic.asp
        >
        There are several container classes in .Net may fit your need, such as
        ArrayList, Hashtable, Queue etc..., they all exist under
        System.Collecti ons
        namespace. They all have different usages, you may refer to MSDN for their
        difference and choose the suitable for using as memory container table.
        >
        Hope this helps!
        >
        Best regards,
        Jeffrey Tan
        Microsoft Online Community Support
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no
        rights.
        >

        Comment

        • cj

          #5
          Re: Class used by multiple threads to write to Table

          I'm don't think that's what I'm looking for. My program creates threads
          to handle IP connections made to it. I've seen up to 66 concurrent
          threads running to date. But usually it's around 3 or 4. For
          accountability purposes each thread uses the MyStringLogger class I
          posted in my first message to write it's results to a log file.
          SyncLock is used to ensure only one thread is writing data at a time.

          Now I'm trying to write into this program another type of log. Each
          thread will be required to write two fields to an in memory db table.
          Why--Each thread receives a request from a IP client then calculates and
          sends a response to it. Part of the calculations involve other machines
          and if the network is down the threads can't calculate the response so
          they return an error code. In the future if the network is down I want
          the program to search the new in memory db table for the last time that
          question was asked by an ip client and return the same answer as it will
          most likely still be correct.

          Perhaps hourly a maintenance routine will be run to remove entries from
          this in memory db table older than X hours or minutes or whatever is
          determined to be appropriate.

          Any suggestions?


          Cor Ligthert [MVP] wrote:
          Jeffrey,
          >
          Thank you, I did not copy it this was from a previous answer, mine was
          >

          >
          A little bit shorter than your URL.
          >
          By the way, the queue class (FIFO) is extremely sufficient with multi
          threading.
          >
          Cor
          >
          ""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.comsch reef in bericht
          news:IIsQlK1nGH A.4612@TK2MSFTN GXA01.phx.gbl.. .
          >Hi Cj,
          >>
          >It seems Cor Ligthert [MVP] provided an incorrect URL for queue class, I
          >think he is referring the URL below:
          >"Queue Class"
          >http://msdn.microsoft.com/library/de...us/cpref/html/
          >frlrfsystemcol lectionsqueuecl asstopic.asp
          >>
          >There are several container classes in .Net may fit your need, such as
          >ArrayList, Hashtable, Queue etc..., they all exist under
          >System.Collect ions
          >namespace. They all have different usages, you may refer to MSDN for their
          >difference and choose the suitable for using as memory container table.
          >>
          >Hope this helps!
          >>
          >Best regards,
          >Jeffrey Tan
          >Microsoft Online Community Support
          >============== =============== =============== ======
          >When responding to posts, please "Reply to Group" via your newsreader so
          >that others may learn and benefit from your issue.
          >============== =============== =============== ======
          >This posting is provided "AS IS" with no warranties, and confers no
          >rights.
          >>
          >
          >

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Class used by multiple threads to write to Table

            cj,

            Reading it, and trying to understand it, would I go to the sorted list (with
            a good created key) or the hashtable with the same as already stated by
            Jeffrey,

            Cor

            "cj" <cj@nospam.nosp amschreef in bericht
            news:uQJyWnDoGH A.776@TK2MSFTNG P04.phx.gbl...
            I'm don't think that's what I'm looking for. My program creates threads
            to handle IP connections made to it. I've seen up to 66 concurrent
            threads running to date. But usually it's around 3 or 4. For
            accountability purposes each thread uses the MyStringLogger class I posted
            in my first message to write it's results to a log file. SyncLock is used
            to ensure only one thread is writing data at a time.
            >
            Now I'm trying to write into this program another type of log. Each
            thread will be required to write two fields to an in memory db table.
            Why--Each thread receives a request from a IP client then calculates and
            sends a response to it. Part of the calculations involve other machines
            and if the network is down the threads can't calculate the response so
            they return an error code. In the future if the network is down I want
            the program to search the new in memory db table for the last time that
            question was asked by an ip client and return the same answer as it will
            most likely still be correct.
            >
            Perhaps hourly a maintenance routine will be run to remove entries from
            this in memory db table older than X hours or minutes or whatever is
            determined to be appropriate.
            >
            Any suggestions?
            >
            >
            Cor Ligthert [MVP] wrote:
            >Jeffrey,
            >>
            >Thank you, I did not copy it this was from a previous answer, mine was
            >>
            >http://msdn2.microsoft.com/en-us/lib...ons.queue.aspx
            >>
            >A little bit shorter than your URL.
            >>
            >By the way, the queue class (FIFO) is extremely sufficient with multi
            >threading.
            >>
            >Cor
            >>
            >""Jeffrey Tan[MSFT]"" <jetan@online.m icrosoft.comsch reef in bericht
            >news:IIsQlK1nG HA.4612@TK2MSFT NGXA01.phx.gbl. ..
            >>Hi Cj,
            >>>
            >>It seems Cor Ligthert [MVP] provided an incorrect URL for queue class, I
            >>think he is referring the URL below:
            >>"Queue Class"
            >>http://msdn.microsoft.com/library/de...us/cpref/html/
            >>frlrfsystemco llectionsqueuec lasstopic.asp
            >>>
            >>There are several container classes in .Net may fit your need, such as
            >>ArrayList, Hashtable, Queue etc..., they all exist under
            >>System.Collec tions
            >>namespace. They all have different usages, you may refer to MSDN for
            >>their
            >>difference and choose the suitable for using as memory container table.
            >>>
            >>Hope this helps!
            >>>
            >>Best regards,
            >>Jeffrey Tan
            >>Microsoft Online Community Support
            >>============= =============== =============== =======
            >>When responding to posts, please "Reply to Group" via your newsreader so
            >>that others may learn and benefit from your issue.
            >>============= =============== =============== =======
            >>This posting is provided "AS IS" with no warranties, and confers no
            >>rights.
            >>>
            >>

            Comment

            • Jeffrey Tan[MSFT]

              #7
              Re: Class used by multiple threads to write to Table

              Hi Cj,

              Thanks for your feedback!

              Based on my reading, your main concern is writing 2 fields to the memory
              table instead of 1 field.

              I think you have 2 choice to resolve this:
              1. If one of your field is unique, you can use it as a key for lookup. Just
              as Cor suggested, you may use SortedList or Hashtable for storage, since
              they both use a key for lookup. So your first field is used as a key, and
              the other field is stored in the SortedList/Hashtable associated with the
              key(first field). This implementation is easy.
              2. If your 2 fields are not unique, you can implement a customized class
              which contains 2 properties to store these 2 fields, and you may store this
              class instance in the System.Collecti ons.* class. Choose what class in this
              namespace you want.

              Thanks.

              Best regards,
              Jeffrey Tan
              Microsoft Online Community Support
              =============== =============== =============== =====
              When responding to posts, please "Reply to Group" via your newsreader so
              that others may learn and benefit from your issue.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no rights.

              Comment

              Working...