函数等待其他应用程序的响应

发布于 2024-08-18 19:13:51 字数 655 浏览 3 评论 0原文

我们正在致力于集成两个同时运行并共享数据的不同应用程序。一个应用程序提供数据,另一个应用程序根据外部系统和数据计算一些值,并且必须将其提供回第一个应用程序。

我们使用这个库在应用程序之间共享数据: http:// /grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/Networking

该库基本上允许创建一个可以由任何应用程序查询的共享字典(只要它知道共享字典的位置) )。

因此,应该发生的情况是程序 A 必须向程序 B 提供一些数据,程序 B 使用这些数据并将其他一些数据返回给程序 A。

我的问题是如何让程序 A 等待 B 的响应。更具体地说,我可以将一个对象放入共享字典中,另一个程序收到字典中更改的通知,它可以计算一些属性并更新字典中的对象。程序 A 可以得到通知,但我希望程序 A 等待,直到它返回此响应 - 程序 A 的操作应该基于返回的值。

我认为可以做到这一点的一个非常丑陋的方法是在函数内部有一个无限循环,它不断查询字典以查看对象是否已更新 - 如果它已脱离循环并使用该对象及其计算属性。有谁知道更好的解决方案?

We are working on integrating two different applications that run simultaneously and share data. One application provides the data, the other one computes some values based off external systems and the data and has to provide it back to the first application.

We are using this library to share the data between the applications: http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/Networking

The library basically allows to create a shared dictionary which can be queried by any application (as long as it knows the location of the shared dictionary).

So, what should happen is program A has to provide some data to program B and program B uses this data and returns some other data back to program A.

My problem is how do I make the program A wait for the response from B. In more concrete terms, I can put an object in the shared dictionary, the other program gets notified of a change in the dictionary, it can compute some attribtues and update the object in the dictionary. Program A can get notified, but I want program A to wait till it gets back this response - program A's action should be based on the returned value.

A very ugly way I see this can be done is have an infinite loop inside the function, that keeps querying the dictionary to see if the object has been udpated - if it has break out of the loop and use the object and its computed attributes. Does anyone know of a nicer solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦晓ヶ微光ヅ倾城 2024-08-25 19:13:51

使用他们的订阅模型,您可以避免所有无限的-循环。 当您唯一需要检查的时间是实际更新的时间时,为什么您需要循环?因为您订阅了字典中的关键模式,并且当符合该模式的键出现时,您的连接将收到通知更新/添加,您只需要检查即可。

所以基本上,您可以使用 ManualResetEvent 来等待您自己的系统内的同步。以下是 ManualResetEvent 的使用示例:

using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        //    create the reset event -- initially unsignalled
        var resetEvent = new ManualResetEvent(false);
        //    information will be filled by another thread
        string information = null;

        //    the other thread to run
        Action infoGet = delegate
        {
            //    get the information
            information = Console.ReadLine();
            //    signal the event because we're done
            resetEvent.Set();
        };

        //    call the action in a seperate thread
        infoGet.BeginInvoke(null, null);
        //    wait for completion
        resetEvent.WaitOne();
        //    write out the information
        Console.WriteLine(information);
    }
}

要转换为您的框架,您可能让订阅处理程序检查更新的内容,找到等待句柄并向其发出信号,从而推进适当的等待线程。

Using their subscription model, you are able to avoid all infinite-looping. Why would you need to loop when the only times you need to check is when something is actually updated? Since you subscribe to key patterns in the dictionary and your connection will be notified when a key fitting that pattern is updated/added, you only need to check on that.

So basically, you can use a ManualResetEvent to wait for synchronization within your own system. Here is an example of usage of ManualResetEvent:

using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        //    create the reset event -- initially unsignalled
        var resetEvent = new ManualResetEvent(false);
        //    information will be filled by another thread
        string information = null;

        //    the other thread to run
        Action infoGet = delegate
        {
            //    get the information
            information = Console.ReadLine();
            //    signal the event because we're done
            resetEvent.Set();
        };

        //    call the action in a seperate thread
        infoGet.BeginInvoke(null, null);
        //    wait for completion
        resetEvent.WaitOne();
        //    write out the information
        Console.WriteLine(information);
    }
}

To translate to your framework, you might have the subscription handler check what was updated, find that wait handle and signal it, thus advancing the proper waiting threads.

悟红尘 2024-08-25 19:13:51

使用 ManualResetEvent。

 // machine A
 var event = new ManualResetEvent(false);
 B_Listener.OnChanged += delegate { event.Set(); }
 myDictionary.UpdateValue();
 event.WaitOne();

use a ManualResetEvent.

 // machine A
 var event = new ManualResetEvent(false);
 B_Listener.OnChanged += delegate { event.Set(); }
 myDictionary.UpdateValue();
 event.WaitOne();
℡Ms空城旧梦 2024-08-25 19:13:51

如果不使用 IPC 的其他方法(因为您似乎已经选择了这个特定的库),在我看来,连续轮询是唯一的方法。如果您愿意(并且能够)实现其他 IPC 方法,那么您应该停止使用当前的库,而自己在程序之间传递数据。

Without using other methods of IPC (since you've seemed to settle on this particular library) it would seem to me that continuous polling is about the only way. If you are willing (and able) to implement other IPC methods then you should discontinue use of the current library and just pass the data between the programs yourself.

心不设防 2024-08-25 19:13:51

如果我理解正确的话,在程序 B 更新该值之前,程序 A 不得执行任何其他逻辑,并且程序 A 可以从 UCalgary 的该系统获取所述更新的通知。

基于这些假设,我建议在将数据发送到共享字典后,启动一个计时器,其 Tick 事件由一个方法处理,该方法将 (a) 检查字典是否有更新,或者(b) 检查由来自共享字典库的通知设置的变量。

一旦发现更新,停止计时器,并执行将开始处理新更新的值的方法。

计时器与您建议的“无限循环”没有什么不同,但它实际上没有处理开销,可以设置为以实际间隔(即 1 秒、1 分钟、24 小时)进行检查,并且可以设置为时间如果程序 B 或字典的事情没有按计划进行,则退出。

If I understand correctly, Program A mustn't execute any additional logic until Program B has updated the value, and Program A can get a notification of said update from that system out of UCalgary.

Based on those assumptions, I'd suggest that after sending data to the shared dictionary, you start a timer whose Tick event is handled by a method that will either (a) check the dictionary for updates, or (b) check a variable that gets set by the notification that comes in from the shared dictionary library.

Once the update has been found, stop the timer, and execute the method that will pick up processing the newly updated value.

The timer is not unlike the "infinite loop" you suggested, but it has virtually no processing overhead, can be set to check at a realistic interval (be that 1 second, 1 minute, 24 hours), and can be set up to time out if things don't go as planned with Program B or the dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文