InvokeRequired equivalent in WPF

I am a "WPF beginner" .
I am converting an app UI from WinForms to WPF and I have to adapt this method:

private void InvokeAppendText(TextBox txb, string text)
        {
            if (txb.InvokeRequired)
                txb.Invoke((MethodInvoker)delegate { txb.AppendText(text); });
            
            else
            {
                txb.AppendText(text);
                txb.ScrollToCaret();  
            }
        }

Open in new window


In WPF :
-  InvokeRequired does not exist and I guess I have to use Dispatcher.CheckAccess() ;
- MethodInvoker does not exist and I guess I have to use Dispatcher.CheckAccess() ;
- ScrollToCaret does not exist and i gues I have to use something similar to:

        var selectionStart = x;
        var selectionLength = y;
        txb.Select(selectionStart, selectionLength);
        txb.ScrollToLine(textBox.GetLineIndexFromCharacterIndex(textBox.SelectionStart));

Open in new window


I've tried different syntaxes and now i am lost !!
Could an Expert help me
PaulagierResearch DirectorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
You could try something like:
static class Extensions
{
	public static void HandleInvokeRequired<T>(this T control, Action<T> action) where T : Control
	{
		//Check to see is the control is not null
		if (control == null)
			throw new ArgumentNullException(string.Format("Cannot execute {0} on {1}.  {1} is null.", action, control));

		//Check to see if the control is disposed.
		if (PresentationSource.FromVisual(control).IsDisposed)
			throw new ObjectDisposedException(string.Format("Cannot execute {0} on {1}.  {1} is disposed.", action, control));

		//Check to see if the control's InvokeRequired property is true
		if (control.Dispatcher.CheckAccess())
		{
			try
			{
				//Perform the action
				action(control);
			}
			catch (Exception ex)
			{
				throw new Exception(string.Format("Cannot execute {0} on {1}.  {2}.", action, control, ex.Message));
			}
		}
		else
		{
			try
			{
				//Use Invoke() to invoke your action
				control.Dispatcher.Invoke(action, new object[] { control });
			}
			catch (Exception ex)
			{
				throw new Exception(string.Format("Cannot execute {0} on {1}.  {2}.", action, control, ex.Message));
			}
		}
	}
}

Open in new window

Example usage -
namespace EE_Q28857879
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		private void OnClick(object sender, RoutedEventArgs e)
		{
			tbHello.HandleInvokeRequired(tb => tb.Text = "Hi Button Click");
		}
	}
}

Open in new window

-saige-

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PaulagierResearch DirectorAuthor Commented:
Hello,
Sorry, does not compile
it_saigeDeveloperCommented:
What is the error?

-saige-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.