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. multi threading problem???

multi threading problem???

Scheduled Pinned Locked Moved C#
databasehelpquestion
5 Posts 4 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.
  • M Offline
    M Offline
    mr mohsen
    wrote on last edited by
    #1

    hi i have two thread that run two different methods. in both methods i write a code that read a data from database. for reading data from database i use sqlDataReader object. assume two below area code: first method(for first thread): cmd.commandText="sql query"; sqldatareader dr=cmd.executereader(); if(dr.Hasrows) { dr.Read(); // in here i have a problem when another threads try to execute dr.Read() command of another methods // because still this datareader object is not close. and the exception error is : //"invalid try to read data. a datareader must be closed first." } dr.Close(); ......... second method(for second thread): cmd.commandText="second sql query"; sqldatareader dr=cmd.executereader(); if(dr.Hasrows) { dr.Read(); // in here i have a problem when another threads try to execute dr.Read() command of another methods // because still this datareader object is not close. and the exception error is : //"invalid try to read data. a datareader must be closed first." } dr.Close(); ------------- please attention this two methods have completetly different code but in both of them should be read a data from database with sqldatareader. i need a class such as Monitor.Enter(Object) to lock this area codes but the problem is this two area codes are not in one method. please tell me a solution. thanks alot.

    nobody help you... you have to help you yourself and this is success way.

    F 1 Reply Last reply
    0
    • M mr mohsen

      hi i have two thread that run two different methods. in both methods i write a code that read a data from database. for reading data from database i use sqlDataReader object. assume two below area code: first method(for first thread): cmd.commandText="sql query"; sqldatareader dr=cmd.executereader(); if(dr.Hasrows) { dr.Read(); // in here i have a problem when another threads try to execute dr.Read() command of another methods // because still this datareader object is not close. and the exception error is : //"invalid try to read data. a datareader must be closed first." } dr.Close(); ......... second method(for second thread): cmd.commandText="second sql query"; sqldatareader dr=cmd.executereader(); if(dr.Hasrows) { dr.Read(); // in here i have a problem when another threads try to execute dr.Read() command of another methods // because still this datareader object is not close. and the exception error is : //"invalid try to read data. a datareader must be closed first." } dr.Close(); ------------- please attention this two methods have completetly different code but in both of them should be read a data from database with sqldatareader. i need a class such as Monitor.Enter(Object) to lock this area codes but the problem is this two area codes are not in one method. please tell me a solution. thanks alot.

      nobody help you... you have to help you yourself and this is success way.

      F Offline
      F Offline
      Frank Horn
      wrote on last edited by
      #2

      Why don't you let each thread open its own connection?

      M 1 Reply Last reply
      0
      • F Frank Horn

        Why don't you let each thread open its own connection?

        M Offline
        M Offline
        mr mohsen
        wrote on last edited by
        #3

        thank you frank but i think you didnt understand my problem. when i open a connection to a database with sqldatareader, i can not open another connection to that database or anyoneelse untill previouse connection will be close. but in multi threading its maybe happen. what should i to do to avoid to this critical error????

        nobody help you... you have to help you yourself and this is success way.

        M M 2 Replies Last reply
        0
        • M mr mohsen

          thank you frank but i think you didnt understand my problem. when i open a connection to a database with sqldatareader, i can not open another connection to that database or anyoneelse untill previouse connection will be close. but in multi threading its maybe happen. what should i to do to avoid to this critical error????

          nobody help you... you have to help you yourself and this is success way.

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          mr.mohsen wrote:

          i can not open another connection to that database or anyoneelse untill previouse connection will be close.

          In that case you don't need multiple threads. If you use multiple threads you need to synchronise access to the connection so only one thread can use it at a time...may as well just use one thread. Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          1 Reply Last reply
          0
          • M mr mohsen

            thank you frank but i think you didnt understand my problem. when i open a connection to a database with sqldatareader, i can not open another connection to that database or anyoneelse untill previouse connection will be close. but in multi threading its maybe happen. what should i to do to avoid to this critical error????

            nobody help you... you have to help you yourself and this is success way.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            I think Frank has the correct solution. Your code does not show where you create the SQLCommand object and assign it a connection. Each connection can have only 1 datareader open. If you instantiate a command and connection IN each thread it will work.

            Never underestimate the power of human stupidity RAH

            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