静态类的范围是什么?

发布于 2024-07-08 03:00:59 字数 190 浏览 12 评论 0原文

我有一个程序集,一次可能被多个进程使用。 如果我使用静态类,多个进程是否都会使用该类的相同“实例”?

由于进程是分开的,这些进程是否会在不同的应用程序域下运行,因此静态“实例”是分开的吗?

这里的细节中的布丁是,该程序集由自定义 BizTalk 适配器使用,我将其设置为并行批处理消息。 这就是我上面所说的“多进程”。

I have an assembly that may be used by more than one process at a time. If I am using a static class, would the multiple processes all use the same "instance" of that class?

Since the processes are separate, would these be running under difference Application Domains, hence have the static "instances" separate?

The pudding in the details here is that the assembly is being used by a custom BizTalk adapter that my be set to process the messages in parallel batches. That is what I am calling "multiple processes" above.

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

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

发布评论

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

评论(5

心头的小情儿 2024-07-15 03:00:59

每个应用程序域静态类存在一次。 在您的情况下,这取决于适配器是在同一应用程序域中使用多个线程(从而共享静态类的单个实例)还是使用多个进程(从而具有静态类的单独实例)。

Static classes exist once per application domain. In your case, it would depend on whether the adapter is using multiple threads in the same application domain (thus sharing a single instance of the static class) or using multiple processes (thus having separate instances of the static class).

夏了南城 2024-07-15 03:00:59

多个线程将共享一个实例。 因此,静态类可以方便地在线程之间传递状态,但您需要非常小心,不要引入竞争条件(监视锁定您的属性)。

但是,多个进程应该位于单独的 AppDomain 中,因此每个进程都有自己的实例。

Multiple threads would share an instance. For this reason a static class can be convenient for passing state between threads, but you need to be very careful not to introduce race conditions (Monitor or lock your properties).

However, multiple processes should be in separate AppDomains and therefore each have their own instance.

暖心男生 2024-07-15 03:00:59

“我有一个程序集,一次可能被多个进程使用。如果我使用静态类,多个进程都会使用该类的同一个“实例”吗?”

否,它们都有单独的实例。

“由于进程是独立的,它们是否会在不同的应用程序域下运行,因此静态“实例”是独立的?”

是的。

"I have an assembly that may be used by more than one process at a time. If I am using a static class, would the multiple processes all use the same "instance" of that class?"

No, they all have separate instances.

"Since the processes are separate, would these be running under difference Application Domains, hence have the static "instances" separate?"

Yes.

蝶…霜飞 2024-07-15 03:00:59

静态类的范围仅限于应用程序域。 每个应用程序域都将拥有您可能拥有的任何静态变量的自己的副本。 如果您的“进程”是同一应用程序域中的线程,那么它们将共享静态值。 但如果它们实际上是单独的 Windows 进程,那么它们将具有不同的应用程序域,因此具有单独的副本。

The scope of a static class is limited to the application domain. Each app domain will have its own copy of any static variables you might have. If your "processes" are threads within the same app domain, then they will share the static values. But if they are actual separate Windows processes, then they will have different app domains and hence separate copies.

提笔书几行 2024-07-15 03:00:59

您可能想看看单例模式。 要点似乎是您想要控制服务实例的数量。

我猜你想要一个单独的 dll/项目来服务所有客户端请求。 您可以使用静态类/单例/多例来实现所需的功能。 这实际上取决于您想要实现的目标。

You may want to look at the Singleton pattern. The gist seems to be you want to control the number of service instances.

I'm guessing that you want a separate dll/project servicing all the client requests. You could use static class/singleton/multiton to implement the desired functionality. It really depends on what you are trying to accomplish.

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