multi threading problem???
-
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.
-
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.
Why don't you let each thread open its own connection?
-
Why don't you let each thread open its own connection?
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.
-
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.
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:
-
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.
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