|
-
Nov 26th, 2013, 08:34 PM
#1
Re: [RESOLVED] How to use SynchronizingObject property
 Originally Posted by VBobCat
Is there a way to assure that a Timer and an OleDbConnection share the same thread? Might this SynchronizingObject property be used for that?
Neither Timers nor OleDbConnections have threads so how can they share one? The only time that it really makes sense to talk about a relationship between an object and a thread is for controls. That's because the handle of the control can only be accessed on the thread on which it was created. For any other type of object, there is no specific relationship between it and a thread. In the case of a a Timers.Timer, it raises its Elapsed event on a ThreadPool thread but the Timer itself doesn't exist on any specific thread. If you set the SynchronizingObject property then you will be assigning a form or other control to it. The handle of that control is used to ensure that the Elapsed event is raised on the thread that created that handle, i.e. the UI thread.
In the case of an OleDbConnection, there's no thread affinity at all. You can call Open on a specific thread but that makes no difference. The connection is either open or not, regardless of which thread it was opened on. You can then execute commands over that connection on whatever thread you like.
It would be possible to use the SynchronizationContext class to determine what thread an Elapsed event was raised on and execute other methods on that thread later. The thing is, the Elapsed event can be raised on any ThreadPool thread each time, not necessarily the same one. Also, there's no guarantee that a ThreadPool thread will exist at a later stage, so trying to execute a method on the same thread that an Elapsed event was previously raised on may not be safe.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|