在 C# 中运行的应用程序之间共享变量 [英] sharing variables between running applications in C#

查看:18
本文介绍了在 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屋!

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