1-) C# RMOS - formdan forma veri gönderme farklı uygulamalar için
SERVER
using System;
using System.IO;
using System.IO.Pipes;
using System.Windows.Forms;
namespace paramatreServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void server()
{
var pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4);
StreamReader sr = new StreamReader(pipeServer);
StreamWriter sw = new StreamWriter(pipeServer);
do
{
try
{
pipeServer.WaitForConnection();
string test;
sw.WriteLine("Waiting");
sw.Flush();
pipeServer.WaitForPipeDrain();
test = sr.ReadLine();
Application.DoEvents();
this.Invoke(new MethodInvoker(() =>
{
memoEdit1.Text += "\n" + test;
Console.WriteLine(memoEdit1.Text);
}));
}
catch (Exception ex) { throw ex; }
finally
{
pipeServer.WaitForPipeDrain();
if (pipeServer.IsConnected) { pipeServer.Disconnect(); }
}
} while (true);
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
server();
}
}
}
CLİENT async tavsiye edilen
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Pipes; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace Client1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { client(); } private async void client() { var pipeClient = new NamedPipeClientStream(".", "testpipe", PipeDirection.InOut, PipeOptions.None);
try { await pipeClient.ConnectAsync(5000); // 5 saniye içinde bağlantı kurmaya çalış
if (pipeClient.IsConnected) { StreamReader sr = new StreamReader(pipeClient); StreamWriter sw = new StreamWriter(pipeClient);
string temp = sr.ReadLine();
if (temp == "Waiting") { sw.WriteLine(textBox1.Text); sw.Flush(); pipeClient.Close(); } } else { MessageBox.Show("Bağlantı kurulamadı. Lütfen POS'u çalıştırın."); } } catch (Exception ex) { // Hata durumuyla ilgili bir işlem yapabilirsiniz. MessageBox.Show("Hata oluştu: " + ex.Message); } }
} }
|
CLİENT
using System;
using System.IO;
using System.IO.Pipes;
using System.Windows.Forms;
namespace paramatreClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
client();
}
private void client()
{
var pipeClient = new NamedPipeClientStream(".",
"testpipe", PipeDirection.InOut, PipeOptions.None);
if (pipeClient.IsConnected != true) { pipeClient.Connect(); }
StreamReader sr = new StreamReader(pipeClient);
StreamWriter sw = new StreamWriter(pipeClient);
string temp;
temp = sr.ReadLine();
if (temp == "Waiting")
{
try
{
sw.WriteLine(memoEdit1.Text);
sw.Flush();
pipeClient.Close();
}
catch (Exception ex) { throw ex; }
}
}
}
}