stustarz Posted October 30, 2004 Posted October 30, 2004 Hi all Query for you, using VB.net by the way: I have a form with 2 listboxes, i have a function that returns a datareader object. I want to make it multithreaded, so that when a user clicks a button, both listboxes fill with data from my connected database. When i do this i am getting an exception, because the two threads are both trying to run the same function and use the same object etc (the function is actually held in a class on its own) i am wondering if there is any way of allowing a function to return an object with the same name, twice? I hope i have worded this correctly. Thanks Stu Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
Administrators PlausiblyDamp Posted October 31, 2004 Administrators Posted October 31, 2004 You could possibly return an array of two objects from the function, however could you not just populate both controls from the same DataReader? If you are already using the reader in a loop to populate one control just append the values to the second control at the same time. If you are data binding to the reader then things get more complicated as you cannot have two readers open on the same DB connection - you would need to bind one control and then re-create the reader to bind the second which would defeat the point of multithreading anyway. Also bear in mind that you shouldn't update the UI from anything other than thr main application thread anyway. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
stustarz Posted October 31, 2004 Author Posted October 31, 2004 The two controls are going to contain values from two different tables, thats why i would prefer two readers, but if only one reader can exist on a connection at a time, then ill have to just abandon that idea. Ive currently got it that once one listbox has completed populating, it fires the function to populate the next one. The actual database connection and Stored Proc execution is done on one thread, and once the datareader has been executed it then raises an event to the main thread to populate the listview. I suppose i could do it using datasets, but i am have no need to store the data for further processing, and i think this would again defy the point in multithreading my app cause i would lose all the performance of using the DReader. :) Im just thinking out loud now! :D thanks for your advice Quote Visit: VBSourceSeek - The VB.NET sourcecode library "A mere friend will agree with you, but a real friend will argue."
*Experts* Nerseus Posted October 31, 2004 *Experts* Posted October 31, 2004 As for your first question (sorta) - I believe you can only have one DataReader on a connection at a time, if the DataReader is a forward only "firehose" type reader (old terminology). Generally, this type of reader is using the connection the whole time (as you read a record, it's retrieved from the server). These used to be the server-side cursors, because the server (such as SQL Server) would hold all the data in tempdb until the reader asked for it. I'm not sure if that helps or not. Hopefully I'm not making it worse. Now, if you're using webservices... all webservices provide a default asynchronous call. You might look into that if you want to load two chunks of data at the same time. It doesn't require any special threading logic on your part - just two calls instead of one. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.