Problems to do Multithreading with C #

4

Well, I'm having a problem with multithreading on a Kinect system I'm developing for my research project.

I understand that this happens because I'm trying to access a Thread B resource that is on Thread A, thus launching the System.InvalidOperationException . The question is: How can I solve this? I really do not have a clue how to do it (it's the first time I'm working with more than one thread).

Thanks in advance.

Oh, if you need anything else in the code, please let me know! : D

    
asked by anonymous 05.08.2015 / 22:47

1 answer

2

One way to access an object that belongs to another Thread is through Delegate :

private delegate void translateObjectDelegate();

private void translateObject(){
    if (translate.InvokeRequired)
        {
            translateObjectDelegate delegateTO = new translateObjectDelegate(translateObject);
            this.Invoke(delegateTO, null);
        }
    else
        {
             //translate.OffsetX = ..........
             //aqui você continua o método normal
        }
}

First, the method checks if the object is accessible in Thread current in line if (translate.InvokeRequired) , if it is not accessible, it is instantiated a Delegate and it is invoked, triggering execution of the method again. object is accessible. If the object is accessible from the beginning, the method will execute normally.

Some sources for reading about Delegate and Thread/Invoke :

MSDN Delegate

MSDN Control.Invoke

MSDN Control.InvokeRequired

    
12.08.2015 / 13:35