Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 1.30 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. The calling thread cannot access this object because a different thread owns it
  2. drawThread = new Thread(new ThreadStart(drawPosters));
  3.                 drawThread.SetApartmentState(ApartmentState.STA);
  4.                 drawThread.Start();
  5.        
  6. while (true)
  7. {
  8.   waitEvent.WaitOne();
  9.   //do some calculations
  10.   // change uniform grid rows & cols
  11.   posterUniformGrid.Rows = calculatedRows; //**-> error is first thrown here**
  12.   posterUniformGird.Columns = calculatedCols;            
  13. }
  14.        
  15. Dispatcher.Invoke(DispatcherPriority.Normal,
  16.                     new Action<object[]>(SetGrid),
  17.                     new object[] { calculatedRows, calculatedColumns });
  18.        
  19. while (true)
  20.  {
  21.    waitEvent.WaitOne();
  22.    this.InvokeEx(t => t.posterUniformGrid.Rows = calculatedRows);
  23.    this.InvokeEx(t => t.posterUniformGird.Columns = calculatedCols);            
  24.  }
  25.  
  26.  public static class ISynchronizeInvokeExtensions
  27.     {
  28.         public static void InvokeEx<T>(this T @this, Action<T> action) where T : ISynchronizeInvoke
  29.         {
  30.             if (@this.InvokeRequired)
  31.             {
  32.                 try
  33.                 {
  34.                     @this.Invoke(action, new object[] { @this });
  35.                 }
  36.                 catch { }
  37.             }
  38.             else
  39.             {
  40.                 action(@this);
  41.             }
  42.         }
  43.     }