Solved

single or multiple threaded AND syncronisation

Posted on 2003-11-13
3
530 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 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

12 Experts available now in Live!

Get 1:1 Help Now