0

Is there a way around using the Invoke and InvokeRequired methods for objects which were created in other threads? Is there a method which is more direct and less memory intensive? Perhaps a state machine or thread controls?

Am I wasting my time worrying about this method's resource usage?

I'm using this invoke method: http://tech.xster.net/tips/invoke-ui-changes-across-threads-on-vb-net/

I'm using VB.NET in VS 2012

turbonate
  • 159
  • 2
  • 13
  • Not sure what you are asking. Invoke and InvokeRequired are used to access the UI thread. – dbasnett Apr 14 '13 at 16:35
  • dbasnett, thank you. I do have code which uses the Invoke method to access the UI thread. However, there's more than one way to skin a cat and I'm looking for another option. Hans Passant gave me enough information to do some digging. – turbonate Apr 18 '13 at 04:13

1 Answers1

1

This strongly fits my doctor's usual advice: "if it hurts then don't do it".

There are several .NET patterns that emphasize keeping the threaded code separate from code that needs to run on the UI. BackgroundWorker has been available for a long time, note how its ProgressChanged and RunWorkerCompleted events run on the UI thread. Good place to update UI without having to invoke yourself.

The .NET 4 Task class hands you the TaskScheduler.FromCurrentSynchronizationContext() method. Which is a good way to chain a task that runs on the UI thread, pretty specifically intended to update the UI with the results of previous tasks that run asynchronously. VS2012 provides yet another weapon with the Async and Await keywords. All good ways to avoid writing the code you don't want to write.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536