A program sharing TEXT with its copy (opened twice)

╄→尐↘猪︶ㄣ 提交于 2020-01-06 07:56:31

问题


What i'm trying to do, is a simple program where i have 2 textboxes, and a button.(there is more but no need to say for resolving my problem)
When i write text in the 1st textbox and click the button, it will be written in the 2nd textbox. Everything is ok here.
Now, when i run this program more than once, i want the text to be written in the 2nd textbox of the others programs.
Sorry for the bad explaination i gave you before, hope it's better :)

OLD DESCRIPTION
i'm actually trying to make a program that works pretty much like a messenger (basically 2 textboxes, 1 to send, 1 to view, and a button to send) and i can't figure out how to link the program with itself (copies).
I want it to be able to read the textboxes contained in the other copies and i don't want it to be 1 program with 2 forms, but 1 single form, running severeal times (2 or more)
How can i do that?
/!\ NOTE : It will be on a single computer, just like 2 forms!


回答1:


I am assuming you are doing this in WinForms and only running one executable.

Sounds like you you want to create a single instance of a class that is shared between several forms. It should raise an event when the values change. After you create each instance of your form you pass in the class. In the form you would declare it With Events. When one form updates the class it would raise an event notifying the other forms to refresh themselves.

If you are familiar with WPF and MVVM this sort of design would be a little cleaner, but that takes some time to get up to speed on.

This design would not be appropriate if you want to run seperate applications and communicate between them.




回答2:


You will have to do some kind of IPC, respectively remoting.

You could use:

  • anonymous pipes
  • named pipes
  • UDP
  • TCP
  • WCF (would be overkill IMHO)
  • windows messaging (would be the easiest way)


来源:https://stackoverflow.com/questions/13049865/a-program-sharing-text-with-its-copy-opened-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!