Solved

single or multiple threaded AND syncronisation

Posted on 2003-11-13
3
531 Views
Last Modified: 2012-06-27
Ok, so I was under the impression that vb is single threaded correct?

well the scenario is thus...

We have a VB application, say "Reciever",
We have many instances of another VB application, say "Client"
We also have a VB COM+ "Event Server" application.

So the client calls a method on the Event Server which fires an event on the Reciever.
This event is handled and carries out some manipulation on the apps members.
In the mean time the app is carrying out its main line of execution.

While stepping through the main code, if an event is fired then the current line will jump to the event code.

So this would say to me that Reciever is multi threaded...

Anyway, when there are multiple Clients running, we get an automation error (-2147164155 OR 8004e005).
So we tried utilising Critical Sections to lock access to the array while in use.
When the main code is executing, we get a lock on the Critical Section and release it at the end of this iteration of the code.

When an event comes in, it attemps to get a lock and in theory has to wait until the lock is released by the main code.
In practice though the lock is granted which means it is implied that the event is in the same thread.

Chicken and egg has nothing on this!

how can we ensure cross-'Thread' sync'ing?
is vb truly single threaded?

m3
0
Comment
Question by:monkey3
3 Comments
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 250 total points
ID: 9741431
Yes, VB is in general single threaded. The behaviour you describe can be caused by something called re-entry. What can happen is whenever you make an out-of-apartment call (typically an out-of-process call, like to SQL server or to a COM+ component) any outstanding events that are waiting for that thread can be processed, which gives the impressing that two functions are executing simulatenously. But they are not, its just that the first one was interrupted part way through, and the second one took over. This is why the critical section approach doesn't work, because it is the same thread, as you said.

This can be very problematic to deal with. One thing to do is try to avoid making any out-of-apartment calls when your variables are vulnerably to re-entry. If that can't work, then you may need to implement your own protection scheme, have a boolean that says if the variable is locked, then in your event handler, put in a loop until the boolean is cleared with a DoEvents.

Zaphod.
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9747874
monkey3,
    VB is normaly single threaded, but there are workarounds to do multithreading if you really really need to. Z_Beeblebrox's description does answer what you specificaly described in your question though.

http://www.freevbcode.com/ShowCode.Asp?ID=1287

Dang123


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now