Threading Question

fredriko
08-22-2007, 02:04 PM
I have an Object Customer (working on thread #1) that triggers method Customer.GetCustomers (working on thread #2). My question is this, how do I trigger a different method e.g Customer.Update on Thread#1 when I am currently in Thread#2.
This would be easy if it were a windows form application as I could trigger form.invoke but it isn't. Any ideas how I can get around this?

shaul_ahuva
08-22-2007, 02:38 PM
If there is definitely a need to marshal the call from thread 2 to thread 1, you'll need to come up with your own marshalling code. I have an idea about how to do this, but it would probably be sufficient to use built-in thread synchronization (such as SyncLock) to make your Customer.Update method thread-safe:

Public Class Customer
Private UpdateLockObject As New Object

Public Sub Update()
SyncLock(UpdateLockObject)
'This code will only ever be executed by one thread
'at a single point in time.
End SyncLock
End Sub
End Class


That being said, what exactly are you trying to accomplish?

AtmaWeapon
08-22-2007, 03:13 PM
I think I know a spiffy way to make this work but I need to know some things.

Are you using .NET 2.0 or later?

Also, let's make sure I understand the situation. You have some class Customer that has two methods we are concerned with: GetCustomers() and Update(). You have two instances: Customer1 on one thread and Customer2 on another thread. You would like Customer2 to call Customer1.Update() and have the call executed on the same thread as Customer1. Is this correct?

If so, I need a little more information about your design. What are these calls doing? Why are they on different threads? How is each object spawned? How does Customer1 execute Customer2.GetCustomers() on thread 2?

In short, just like shaul_ahuva I don't think I can really offer any useful advice without some more insight into your problem.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum