首页 > Windows开发 > 详细

C# 学习一些经验。

时间:2019-02-11 10:59:39      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:config   byte[]   \n   trie   obj   bytes   传递   odin   star   

1.启动线程:

using System.Threading;
using System.Threading.Tasks;

启动线程
Thread th1 = new Thread(new ThreadStart(threadthree)); //参数名不需要带括号
th1.IsBackground = true;
th1.Start();
设置线程函数
private void threadthree()
{
while (true)
{
/。。。/
System.Threading.Thread.Sleep(1000);
}
}

Thread th = new Thread(threadOne);//这个方式启动的线程不能柱塞
th.IsBackground = true;
th.Start("123");

2.启动进程 扫描某个进程 启动exe:
Process[] process = Process.GetProcesses();
foreach (Process prc in process)
{
Console.WriteLine(prc.ProcessName);
// if (prc.ProcessName == "CloudFfmpegOpengl")
// prc.Kill();
}

3.结构体的赋值发送

public struct Test
{
public int itype;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
public char[] name;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public char[] message;
public Test(int i, string name, string message)
{
this.itype = i;
this.name = name.PadRight(256, ‘\0‘).ToCharArray();
this.message = message.PadRight(256, ‘\0‘).ToCharArray();
}
};
TCP下面通讯使用的字节需要转换一下
public byte[] StructToBytes(object obj)
{
int size = Marshal.SizeOf(obj);
byte[] bytes = new byte[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(obj, structPtr, false);
Marshal.Copy(structPtr, bytes, 0, size);
Marshal.FreeHGlobal(structPtr);
return bytes;
}
使用的时候可以按照字节发送出去Test pack = new Test(type, psid, json);


4.TCP客户端连接

Socket socketclient = null;
socketclient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketclient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
socketclient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
IPAddress address = IPAddress.Parse(ConfigurationManager.AppSettings["ServerIP"]);
port = ConfigurationManager.AppSettings["OtherServerPort"];
IPEndPoint point = new IPEndPoint(address, int.Parse(port));
socketclient.Connect(point);
Test pack = new Test(type, psid, json);
byte[] Message = StructToBytes(pack);
socketclient.Send(Message);
Message = null;//C#有自己的GC不需要手动释放,最好不用了置空就行

5.TCP服务端连接

整体连接过程没变化多了特有的 绑定 监听 接受步骤
Socket socketWatch = null;
socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress address = IPAddress.Parse(ConfigurationManager.AppSettings["ServerIP"]);
string port = ConfigurationManager.AppSettings["ServerLocalPort"];
IPEndPoint point = new IPEndPoint(address, int.Parse(port));
socketWatch.Bind(point);//开启绑定......
socketWatch.Listen(10);//开启监听10个......
this.textBox4.Text += port;
this.textBox4.Text += "开启监听......\r\n";
while (true)
{
Socket connection = null;
connection = socketWatch.Accept();//开启循环接受......
string remoteEndPoint = connection.RemoteEndPoint.ToString();
byte[] recvBytes = new byte[1024];
int length = connection.Receive(recvBytes);
string strSRecMsg = Encoding.UTF8.GetString(recvBytes, 0, length);
writeLog("[线程" + Thread.CurrentThread.ManagedThreadId.ToString() + "] 收到一条消息:" + strSRecMsg);
}

 

6.两个窗口之间传递消息
from1窗体拉起from2窗体 并且在from2窗体结束的时候 传递一些消息,值之类的回到from1窗体


from2窗体:需要设置消息委托指定消息函数

public event setFixsTextValue setFormfixTextValue;
public delegate void setFixsTextValue(string textValue);
public void deviceInfo(string textValue)
{
string[] sArray = textValue.Split(new string[] { "", "\n" }, StringSplitOptions.RemoveEmptyEntries);
//this.textBox1.Text = textValue;
this.label8.Text = sArray[0];
}
窗体结束时:
setFormfixTextValue("传递的值 消息");
this.close();

from1窗体:需要引用from2窗体
using static testtool.from2;
from2 fp = new from2();
fp.deviceInfo(temp);//直接调用from2方法设置消息或者值
fp.setFormfixTextValue += new setFixsTextValue(fix_setFormTextValue);
fp.Show();//Show方法
void fix_setFormTextValue(string textValue)
{
string[] sArray = textValue.Split(new string[] { "", "\n" }, StringSplitOptions.RemoveEmptyEntries); //分割返回值
}
7.字符串的处理


7.1去掉某一个特殊字串400OK
string str = 400OK我们是中国人;

使用函数str = str.Replace("400OK", "");


7.2检测是否包含字串

使用函数if(str.Contains("400OK"))

 

C# 学习一些经验。

标签:config   byte[]   \n   trie   obj   bytes   传递   odin   star   

原文:https://www.cnblogs.com/xlsss159/p/10361075.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号