在 C# 中运行的应用程序之间共享变量

编程入门 行业动态 更新时间:2024-10-28 02:30:53
本文介绍了在 C# 中运行的应用程序之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 开发两个简单的应用程序,在没有网络要求的同一台本地机器上运行.

I am developing in C# two simple applications, running in the same local machine without network requirements.

第一个应用程序初始化一个 DLL (Class1) 并设置一个变量.第二个应用程序只是读取先前存储的数据.两个应用程序都实例化相同的 Class1.

The first application initializes an DLL (Class1) and set a variable. The second application just read it the data which was previously stored. Both applications instanciates the same Class1.

代码:

DLL(Class1):

DLL (Class1):

public class Class1
{

private string variableName;

public string MyProperty
 {
    get { return variableName; }
    set { variableName = value; }
  }

}

应用程序 A:

Application A:

class Program
{
static void Main(string[] args)
{
    Class1 class1 = new Class1();

    string localReadVariable = Console.ReadLine();

    class1.MyProperty = localReadVariable;

   }
}

应用程序 B:

Application B:

class Program
{
    static void Main(string[] args)
{
    ClassLibraryA.Class1 localClass = new ClassLibraryA.Class1();

    string z = localClass.MyProperty;

    Console.WriteLine(z);
}
}

我的问题是我不知道如何从另一个线程读取变量.

My problem is that I do not know how to read a variable from another thread.

应用程序 B 必须读取应用程序 B 设置的变量名"

Application B must read the "variableName" set by application B

谢谢

推荐答案

您需要某种机制来在应用程序之间进行通信.

You need some sort of mechanism to communicate between the applications.

这可以通过注册表、文件、内存映射文件等等……

This can be through the registry, files, memory mapped files etc...

如果两个应用程序都需要编写,则需要在代码中添加同步逻辑.

If both applications are expected to do write, you need to add synchronization logic to your code.

这篇关于在 C# 中运行的应用程序之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 20:58:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/813716.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   应用程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!