Simple event listener from a thread

Hi all.
I keep learning about plugins in vvvv
I try to make a listener from a thread and get its value at vvvv output pin.

I take a simple metronome example which sends a tick every 3 seconds.

right now, the code crashes vvvv.
The key of my issue must be is it possible to handle stuff outside the Evaluate()…

Apparently the issue would be at the launch of the thread…
any tips would be appreciated :)
binaries are attached to this message… (EDIT: I can’t upload the dll file).
here’s the code:

namespace evens_metro_vvvv
{
[PluginInfo(Name = "Thread_Listener", Category = "first step", AutoEvaluate = true)]

#region IPluginEvaluate
public class Class1 : IPluginEvaluate
{
    #region pins
    [Input("Launch Thread", IsBang = true)]
    ISpread<bool> FInput;

    [Output("Thread Listener")]
    ISpread<int> FOutput;
    #endregion pins

    #region declare
    Metronome m = new Metronome();
    Listener l = new Listener();
    bool FirstFrame = true;
    #endregion declare

    #region Evaluate()
    public void Evaluate(int SpreadMax)
    {
        if (FirstFrame && FInput[0]) //Launch the thread when Bang.
        {
            l.Subscribe(m);
            m.Start();
            FirstFrame = false;
        }
        FOutput[0] = l.valeur();      //Assign listener value to the Output pin.
    }
    #endregion Evaluate()
}
#endregion IPluginEvaluate

#region Metronome
public class Metronome
{
    public event TickHandler Tick;
    public EventArgs e = null;
    public delegate void TickHandler(Metronome m, EventArgs e);
    public void Start()
    {
        while (true)
        {
            System.Threading.Thread.Sleep(3000);
            Tick?.Invoke(this, e); // if threaed != null {Tick(this, e)};
        }
    }
}
#endregion Metronome

#region Listener
public class Listener
{
    private int z = 0;
    public void Subscribe(Metronome m)
    {
        m.Tick += new Metronome.TickHandler(HeardIt);
    }
    public void HeardIt(Metronome m, EventArgs e)
    {
        z++;
        //Console.WriteLine("Heard IT");
    }
    public int valeur()
    {
        return z;
    }
}
#endregion Listener

}

test_thread_listener.v4p (3.2 KB)

Hi man, i’m not sure this gonna work, since you are in same thread as vvvv actually, but i can be wrong

i’m pretty sure you can find some good code examples on github

this also doesn’t look right, cause this will be called every frame basically, if you want to preintialize something you need this IPartImportsSatisfiedNotification
(example dx11-vvvv/Core/VVVV.DX11.Lib/BaseNodes/ConsNonNilNode.cs at a8e29a0f85658c5ab58542f07187730f21775285 · mrvux/dx11-vvvv · GitHub)

antokhio is right, you are sending the main thread to sleep forever…

Have a look at System.Threading.Task to make the metronome.start method asynchronous:
https://msdn.microsoft.com/de-de/library/system.threading.tasks.task(v=vs.110).aspx

the declare region looks ok in my opinion. as far as I know, only the evaluate function gets called every frame!

right, you are never actually creating a new thread. you are only creating instances of the classes and call them from the main thread. so what you do is blocking vvvv forever every 3 seconds.

there are several threading patterns in C# which are not so easy to understand. a very good source to read is this online book: Threading in C# - Free E-book

it is not very easy to rewrite your example with threading and thread safe events. because you not only have to start a new thread, but you also want to send data back to the main thread via the Tick event every 3 seconds.

Tasks are more easy to handle async stuff in C# and definitely the first thing to look at. But they also do not handle the recurring events back to the main thread in an easy way.

depending on what you want to do, VL has a AsycTask and AsyncLoop region that do all the complicated threading stuff for you and you just have to fill out the functionality that you want to do asynchronously. check the AsyncTask example in the VL girlpower and the attached patch that does the same as you were trying to do in the C# code:

AsyncLoop.zip (5.9 KB)

and if you need threading only because you want to implement a metronome-like function have a look at the Timers that are part of .net:
https://msdn.microsoft.com/de-de/library/system.timers.timer(v=vs.110).aspx

they do the async stuff automatically!

Hi all !
Thank you so much for your responses !!
I’ll check them out !
my final purpose is to catch events from the eyetracker Tobi EyeX (which is not handled by the actual node Eytracker.)

but first I need to understand how to deal with threads in plugin in vvvv.
all your links should help me, thanks !
I’ll post simples patches which demonstrate thread implementation in a vvvv plugin with a listen handler.
Merci merci !

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.