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

  • SEARCH
  • Home
  • 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 9239693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:02:55+00:00 2026-06-18T08:02:55+00:00

I am trying to get my head around the changes in .NET 4.5, mainly

  • 0

I am trying to get my head around the changes in .NET 4.5, mainly the async features. To get my head around it I thought i would create a little app for archiving my massive photo collection. I learn best by doing so the application serves a double purpose.

I have read plenty MSDN articles on using async but I don’t think I have a good enough understanding of it (because it’s not working). My intention was to have each photo at a source folder copied to a destination folder based on its date taken (or created if taken meta data is missing). At the same time renaming it to a standard naming convention and showing the image as it is archived in an image box. I wanted the application to keep responding during the work, which is where async comes in. Now the app purpose is unimportant, the entire point was getting my head around async.

What actually happens is the app goes unresponsive, archives all the images as intended but the image box only shows the final picture. Async is kicking off the file transfer then moving on to the next image, kicking off the transfer then moving on etc etc so i end up with hundreds of open file streams rather than it waiting for each to close.

Any pointers in where I am going wrong would be appreciated. My understanding of using Tasks is shakey, returning a task serves what purpose?

imgMain is the imagebox in the XAML file. The async/await is in the archive method but showing all code as it may be relevant.

using System;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Forms;
using System.IO;

namespace PhotoArchive
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    private string Source 
    {
        get { return txtSource.Text; }
        set { txtSource.Text = value; }
    }

    private string Destination
    {
        get { return txtDestination.Text; }
        set { txtDestination.Text = value; }
    }


    public MainWindow()
    {
        InitializeComponent();

    }

    private void btnBrowseDataSource_Click(object sender, RoutedEventArgs e)
    {
        var dialogue = new FolderBrowserDialog();
        dialogue.ShowDialog();
        Source = dialogue.SelectedPath;

    }

    private void btnBrowseDestination_Click(object sender, RoutedEventArgs e)
    {
        var dialogue = new FolderBrowserDialog();
        dialogue.ShowDialog();
        Destination= dialogue.SelectedPath;
    }

    private void btnSort_Click(object sender, RoutedEventArgs e)
    {
        var files = Directory.GetFiles(Source, "*.*", SearchOption.AllDirectories);
        var result = from i in files
                     where i.ToLower().Contains(".jpg") || i.ToLower().Contains(".jpeg") || i.ToLower().Contains(".png")
                     select i;


        foreach (string f in result)
        {
            DateTime dest = GetDateTakenFromImage(f);
            Archive(f, Destination, dest);
        }

    }

    private async void Archive(string file, string destination, DateTime taken)
    {

        //Find Destination Path
        var sb = new StringBuilder();
        sb.Append(destination);
        sb.Append("\\");
        sb.Append(taken.ToString("yyyy"));
        sb.Append("\\");
        sb.Append(taken.ToString("MM"));
        sb.Append("\\");

        if (! Directory.Exists(sb.ToString()))
        {
            Directory.CreateDirectory(sb.ToString());
        }

        sb.Append(taken.ToString("dd_MM_yyyy_H_mm_ss_"));
        sb.Append((Directory.GetFiles(destination, "*.*", SearchOption.AllDirectories).Count()));
        string[] extension = file.Split('.');
        sb.Append("." + extension[extension.Length-1]);


        using (FileStream fs = File.Open(file, FileMode.Open))
        using (FileStream ds = File.Create(sb.ToString())) 
        {
            await fs.CopyToAsync(ds);
            fs.Close();
            File.Delete(file);
        }

        ImgMain.Source = new BitmapImage(new Uri(sb.ToString()));
    }

    //get date info
    private static Regex r = new Regex(":");

    public static DateTime GetDateTakenFromImage(string path)
    {
        using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            using (System.Drawing.Image img = System.Drawing.Image.FromStream(fs, false, false))
            {
                PropertyItem prop;

                try
                {

                    prop = img.GetPropertyItem(36867);

                }
                catch (Exception)
                {
                    prop = img.GetPropertyItem(306);
                }

                string dateTaken = r.Replace(Encoding.UTF8.GetString(prop.Value), "-", 2);
                return DateTime.Parse(dateTaken);
            }
        }


    }
}

}

  • 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-06-18T08:02:56+00:00Added an answer on June 18, 2026 at 8:02 am

    My understanding of using Tasks is shakey, returning a task serves what purpose?

    The Task is a representation of the async operation. When the Task completes, it means the operation completed. And you can await the Task, which means you will asynchronously wait for it to complete (not blocking the UI thread).

    But if you make your method async void, there is no way to wait for the operation to complete. When the method returns, you know that the async operation was started, but that’s it.

    What you need to do is to change Archive() to return a Task, so that you can wait for it to complete in your event handler. The Task will be returned automatically, you don’t need to (or can) add any returns.

    So, change the signature of Archive() to:

    private async Task Archive(string file, string destination, DateTime taken)
    

    And then await it in your event handler (which you also need to change to async):

    private async void btnSort_Click(object sender, RoutedEventArgs e)
    {
        // snip
    
        foreach (string f in result)
        {
            DateTime dest = GetDateTakenFromImage(f);
            await Archive(f, Destination, dest);
        }
    }
    

    In general, async void methods should be used only for event handlers. All other async methods should be async Task (or async Task<SomeType> if they return some value), so that you can await them.

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

Sidebar

Related Questions

essentially what I'm trying to do is get my head around Microsoft's ASP.NET MVC
I have been trying to get my head around C#'s new async/await and Task.Run
trying to get my head around Feedzirra here. I have it all setup and
Trying to get my head around how to integrate fancyBox with an aspx c#
Just trying to get my head around what can happen when things go wrong
I'm trying to get my head around using CoffeeScript comprehensions as efficiently as possible.
I am trying to get my head around this query - seems pretty straight
I'm trying to get my head around the covariance of Scala's collections. I have
I'm trying to get my head around AMQP . It looks great for inter-machine
I'm trying to get my head around Cappuccino. I'd like my StackOverview peers to

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.