2012-03-06 91 views
0

我在WPF中使用EmguCV,我发现这个例子TP捕获图像,我想在我的一些其他方法Method3()中使用bs1,但我得到错误,该对象属于一些其他线程,任何人都有任何想法是什么问题? BS1毕竟是一个全局变量后WPF线程错误

BitmapSource bs1; 

private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     capture = new Capture();  ///capture image 

     timer = new DispatcherTimer();  // timer object 
     timer.Interval = new TimeSpan(500); 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.Start(); 

    } 
    void timer_Tick(object sender, EventArgs e) 
    { 
     using ( Image<Bgr, byte> Frame = capture.QueryFrame()) 
     { 
      if (Frame != null) 
      { 
       bs1 = ToBitmapSource(Frame); 
webcam.Source = ToBitmapSource(Frame); // ToBitmapSource convert image to bitmapsource webcam is a picture in mainwindow 
       Frame.Save("fg.jpeg"); //this work but use lot of processing 

      } 
     } 
    } 


public void Method3_click (...) 
{ 
    use_of_bs1(bs1); 
} 


    private void use_of_bs1() 
    { 

     data.Text = "waiting..."; 

     System.Threading.ThreadPool.QueueUserWorkItem(Startwork); 
    } 


    private void Startwork(object state) 
    { 

     try 
     { 
      _work = _worker.bs1_analysis(bs1);  // it is where bs1 giving thread errorbs1_analysis is library function 
     } 
     catch (Exception ex) 
     { 
      Dispatcher.BeginInvoke(new ShowworkInformationDelegate(ShowworkInformation)); 
      MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); 
      return; 
     } 
     Dispatcher.BeginInvoke(new ShowWorkInformationDelegate(ShowWorkInformation)); 

    } 

/// ToBitmapsource功能

public static BitmapSource ToBitmapSource(Emgu.CV.IImage image) 
    { 
     using (System.Drawing.Bitmap source = image.Bitmap) 
     { 
      IntPtr ptr = source.GetHbitmap(); 
      BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ptr, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); 
      DeleteObject(ptr); 
      return bs; 
     } 
    } 
+0

看看http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired/ – Mohit 2012-03-06 12:06:29

+1

考虑到你使用的是DispatcherTimer,我猜你的Tick()是在调度程序线程正确地被解雇,所以你不需要特别调度。有可能指定什么函数引发这个错误?您发布的代码或您拥有的** NOT **代码(例如'Capture'类或'ToBitmapSource()'funcion)? – 2012-03-06 12:34:00

+0

实际上方法3是引发错误的方法 method3是一个按钮,它具有也使用bs1的线程的功能 – murmansk 2012-03-07 05:40:26

回答

1

如果您创建了一个wpf资源并希望在不同的线程上使用它,您可以在传递它之前在对象上调用Freeze()。这将使它在另一个线程上使用是不可变的和合法的。

+0

谢谢它做了诀窍 – murmansk 2012-03-07 07:21:43

1

从你所描述的东西,BS1的是与Window.Dispatcher相关的,所以当你访问它里面Method3() ,引发了一个例外。要解决这个问题,你可以做这样的事情

public void Method3() 
{ 
    Action<BitmapSource> useBs1 = (source) => use_of_bs1(source); 
    if(Thread.CurrentThread == this.Dispatcher.Thread) 
     { 

    useBs1(bs1); 
} 
else 
{ 
    this.Dispatcher.Invoke(DispatcherPriority.Normal,userBs1, bs1); 
} 


} 
1

在计时器事件(timer_Tick)你在不同的线程的一个地方BS1属于

您需要在执行事件主线程。例如:

void timer_Tick(object sender, EventArgs e) 
{ 
    Dispatcher.Invoke(DispatcherPriority.Normal, 
      new Action(
       delegate { 
    using ( Image<Bgr, byte> Frame = capture.QueryFrame()) 
    { 
     if (Frame != null) 
     { 
      bs1 = ToBitmapSource(Frame); 
      webcam.Source = ToBitmapSource(Frame); // ToBitmapSource convert image to bitmapsource 
      Frame.Save("fg.jpeg"); //this work but use lot of processing 

     } 
    }})); 
} 
+0

实际上它是方法3引发错误 – murmansk 2012-03-07 05:42:47

2

在WPF中,UI元素只能由创建它们的相同线程(除了可冻结元素)访问和使用。在你的代码中,bs1是在主UI线程中创建的。计时器是不同的线程不能访问该资源。 每当你想要做的就是主UI线程上创建的UI元素的东西,做到以下几点:

Dispatcher.Invoke(DispatcherPriority.Normal, new Action(()=>DoSomeCodeWithUIElement())); 

使用Dispatcher.Invoke,如果你想如果你想异步调用的操作同步或Dispatcher.BeginInvoke运行。 其中DoSomeCodeWithUIElement是一种访问并最终更新UI元素的方法。