Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a DispatcherHelper as follows:
C#
//*************************************************************************************/
// Dispatcher Heler
//*************************************************************************************/
public static class DispatcherHelper
{
	public static void Execute(Action action)
	{
		if (Application.Current is null||Application.Current.Dispatcher is null)
			return;

		// Marshall to Main Thread
		Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,action);
	}
}


I have an observable collection and want to show changes on the UI each time a change occurs. The revant code I have is as follows:
C#
// Save New File Name to Observable and Display it.
rOB.NewName=newFileName;
rOB.SecondPass=false;
rOB.RenameStatus="No WEB Details yet";
rOB.DontRename=false;
//
//Utils.DispatcherHelper.Execute(() => ;
return newFileName;


How do I specify the line commencing with Utils.DispatcherHelper correctly?

What I have tried:

I have another area of code that just displays my original files and it works fine.
C#
// Display and Load Rename Observable Collection with Original File Name
//Utils.DispatcherHelper.Execute(() => Gbl.rO.Add(new RenameObservable(fileName,"","Orig Loaded",true,true,filePath,file)));


I have tried many ways but cannot get it to work.
Posted
Updated 22-Sep-24 5:26am
v2
Comments
Richard Deeming 23-Sep-24 3:46am    
NB: Failing silently if there is no Application.Current.Dispatcher seems like a bad idea. In that case, you should either execute the action directly, or throw an exception.

Just returning from the method without executing the action or giving any indication that something went wrong will likely lead to hard to track heisenbugs[^] later on.

1 solution

If all you need to do is handle changing the collection from any thread, you can use BindingOperations.EnableCollectionSynchronization Method (System.Windows.Data) | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900