0

Hi I am looking for some very basic information about c# coding in general and especially WPF.

First of all I do not really understand, when I have to update a form component in WPF with Invoke and when not.

For example if you have a simple button click event you usally do something like this in it:

this.label1.content = this.dataObject.Position;

But if you for example poll a lot of data you usually invoke label 1.

I do not understand when I have to invoke and when not. Does it depend on my dataObject instance? For example when I have my WPF form and another class in my Project which is called dataObject.cs and I initialize this class with

private dataObject dataO = new dataObject();

in my WPF form, do i have to invoke it, when I use it, because it is another class? Or rather do I not have to invoke it, because it is initialized in my gui and thus the GUI thread is handling the class anyway? So in which cases I have to invoke a dataobject?

Secondly I would like to get information about how to improve performance and data allocation in c#. For example, at which place in my gui do I declare new threads, variables etc. for best performance? Where do I initialize them? When is it recommended to create another class which handles all my parameters and how do I call this class (and where) in my WPF code? Maybe you know some good tutorials or books in general which focus on this topic.

Thanks a lot!

gagan mahatma
  • 336
  • 2
  • 9
DerBenutzer
  • 311
  • 1
  • 5
  • 20

1 Answers1

2

I do not understand when I have to invoke and when not.

In UI applications, there is 1 thread that processes all UI rendering. This is typically the main thread that your application starts (see Message Pump for more info). As a rule of thumb, you don't want to do any heavy-lifting work in this thread, because when you do so, no other messages can be processed. Which means your UI becomes laggish and irresponsive.

If it's a quick update, you can do it in the main UI thread. If operation requires long time - do it in another thread with Invoke.

The other use of Invoke is when you need to update UI state from the non-UI thread.

Secondly I would like to get information about how to improve performance and data allocation in c#.

Don't optimise until really needed, the stuff you mention is probably not important in terms of performance. At this stage I'd concentrate on designing testable code and writing tests, rather than on performance. When you have a clear performance problem you can use tools to help diagnosing bottlenecks, e.g. profilers from RedGate or JetBrains.

Community
  • 1
  • 1
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • I'd add a detail: as I explain in the above mentioned answer I posted to a different question, "ui updates" performed using the built in data binding in WPF don't require any particular attention (no need to use invoke) since "property change" related stuff is automatically marshalled at UI level by the WPF framework itself – Leonardo Spina Jan 07 '16 at 10:56