How to run code at every new frame?

Hi guys, this may be a basic question and my apologies early if it is (I haven’t done C#/C++ since university and I just spent 12 hours today trying to figure it out).

My code is the same as "sample" walkthrough up to making the marker and before the drawing. I want to continuously display the X and Y coordinates in a LABEL on the FORM as the program update
with each new frame of the webcam. I thought you would put code in under:

void c_OnImageCaptured(object sender, CameraEventArgs e)

or

void m_OnChange(object sender, MarkerEventArgs e)

because these run with each new frame (or so I think). But when I tried to enter something like:

it gave me a "cross thread operation not valid" error. I'm not too sure what a thread is, but I gather each event function is a thread? I moved the code to a new button and it works
fine but it only updates when you click the button.

How do I make it update continuously with the frames? I will need to run more logic code and a serialport output write at this interval as well automatically with the frames.

the location is choosed exactly right, the Exception you encounter is rather "normal":

You can only change GUI values in the thread that created the GUI element. Fortunately, MS has built in support for your problem to "switch the thread":
(The trick in in the if (InvokeRequired) line ;))

Maybe I'm asking the wrong question. How can I take the marker X and Y data and bring it into another method? If I call the method from within m_OnChange(...) I will still have the cross-thread issue right?

If you want to access the marker data from a separate thread (like a UI thread in the case of updating a label), you'll ideally employ thread-safe accessors and mutators for the shared data. Thread safety is a large topic in which I don't claim to be an
expert. General programming reference guides should cover the topic sufficiently for you. Here's a short MSDN guide to Thread Synchronization in C#:
http://msdn.microsoft.com/en-us/library/ms173179.aspx that goes beyond BeginInvoke.

Keep us up to date on your project; we like hearing about creative uses for Touchless SDK. (Are you using XIM360?)

Well, I think everything is said by Mike, The generic parameter was killed by my html-source editing, sorry.

But just to clarify your questions:

I personally use this whenever I access global (class member) fields or properties. This makes it clear to the reader that these are not locally declared variables and second ensures that, when someone later adds a local variable with the same name the meaning
of the existing code is not changed. I think that depends a bit on your attitude whether you use it or not.

And in the code, the if-Part is entered when the execution is "not in the gui thread" and then calls itself in the else-part. So you have to add all code that acts with gui elements to the else part, any other code _could_ be in the if part, but
its simply more clean to do everything in the else part. You could also move the if into the OnChanged-Eventhandler to clean it up further:

This way, the MarkerChanged method is always called in the UI thread and you don't have the ugly invoke inside your business method.
Of Course, you could also pass the whole MarkerEventArgs object if you'd like.

What Mike said about threading is an imporant point:

You pass the MarkerEventData object as reference to another thread. No one guarantees you that the data will not be changed in the invoking (or any other) thread between the call to "BeginInvoke" and the execution of your code in the MarkerChanged
method (so called race conditions). You have to ensure this by locking the fields yourself and/or using copied data.
Multithreading is not trivial to use and I suggest you to read and think well before using.

Again said, the code above DOES HAVE race conditions.

But in this case, no harm is done as the touchless lib will create a new instance of the MarkerEventArgs and MarkerEventData when thenext image is parsed. So the MarkerEventData object you hold in your hand in the GUI thread will not change any further.
This does not apply to the MarkerEventArgs.Marker object. This is a reference to the (one and only) marker objected created with AddMarker.
This sample shows a race condition changing the SmoothingEnabled marker property:

The SmoothingEnabled property will at least in the "after sleep" line show the value "false", before the sleep I cannot say, this really depends on the order in which the CLR will call your code.
(infact, even after the sleep, the value may still be true, but 3 seconds delay give plenty of time for the other thread to execute a single line ;))

To answer your question: You can copy the X and Y values into new variables and invoke your method with these copies. As they are fully under your control, no one (except you, hehe) will change them while you wait for your other thread to execute...

(you could even directly move the e.EventData.X/Y into the object[] above, because they are value types. For refernece type members, such as the Marker object you would have to use the Clone() method or such in order to create the "private copy").

Sweet! that helps out a lot! I got my program working now. I use "this." keyword for all my global variables and it does clean it up and make it easier to read - thanks for that tip. You guys are going to make a programmer out of me yet =)