Вопрос

Can someone help me, I have one method that I call to "activate" callback method(which is set to send me changed data every second) and second method(which is "activated" by first) that runs every second and give me data. That is all fine, but I need that second method run in new thread and after couple days of trying and reading I couldn't mange to do that. Can someone please guide me how to do that? here is my code.

    int ItemNumItems;
    Array ItemClientHandles;
    Array ItemServerValues;
    Array ItemQualities;
    Array ItemTimeStamps;

    public void Callback()
    {
        //Here I add a delegate with method to be called on data change.

        Grupa1.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(Grupa1_DataChange);
    }

    void Grupa1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
    {
        //Here I read my data and pass them to my variables(and this is 
        //runned every second) and I want this method to run in new thread.

        ItemNumItems = NumItems;
        ItemClientHandles = ClientHandles;
        ItemServerValues = ItemValues;
        ItemQualities = Qualities;
        ItemTimeStamps = TimeStamps;
    } 
Это было полезно?

Решение

Something like that maybe:

void Grupa1_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
    Array tmpClientHandles = ClientHandles;
    Array tmpItemValues = ItemValues;
    Array tmpQualities = Qualities;
    Array tmpTimeStamps = TimeStamps;

    ThreadPool.QueueUserWorkItem(o =>
    {
        //Here I read my data and pass them to my variables(and this is 
        //runned every second) and I want this method to run in new thread.

        ItemNumItems = NumItems;
        ItemClientHandles = tmpClientHandles;
        ItemServerValues = tmpItemValues;
        ItemQualities = tmpQualities;
        ItemTimeStamps = tmpTimeStamps;
   });
} 

?

But why to you want to run it asynchronously?

From the code you show there is nothing that will gain to being run in parallel.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top