synclock causing bad performance?

I am using threads to process some information and then construct a SQL insert command to be included in a batch insert to the DB.  I setup a global variable to store the batch commands and also a counter to keep track of the number of pending inserts.  I tried to use synclock to lock the two global variables while they are updated but this code section seems to be causing my app to work up to 100% cpu usage.  I'm really at a loss as to how to troubleshoot this issue.  Does anyone have any suggestions?

       'construct the SQL insert statement = strSQL

        SyncLock BatchSQL
            BatchSQLCount += 1
        End SyncLock

       'End of thread
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
You need to make sure that you are avoiding deadlocks:


Lab 6: Monitors and Thread Synchronization 2

Bob LearnedCommented:
Do you feel that threading will provide enough benefit to outweigh the complexity that it brings to your solution?  Are the SQL operations optimized?  Are there other bottlenecks that might prove to be more beneficial?

stealth188Author Commented:
Yeah multithreading is required to get the performance I need.  I'm sure that it is related to the synclock because when I remove it the processor goes back to being nice, although the app doesn't work correctly because of multiple threads accessing global variables at the same time.  The queries are optimized as best as can be for adhoc queries, all that i want to do it make sure that the global variable batchSQL is protected from corruption.  I was trying to use stringbuilder to concantonate the string because MSDN says it is faster than just doing a simple &.
stealth188Author Commented:
You got it.  I had found this last night when puzzling over it some more.  Had a situation where threads were calling threading and locking during those activities.  Thanks for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.