简体   繁体   English

在新线程C#中读取回调数据

[英]Read callback data in new thread C#

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM