Solved

single or multiple threaded AND syncronisation

Posted on 2003-11-13
3
536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Suggested Solutions

Title # Comments Views Activity
using Access 8 95
VB6 - Compare and highlight cell not the same 3 63
Looking for VB6 code to read SQL table export it to ascii 8 61
Added a column screws up code 5 78
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

752 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