Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use WCF threads

Shulou Source: shulou.com Published: 2022-06-02 18:34:45 09月28日 Update

Editor to share with you how to use WCF threads, I hope you have something to gain after reading this article, let's discuss it together!

Although many people are skeptical about WCF threads, his security continues to improve year after year. It is possible to keep the WCF thread running properly, but only if you have a deep understanding of what a WCF thread is and how it works.

For these operations, any creation of a new thread to execute is equivalent. However, in some cases, some operations can only be performed under a fixed thread. For example, in a GUI application, access to a control needs to be performed under the thread that created the control

Or we set some Context information in a fixed thread through TLS (Thread Local Storage) for specific operations, and we call the operation and the dependency of a fixed thread thread Thread Affinity.

In the case of WCF, whether you host a service or specify a callback when calling Service, by default, the operations of service and callback are automatically associated with the current SynchonizationContext (if any).

That is, if our service is host under the Windows Form application, then the operation of the service will be performed under the UI thread; similarly, how we call the duplex service and define the callback in a Windows Forms UI thread, then the final execution of the callback will be in the UI thread.

The control of WCF thread affinity can be set through the UseSynchronizationContext property of ServiceBehavior or CallbackBehavior, which defaults to true, which is the reason why WCF has thread affinity by default.

Now let's implement our second set of scenarios: so that Callback execution does not have to be bound to UI threads. To do this, we just need to add what kind of CallbackBehavior attribute.

Namespace Artech.ThreadAffinity.Clients {[CallbackBehavior (UseSynchronizationContext = false)] public class CalculateCallback: ICalculateCallback {public static TextBox ResultPanel; # region ICalculateCallback Members public void DisplayResult (double result) {ResultPanel.Text = result.ToString ();} # endregion}} after reading this article, I believe you have some understanding of "how WCF threads are used". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Threads associations relevance continuance properties situations controls articles security for this year after year that is information premises reasons can be passed security done scheme Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Docker Linux Microsoft MariaDB