Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:29:23+00:00 2026-05-16T02:29:23+00:00

My application is using an image processing library to handle a long running task.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:29:24+00:00Added an answer on May 16, 2026 at 2:29 am

    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>
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small image processing application which does multiple things at once using
I want to write a web version of an image processing application writen using
I have an application that does some image processing using rmagick and imagemagick. This
Hi i am developing real time image processing application on android. I am using
I am creating a simple image processing application using OpenCV / Qt and I
I am developing an image processing application by using android ndk. I am getting
I wrote a small image processing algorithm using the CImg library and bundled it
I'm developing an application for video processing using GStreamer and OpenCV. The task is
We have an image processing windows application where we are using lead tools for
I am trying to develop an image gallery application using Qt Framework. The application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.