Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

single or multiple threaded AND syncronisation

Posted on 2003-11-13
3
Medium Priority
?
539 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
2 Comments
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 1000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

783 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