简体   繁体   中英

Access UI Control on UI Thread using async & a event

I am writing a WPF Form Application, in which i am trying to Loop through a FOLDER and show its files REAL TIME, using Async await approach. Within my Task.Run() method I am Raising my event, which raises fine, however that event code also updates a TEXT BOX , which is on UI Thread , hence i am getting UI Thread Error

'The calling thread cannot access this object because a different thread owns it.'

. Is there any way to change my code so I can update my TextBox?

    private delegate void GetFilesCount(string f);
    private event GetFilesCount onFileCount;


    private void Btn_Compare_Click(object sender, RoutedEventArgs e)
    {
        onFileCount += FileCount;
        CountFilesAsync();
    }


    function async void CountFilesAsync() {
            await Task.Run(()=> {
                System.IO.DirectoryInfo myDir = new System.IO.DirectoryInfo(path);
                foreach (FileInfo item in myDir.GetFiles())
                {
                    onFileCount(item.Name); // This is an EVENT
                } 

            });
}

and my event Handler Code

    private void FileCount(string fileName)
    {
            txtLabel_Log.Text = fileName;   // < -- Calling Method UI Error
    }

Not sure if this will help you, but you could trying invoking your applications dispatcher.

private void FileCount(string fileName)
{
        Application.Current.Dispatcher.Invoke(() => {
            txtLabel_Log.Text = fileName;   // < -- Calling Method UI Error
        });
}

Async in 4.5: Enabling Progress and Cancellation in Async APIs

private void Btn_Compare_Click(object sender, RoutedEventArgs e)
{
    CountFilesAsync(Progress<string>(FileCount));
}

private async void CountFilesAsync(IProgress<string> progress)
{
    await Task.Run(() =>
        {
            System.IO.DirectoryInfo myDir = new System.IO.DirectoryInfo(path);
            foreach (FileInfo item in myDir.GetFiles())
            {
                progress(item.Name); // report progress
            } 
        });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM