我在我的解决方案中分离了两个项目,因为它们每个都需要针对不同 CPU 的库。 在我的一个项目中,我只有响应点击的类(我们称之为 ProjectClick 64 位库),另一个是一种带有 MVVM 实现的UI (ProjectUser 32 位库)。
我正在寻找的是一种让 ProjectUser 知道点击是由 ProjectClick 执行的方法,没有 Project Click 不知道任何其他事情。
到目前为止我尝试了什么
我一直在上网和阅读书籍,以便更多地了解 C#。据我了解,要进行交流,最好的方法是创建一个界面。我一直在看这个subject寻求答案,并一直在尝试实现第三个项目,并在两者之间建立接口(interface)。
好的,代码来了,(这是一个有意简化的代码,我希望它足够清晰)
首先是界面(在控制台应用程序中)
namespace LinkApplication
{
public interface IEvent
{
bool CompareClick { get; set; }
}
}
然后,项目点击是一个wpf
namespace ProjectClick
public partial class MainWindow : Window, IEvent
{
public MainWindow()
{
try { InitializeComponent(); }
catch (Exception e)
{
Console.WriteLine(e.InnerException);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
CompareClick = true;
}
private void Button_Leave(object sender, RoutedEventArgs e)
{
CompareClick = false;
}
}
最后是用户界面
namespace ProjectUser
{
public partial class MainWindow : Window, IEvent, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
DataContext = this;
}
public bool CompareClick { get; set; }
public bool ClickCheck
{
get { return CompareClick; }
set
{
if (value != CompareClick)
{
CompareClick = value;
OnPropertyChanged("ClickCheck");
}
}
}
你可以在窗口中看到realted标签
<Label Content="{Binding ClickCheck}" HorizontalAlignment="Left" Margin="690,358,0,0" VerticalAlignment="Top"/>
这里,值一直停留在false,我不是很明白变化值的逻辑。我还在学习,我在网上看到了其他几个想法,比如一个自定义事件处理程序,但我不太了解两个项目之间的实现,因为它们彼此不了解。如果有人可以引导我找到可能的解决方案或更好的执行方式,我会很高兴。
编辑
我最好避免在 ProjectUser
中引用 Project Click
以保留不同 CPU 目标的特权。反之则没有问题。
感谢您的友好回答。
最佳答案
我得到了很大的建议,并研究了实例之间的进程间通信。我调查了不同的事情,但最令人满意的答案是 Omegaman's blog (有点感谢这个主题)。
所以基本上,我试图避免使用本地主机信息,认为会有更直接的解决方案。但由于我们没有想到更好的东西,我想这就是我一直在寻找的东西。
我发现了什么
现在,这里的解决方案是使用 WCF 服务和 NamedPipes。通过创建 Sender 和 Receiver 操作,ProjectUser
和 ProjectClick
这两个进程永远不会直接相遇。您有一个由 WCF 控制的管道。你可以在博客上看到更多关于如何交流的细节,我只是通过改变传递的信息来改编(没有太大的改变)他所做的。
不过要注意一件事
这两个进程不能同时启动,接收者必须首先启动以监听通过的信息。基本上,发件人必须在之后开始。
我在 WPF 中创建了两个窗口,以及一个 WCFServiceLibrary。单击按钮时,会增加,并在第二个屏幕上显示数字。
一些代码
在Omegaman的博客上可以看到很多,我就把我修改的贴出来。
在ProjectUser
端,本应接收,标签更新如下
接收管道 = new Receiver();
公共(public)主窗口()
{
初始化组件();
//this.WindowStartupLocation = WindowStartupLocation.CenterScreen; //start the window at the centre of the screen
DataContext = this;
pipe.Data += new PipeLink.PipeService.DataIsReady(DataBeingRecieved);
if (pipe.ServiceOn() == false)
MessageBox.Show(pipe.error.Message);
label1.Content = "Listening to Pipe: " + pipe.CurrentPipeName + Environment.NewLine;
}
void DataBeingRecieved(int data)
{
Dispatcher.Invoke(new Action(delegate()
{
label1.Content += string.Join(Environment.NewLine, data);
label1.Content += Environment.NewLine;
}));
}
在 ProjectClick
端,应该发送,按钮点击更新如下
int i;
public MainWindow()
{
try { InitializeComponent(); }
catch (Exception e)
{
Console.WriteLine(e.InnerException);
}
i = 0;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int messages;
i++;
Stopwatch stoop = new Stopwatch();
stoop.Start();
messages = i;
try
{
PipeLink.Sender.SendMessage(messages);
stoop.Stop();
Console.WriteLine(stoop.ElapsedMilliseconds + " ms");
}
catch (Exception u)
{
Console.WriteLine(u);
}
}
代码的重要部分是使用 NetNamedPipeBinding 创建管道本身。这是整个通信发生的地方
您可以在 PipeService 代码中看到它:
public class PipeService : IPipeService
{
public static string URI
= "net.pipe://localhost/Pipe";
// This is when we used the HTTP bindings.
// = "http://localhost:8000/Pipe";
#region IPipeService Members
public void PipeIn(int data)
{
if (DataReady != null)
DataReady(data);
}
public delegate void DataIsReady(int hotData);
public DataIsReady DataReady = null;
#endregion
}
速度怎么样?
我担心简单的数据可能比简单的点击需要更长的时间才能到达。我错了:由于第一次连接,第一个数字比其他数字花费的时间更长,大约一秒钟。但在那之后,点击大约 100 次,我的平均时间为 10 毫秒(我知道这不是重要的数据,但我仍然认为测试几次是好的)。
我在 GitHub 上推送所有内容与 Andreas 一起使用,供任何可能感兴趣的人使用。
不过我仍然不知道代码是否优化。如果您有更好的解决方案,我会很乐意阅读。
关于c# - 一个解决方案中两个项目和事件处理程序之间的接口(interface)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32373221/