| 1. using System; 2. using System.Collections.Generic; 3. using System.Linq; 4. using System.Runtime.InteropServices; 5. using System.Text; 6. using System.Diagnostics; 7. using System.Security.Cryptography; 8. using System.Security.Principal; 9. using Microsoft.Win32.SafeHandles; 10. using System.ComponentModel; 11. using System.Reflection; 12. using System.Security; 13. using System.IO; 14. using System.Threading; 15. using System.Security.Permissions; 16. 17. using ApartmentComponentLib; 18. 19. namespace CSharpQuestions 20. { 21. public class Watcher 22. { 23. private object m_IStaObject = null; 24. 25. public static void Main() 26. { 27. Console.WriteLine(Thread.CurrentThread.GetApartmentState()); 28. Watcher watcher = new Watcher(); 29. watcher.Initialize(); 30. watcher.CreateThreads().Join(); 31. 32. Console.WriteLine("Press any key"); 33. Console.ReadLine(); 34. } 35. 36. private Thread CreateThreads() 37. { 38. Thread thread = new Thread(ThreadFunc); 39. thread.Start(); 40. 41. return thread; 42. } 43. 44. private void ThreadFunc() 45. { 46. Console.WriteLine(Thread.CurrentThread.GetApartmentState()); 47. IStaObject2 obj = (IStaObject2)m_IStaObject; 48. obj.TestMethod(); 49. } 50. 51. private void Initialize() 52. { 53. m_IStaObject = new StaObject2Class(); 54. } 55. } 56. } |