views:

76

answers:

2

My application is using an image processing library to handle a long running task. The main UI with settings and controls is implemented in WPF. The image processing needs to be displayed and the main UI needs to remain responsive. Upon clicking the 'process' button in the main UI a new thread is spawned which creates a new WinForm window to display the processed images in.

Before it was multithreaded the UI would hang while processing and the progress would be visible in the WinForm for displaying the images. Then when the processing would complete the WinForm would remain with the image in it. Events are added to the new WinForm that allow panning and zooming. The panning and zooming functionality worked correctly.

It became obvious due to the requirements of the project that it would need to be multithreaded to function properly.

Now with the new thread the WinForm window is created as before and the image is processed and displayed. The problem is that when this method is completed the thread exits. Having the thread exit means that if the allocated image buffers are not freed then the application throws an exception. To fix this there is a method called to free all allocations before the thread exits. This fixes the exception and makes the entire thread execute successfully but it means that the image display buffer and form to display it in are freed/disposed of and so there is not time available for the zooming and panning events.

The best solution to make the Thread not exit was to make an AutoResetEvent and have something like this at the end of the image processing thread.

while (!resetEvent.WaitOne(0, false)) { }
threadKill(); // frees all allocations   

The AutoResetEvent is fired by the by a button on the main UI that kills the thread. This works to have the image display as long as needed and killed explicitly by the user, however it fails to allow the firing of Click and Drag events needed to make the image pan and zoom. Is there a way to make the thread not exit without having a spinning while loop which prevents the events from being fired? The desired functionality is to have the thread remain alive so that the allocations do not have to be freed and the panning and zooming can be implemented.

Even though the solution may be obvious to someone with more experience threading, any help would be appreciated as I am new to multithreaded applications.

Thanks

EDIT: It should be known that the end goal is to display a constant stream of frames which are processed in this way taken from a frame grabber. So I don't think that it will work to process them separately in the background and then display them in the main UI, because there is going to need to be a constant stream of displays and this would lock up the main UI.

EDIT: The real intent of the question is not to find a better way to do something similar. Instead I am asking if the new thread can be stopped from exiting so that the click events can fire. If this behavior cannot be achieved with System.Threading.Thread then saying it cannot be achieved would also be an accepted answer.

A: 

Use the background worker to process the image for pan and zooming, pass the data to the backgroundworker.RunCompleted Event. You can then display the new image in the main UI thread with no slow down or locking.

JMcCarty
I considered something like this. However the application needs (once a frame grabber is delivered) to constantly capture images, process them, and display them like video.
Fultonae
A: 

If you can use the new parallel classes and collections in C# 4.0 this is a pretty easy task. Using a BlockingCollection<T> you can add images from any thread to the collection and have a background consumer taking images off this collection and process them. This background processing can be easily created and managed (or canceled) using a Task from the TaskFactory. Check out this simple WPF application for loading images and converting them to black and white as long as there are images to process without blocking the UI. It doesn't use two windows but I think it demonstrates the concepts:

using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Microsoft.Win32;

namespace BackgroundProcessing
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private readonly BlockingCollection<BitmapImage> _blockingCollection = new BlockingCollection<BitmapImage>();
    private readonly CancellationTokenSource _tokenSource = new CancellationTokenSource();
    private ImageSource _processedImage;

    public MainWindow()
    {
        InitializeComponent();
        CancellationToken cancelToken = _tokenSource.Token;
        Task.Factory.StartNew(() => ProcessBitmaps(cancelToken), cancelToken);
        PendingImages = new ObservableCollection<BitmapImage>();
        DataContext = this;
    }

    public ObservableCollection<BitmapImage> PendingImages { get; private set; }

    public ImageSource ProcessedImage
    {
        get { return _processedImage; }
        set
        {
            _processedImage = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("ProcessedImage"));
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void ProcessBitmaps(CancellationToken token)
    {
        while (!token.IsCancellationRequested)
        {
            BitmapImage image;
            try
            {
                image = _blockingCollection.Take(token);
            }
            catch (OperationCanceledException)
            {
                return;
            }
            FormatConvertedBitmap grayBitmapSource = ConvertToGrayscale(image);
            Dispatcher.BeginInvoke((Action) (() =>
                                                 {
                                                     ProcessedImage = grayBitmapSource;
                                                     PendingImages.Remove(image);
                                                 }));
            Thread.Sleep(1000);
        }
    }

    private static FormatConvertedBitmap ConvertToGrayscale(BitmapImage image)
    {
        var grayBitmapSource = new FormatConvertedBitmap();
        grayBitmapSource.BeginInit();
        grayBitmapSource.Source = image;
        grayBitmapSource.DestinationFormat = PixelFormats.Gray32Float;
        grayBitmapSource.EndInit();
        grayBitmapSource.Freeze();
        return grayBitmapSource;
    }

    protected override void OnClosed(EventArgs e)
    {
        _tokenSource.Cancel();
        base.OnClosed(e);
    }

    private void BrowseForFile(object sender, RoutedEventArgs e)
    {
        var dialog = new OpenFileDialog
                         {
                             InitialDirectory = "c:\\",
                             Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp",
                             Multiselect = true
                         };
        if (!dialog.ShowDialog().GetValueOrDefault(false)) return;
        foreach (string name in dialog.FileNames)
        {
            CreateBitmapAndAddToProcessingCollection(name);
        }
    }

    private void CreateBitmapAndAddToProcessingCollection(string name)
    {
        Dispatcher.BeginInvoke((Action)(() =>
                                            {
                                                var uri = new Uri(name);
                                                var image = new BitmapImage(uri);
                                                image.Freeze();
                                                PendingImages.Add(image);
                                                _blockingCollection.Add(image);
                                            }), DispatcherPriority.Background);
    }

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }
}
}

This would be the XAML:

<Window x:Class="BackgroundProcessing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="40"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="3*"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Row="0" Grid.ColumnSpan="3" Background="#333">
        <Button Content="Add Images" Width="100" Margin="5" HorizontalAlignment="Left" Click="BrowseForFile"/>
    </Border>
    <ScrollViewer VerticalScrollBarVisibility="Visible"  Grid.Column="0" Grid.Row="1">
        <ItemsControl ItemsSource="{Binding PendingImages}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Image Source="{Binding}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </ScrollViewer>
    <Border Grid.Column="1" Grid.Row="1" Background="#DDD">
        <Image Source="{Binding ProcessedImage}"/>
    </Border>       
</Grid>

C8H10N4O2
Thank you for the extensive answer. There are two reasons why this most likely cannot work however. The first is that I must use C# 3.5, the second is that it appears that this code handles the actual display of the images itself. In order to get the needed FPS the imaging library must display the image buffers. This is why the WinForm was introduced, because the imaging library needs to be passed a window handle as a parameter. Thanks though
Fultonae