两次运行相同应用程序时的C#静态方法 [英] C# Static Methods when running same application twice

查看:109
本文介绍了两次运行相同应用程序时的C#静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个用C#编写的具有静态方法的应用程序。



问题:如果我运行两个可执行文件的实例,同时运行,两个应用程序之间是否存在冲突只是因为它们有静态方法?



我问的原因是有人告诉我这可能会发生。

Hi,
I have an application written in C# that have static methods.

Question: If I run two instances of the executable, running at the same time, is there a clash between the two applications simply because they have static methods?

Reason I'm asking is that someone told me this might happen.

推荐答案

不,从不。这些过程很好地孤立;每个进程都在一个单独的隔离地址空间中运行在通常用于Windows系统(x86,x86-64,IE-64)的高级处理器体系结构上,这是CPU级别的硬件保护。如果这些过程属于同一个应用程序,则无关紧要。



此外,任何可能的冲突(在同一个过程中)都会发生并且与静态字段相关联或者属性,换句话说,与您共享相同内存位置的东西。它与静态方法无关。你根本不明白他们是什么。与实例方法相比,它们与通常的非OOP函数/过程类似。在实例方法中,获取一个隐式的this指针(在CLI的情况下为引用,托管指针),它引用了您的类型的某个实例,因此您可以访问该实例的其他实例成员。静态方法不会获得此引用,也无法访问任何实例。就是这样。



请参阅我过去关于静态方法与实例方法的答案:

在c#中输入类型 [ ^ ],

C#windows基础这个关键词及其在应用程序中的用途 [ ^ ] ,

什么使静态方法可以访问? [ ^ ]。


-SA
No, never. The processes are well isolated; each process runs in a separate isolated address space. On advanced processor architectures usually used for Windows systems (x86, x86-64, IE-64), this is the hardware protection at the level of CPU. If the processes are of the same application, it does not matter.

Besides, any possible clashes (inside the same process) can really happen and are associated with static fields or properties, in other words, with something where you share the same memory location. It has nothing to do with static methods. You simply don't quite understand what are they. They are just similar to "usual" non-OOP functions/procedures, in contrast to instance methods. In instance method gets an implicit "this" pointer (reference, managed pointer, in case of CLI), which references some instance of your type, so you can access other instance members of the instance. A static method does not get this reference and cannot access any instance. That's it.

Please see my past answers on static methods vs. instance methods:
Type casting in c#[^],
C# windows base this key word related and its uses in the application[^],
What makes static methods accessible?[^].

—SA


这篇关于两次运行相同应用程序时的C#静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆