使用回调访问主线程

时间:2022-01-15 13:03:14

I have a program in C# that connects to a server using a DuplexChannelFactory, and that server can call methods from client side by callback, the problem is that I have no idea how to access the Main Thread were the connection was created from the callback methods. Is there any way to do this, by means of passing an object, or do I have to implement an additional communication layer (BD, files, whatever)?

我在C#中有一个使用DuplexChannelFactory连接到服务器的程序,该服务器可以通过回调从客户端调用方法,问题是我不知道如何通过回调方法创建连接来访问主线程。有没有办法做到这一点,通过传递一个对象,或者我是否必须实现一个额外的通信层(BD,文件,等等)?

Hope this was not much confusing.

希望这不会让人感到困惑。

Thanks in advance.

提前致谢。

2 个解决方案

#1


I'm not sure I entirely understand your question, but I'll take a stab at it:

我不确定我是否完全理解你的问题,但我会抓住它:

My understanding of the question:

我对这个问题的理解:

You have a client and server. Your client makes a webservice call to the server, and the server can then make some sort of call back to the client in the process of calculating whatever it calculates. You have this level of infrastructure working, but you're now having difficulty communicating between the callback's state and the state of the original request.

你有一个客户端和服务器。您的客户端向服务器发出Web服务调用,然后服务器可以在计算任何计算的过程中向客户端发出某种回调。您可以使用此级别的基础架构,但现在您在回调状态与原始请求的状态之间进行通信时遇到困难。

I see a few options here, depending on the nature of the state that you're trying to share.

我在这里看到一些选项,具体取决于您尝试分享的州的性质。

1) If the state that you're trying to share isn't spinning (IE, if it's known when you make the request) and isn't terribly large, I think the most sensible option would be to pass that state along with the webservice call so that the server can pass it back in to the callback.

1)如果你试图分享的状态没有旋转(IE,如果它在你提出请求时已知)并且不是非常大,我认为最明智的选择是将该状态与webservice调用,以便服务器可以将其传回回调。

2) If the state is spinning, you may provide access to it via some sort of icky, but threadsafe, global mechanism. Perhaps the much-maligned singleton, or something similar.

2)如果状态正在旋转,您可以通过某种icky但线程安全的全局机制提供对它的访问。也许是备受诟病的单身人士,或类似的东西。

3) If the state isn't spinning, but is large, you may want to consider pre-calculating the server's request before sending the initial request.

3)如果状态没有旋转,但是很大,您可能需要考虑在发送初始请求之前预先计算服务器的请求。

There are a variety of techniques, of which these are only a very few, but there really isn't enough information in the question to give a good answer.

有各种各样的技术,其中只有极少数,但在问题中确实没有足够的信息来给出一个好的答案。

#2


What is your reason for wanting to access the main thread? Do you need to update a UI, and can only do that on the main thread?

你想要访问主线程的原因是什么?您是否需要更新UI,并且只能在主线程上执行此操作?

In that case, look at System.Windows.Forms.Control.Invoke, which can queue a delegate to be executed on the UI thread.

在这种情况下,请查看System.Windows.Forms.Control.Invoke,它可以对要在UI线程上执行的委托进行排队。

#1


I'm not sure I entirely understand your question, but I'll take a stab at it:

我不确定我是否完全理解你的问题,但我会抓住它:

My understanding of the question:

我对这个问题的理解:

You have a client and server. Your client makes a webservice call to the server, and the server can then make some sort of call back to the client in the process of calculating whatever it calculates. You have this level of infrastructure working, but you're now having difficulty communicating between the callback's state and the state of the original request.

你有一个客户端和服务器。您的客户端向服务器发出Web服务调用,然后服务器可以在计算任何计算的过程中向客户端发出某种回调。您可以使用此级别的基础架构,但现在您在回调状态与原始请求的状态之间进行通信时遇到困难。

I see a few options here, depending on the nature of the state that you're trying to share.

我在这里看到一些选项,具体取决于您尝试分享的州的性质。

1) If the state that you're trying to share isn't spinning (IE, if it's known when you make the request) and isn't terribly large, I think the most sensible option would be to pass that state along with the webservice call so that the server can pass it back in to the callback.

1)如果你试图分享的状态没有旋转(IE,如果它在你提出请求时已知)并且不是非常大,我认为最明智的选择是将该状态与webservice调用,以便服务器可以将其传回回调。

2) If the state is spinning, you may provide access to it via some sort of icky, but threadsafe, global mechanism. Perhaps the much-maligned singleton, or something similar.

2)如果状态正在旋转,您可以通过某种icky但线程安全的全局机制提供对它的访问。也许是备受诟病的单身人士,或类似的东西。

3) If the state isn't spinning, but is large, you may want to consider pre-calculating the server's request before sending the initial request.

3)如果状态没有旋转,但是很大,您可能需要考虑在发送初始请求之前预先计算服务器的请求。

There are a variety of techniques, of which these are only a very few, but there really isn't enough information in the question to give a good answer.

有各种各样的技术,其中只有极少数,但在问题中确实没有足够的信息来给出一个好的答案。

#2


What is your reason for wanting to access the main thread? Do you need to update a UI, and can only do that on the main thread?

你想要访问主线程的原因是什么?您是否需要更新UI,并且只能在主线程上执行此操作?

In that case, look at System.Windows.Forms.Control.Invoke, which can queue a delegate to be executed on the UI thread.

在这种情况下,请查看System.Windows.Forms.Control.Invoke,它可以对要在UI线程上执行的委托进行排队。