Ok, I feel like I know the anwser but I can't think of it right now lol.
So say I have the following code:
Now, this is happening in a Windows service and the Windows service created an instance of the MyClass object and internally the MyClass object starts the timer. The problem I am having is that I get an error "The calling thread cannot access this object because a different thread owns it." This leads to, how do I throw the raised event back to thread it was created on? I am looking into the SynchronizationContext object but the documentation said that only UI elements create it otherwise there is nothing in it.Code:public class MyClass { ... private List<MyClass> _MyManagedList = new List<MyManagedList> (); private System.Timers.Timer _ManagedListTimer = new System.Timers.Timer(5000); .... _ManagedListTimer.Elapsed += MyEventHandler_Elapsed; _ManagedListTimer.Start(); .... private void MyEventHandler_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { _MyManagedList.ForEach(item => Console.WriteLine(item.ToString())); } ... }




Reply With Quote