Question:
How to code in c#.net?
ZeroCool
2014-02-25 04:17:29 UTC
Hey guys I have function name ' displayData(int key) ' which displays data from the database by selecting the key.now my problem is ..this function is getting called almost 100 times in a second .due to this the function is interrupted in between its execution. now i want that each request for the function execution
should be fulfilled but in serialized manner .
I tried 'lock(this)' but i did not worked..
i am using c#.net with MySql
any kind of help will be appreciated..
Three answers:
David W
2014-02-25 07:41:12 UTC
It's actually recommended that you pass lock an object that has the same scope/lifecycle as your function, not the calling object itself. So, something like lock (new object()) {...code here..}



But anyway, you might want to think about your design...even if you lock it, that means tons of other calls are going to be waiting? This seems like a major design issue. Can you load all of this data into memory in a single call? Either a DataTable, dictionary, or something...? It would be much better to query the database once and then pull it from memory (LINQ is good for this) than to keep hitting the database over and over again.
AJ
2014-02-25 06:46:39 UTC
What you can do is pull everything and place in a dataset. Then the function is simply querying against the in memory copy of the table.



But why would a function need to be called 100 times a second for to begin with?
Stewy
2014-02-25 04:22:19 UTC
The lock should work, depending on how you're using it. I prefer to create an object instance and lock the object instead of using lock(this). Of course, the important thing is to ensure that all threads are accessing the same instance of the object; whether you use "this" or another object instance. Other than that, you can place requests in a queue, but then that puts you in async programming territory.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...