2015-09-27 36 views
-3

我在计算控制台应用程序中的数据之后卡住了我的项目,我需要将它传递给窗体窗体应用程序,然后在条形图图表上显示值。我怎样才能将我的变量值从控制台应用程序传递给窗体窗体应用程序

+0

您的Windows窗体应用程序是否调用您的控制台应用程序? – ycsun

+0

你是否想用一些参数运行你的WinForms应用程序,或者你想在两个启动的应用程序之间交换一些数据? – Spawn

+0

他们都在同一个项目 –

回答

0

您有一整套选择,各种运输方式,复杂程度,性能&开销不一。一个简单的国家:

假设你有一个服务:

public class ServiceType 
{ 
    public void DoSomething(string someData) 
    { 
     Debug.WriteLine("Got this " + someData); 
    } 
} 
在托管应用程序

然后(Windows窗体):

 IDictionary properties = new Hashtable(); 
     properties.Add("authorizedGroup", "NT AUTHORITY\\NETWORK SERVICE"); // or some other user account that runs the console app 
     properties.Add("portName", "myApp" + Guid.NewGuid()); // if you rapidly restart your app then the pipe may still be there 
     IpcServerChannel serverChannel = new IpcServerChannel(properties, null); 
     ChannelServices.RegisterChannel(serverChannel, true); 
     RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServiceType), "service", WellKnownObjectMode.Singleton); 

然后在控制台应用程序:

  var pipename = System.IO.Directory.GetFiles(@"\\.\pipe\").FirstOrDefault(x => x.Contains("myApp"))) 

      IpcClientChannel clientChannel = new IpcClientChannel(); 
      ChannelServices.RegisterChannel(clientChannel, true); 
      var s = (ServiceType)Activator.GetObject(typeof (ServiceType), "ipc://" + pipename.Replace(@"\\.\pipe\", string.Empty) + "/service"); 
      s.DoSomething("data"); 
      ChannelServices.UnregisterChannel(clientChannel); 

当然,您必须共享ServiceType的库。

相关问题