我正在使用NamedPipeServerStream在两个进程之间进行通信.这是我初始化和连接管道的代码:
void Foo(IHasData objectProvider)
{
Stream stream = objectProvider.GetData();
if (stream.Length > 0)
{
using (NamedPipeServerStream pipeServer = new NamedPipeServerStream("VisualizerPipe", PipeDirection.Out, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous))
{
string currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string uiFileName = Path.Combine(currentDirectory, "VisualizerUIApplication.exe");
Process.Start(uiFileName);
if(pipeServer.BeginWaitForConnection(PipeConnected, this).AsyncWaitHandle.WaitOne(5000))
{
while (stream.CanRead)
{
pipeServer.WriteByte((byte)stream.ReadByte());
}
}
else
{
throw new TimeoutException("Pipe connection to UI process timed out.");
}
}
}
}
private void PipeConnected(IAsyncResult e)
{
}
Run Code Online (Sandbox Code Playgroud)
但它似乎永远不会等待.我经常遇到以下异常:
System.InvalidOperationException:管道尚未连接.在PeachesObjectVisualizer.Visualizer.Show的System.IO.Pipes.PipeStream.WriteByte(字节值)处的System.IO.Pipes.PipeStream.CheckWriteOperations()(IDialogVisualizerService windowService,IVisualizerObjectProvider objectProvider)
我认为在等待返回之后,一切都应该准备好了.
如果我使用pipeServer.WaitForConnection()一切正常,但如果管道没有连接则挂起应用程序不是一个选项.
我在WPF中创建了一个UserControl.此用户控件具有多个文本框,这些文本框绑定到数据库对象上的属性,该对象由UserControl上的proptery引用.xaml看起来像:
<TextBox Name="_txtFirstName" Text="{Binding Path=Contact.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
Run Code Online (Sandbox Code Playgroud)
这工作正常,直到我将Contact属性设置为依赖属性,以便我可以将它绑定到ListBox中的选定项.一旦我这样做了TextBoxes的绑定停止工作.为什么是这样?
DependencyProperty代码是:
public static readonly DependencyProperty ContactProperty = DependencyProperty.Register(
"Contact", typeof(Contacts), typeof(ContactView));
Run Code Online (Sandbox Code Playgroud)