WaitCallback

WaitCallback 委托一般用于将方法异步执行到线程池线程上。线程池是一组可重用的线程,可以在需要时分配线程来执行工作,而不需要创建新的线程。使用线程池可以避免频繁地创建和销毁线程,从而提高应用程序的性能和可伸缩性。 通常,您可以使用 ThreadPool.QueueUserWorkItem 方法将方法异步执行到线程池线程上,并使用 WaitCallback 委托来表示要执行的方法。在这种情况下,WaitCallback 委托的参数是一个 object 类型的对象,该对象可以包含要传递给方法的数据。 ...

2025年7月24日 · 1 分钟 · 283 字

yield

在 C# 中,yield 關鍵字用於定義一個迭代器方法(Iterator Method), 這個方法可以產生一個可枚舉的序列。當迭代器方法被調用時, 它不會立即執行方法體中的程式碼,而是返回一個 IEnumerator 對象。 當使用者對這個 IEnumerator 對象調用 MoveNext() 方法時,才會執行迭代器方法中的程式碼,並返回序列中的下一個元素。 ...

2025年7月24日 · 2 分钟 · 851 字

Tuple

可以通过nuget安装 NetLegacySupport.Tuple。这是从.Net 4.5移植到.Net 2.0和3.5的Tuple类 下面是一个使用 Tuple 的简单示例: using System; class Program { static void Main(string[] args) { // 创建一个包含两个元素的 Tuple var tuple = Tuple.Create("John", 30); // 访问 Tuple 中的元素 Console.WriteLine("Name: " + tuple.Item1); Console.WriteLine("Age: " + tuple.Item2); // 创建一个包含三个元素的 Tuple var tuple2 = Tuple.Create("Mary", 25, "Female"); // 访问 Tuple 中的元素 Console.WriteLine("Name: " + tuple2.Item1); Console.WriteLine("Age: " + tuple2.Item2); Console.WriteLine("Gender: " + tuple2.Item3); } } 在上面的示例中,我们使用 Tuple.Create 方法创建了两个不同元素数量的 Tuple,并访问了其中的元素。Tuple 的元素可以通过 Item1、Item2、Item3 等属性访问,也可以使用解构语法将元素分解成多个变量。 ...

2025年7月24日 · 1 分钟 · 395 字

C#中异步调用函数的方法有哪些

在 C# 中,有几种方法可以进行异步调用函数: 使用 async/await 关键字:async/await 是 C# 中用于编写异步代码的关键字组合。你可以将需要异步执行的函数标记为 async,然后在其中使用 await 关键字来等待异步操作完成。这样可以使函数在异步操作执行期间不阻塞,并在操作完成后继续执行。例如: public async Task MyAsyncMethod() { // 异步操作 await Task.Delay(1000); // 异步操作完成后的代码 Console.WriteLine("异步操作完成"); } 使用 Task.Run 方法:Task.Run 方法可以将一个方法或代码块包装成一个 Task 对象,并在后台线程上执行。这样可以实现异步调用函数的效果。例如: public void MyMethod() { // 异步调用函数 Task.Run(async () => { // 异步操作 await Task.Delay(1000); // 异步操作完成后的代码 Console.WriteLine("异步操作完成"); }); // 继续执行其他代码 Console.WriteLine("异步调用进行中"); } 使用委托和回调函数:你可以定义一个异步委托,并使用回调函数来处理异步操作完成后的结果。例如: public delegate void MyAsyncDelegate(); public void MyMethod() { // 创建异步委托实例 MyAsyncDelegate asyncDelegate = new MyAsyncDelegate(MyAsyncMethod); // 异步调用委托 asyncDelegate.BeginInvoke(MyAsyncCallback, null); // 继续执行其他代码 Console.WriteLine("异步调用进行中"); } public void MyAsyncMethod() { // 异步操作 Thread.Sleep(1000); } public void MyAsyncCallback(IAsyncResult result) { // 异步操作完成后的代码 Console.WriteLine("异步操作完成"); } 这些是在 C# 中进行异步调用函数的几种常见方法。你可以根据具体的需求选择适合的方法来实现异步操作。

2025年7月24日 · 2 分钟 · 611 字

特性和反射

以下是一个示例,展示了如何定义和使用特性,并使用反射来获取特性信息: using System; using System.Reflection; // 自定义特性类 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] class MyCustomAttribute : Attribute { public string Description { get; } public MyCustomAttribute(string description) { Description = description; } } // 带有特性的类 [MyCustom("This is a class attribute")] class MyClass { [MyCustom("This is a method attribute")] public void MyMethod() { Console.WriteLine("Hello from MyMethod"); } } class Program { static void Main() { // 使用反射获取类的特性 Type classType = typeof(MyClass); MyCustomAttribute[] classAttributes = (MyCustomAttribute[])classType.GetCustomAttributes(typeof(MyCustomAttribute), true); foreach (MyCustomAttribute attribute in classAttributes) { Console.WriteLine("Class Attribute: " + attribute.Description); } // 使用反射获取方法的特性 MethodInfo methodInfo = classType.GetMethod("MyMethod"); MyCustomAttribute[] methodAttributes = (MyCustomAttribute[])methodInfo.GetCustomAttributes(typeof(MyCustomAttribute), true); foreach (MyCustomAttribute attribute in methodAttributes) { Console.WriteLine("Method Attribute: " + attribute.Description); } // 等待用户输入,防止控制台窗口关闭 Console.ReadLine(); } } 在这个示例中,我们首先定义了一个自定义特性类 MyCustomAttribute,它继承自 Attribute。然后,我们在 MyClass 类和 MyMethod 方法上应用了 MyCustomAttribute 特性,并传入了相应的描述信息。 ...

2025年7月23日 · 1 分钟 · 449 字

Microsoft.Jet.OLEDB.4.0驱动程序

在64位程序中运行使用Microsoft.Jet.OLEDB.4.0需要特别注意,因为Microsoft.Jet.OLEDB.4.0驱动程序是为32位环境设计的。在64位操作系统和64位应用程序中,它可能无法正常工作。 如果你需要在64位程序中使用Microsoft.Jet.OLEDB.4.0,你可以尝试以下方法: ...

2025年7月22日 · 2 分钟 · 628 字

SerialPort串口编程

从Microsoft .Net 2.0版本以后,就默认提供了System.IO.Ports.SerialPort类,用户可以非常简单地编写少量代码就完成串口的信息收发程序。 串口端口号搜索 string[] portList = System.IO.Ports.SerialPort.GetPortNames(); for (int i = 0; i < portList.Length; i++) { string name = portList[i]; comboBox.Items.Add(name); } 串口属性参数设置 SerialPort mySerialPort = new SerialPort("COM2");//端口 mySerialPort.BaudRate = 9600;//波特率 mySerialPort.Parity = Parity.None;//校验位 mySerialPort.StopBits = StopBits.One;//停止位 mySerialPort.DataBits = 8;//数据位 mySerialPort.Handshake = Handshake.Non; mySerialPort.ReadTimeout = 1500; mySerialPort.DtrEnable = true;//启用数据终端就绪信息 mySerialPort.Encoding = Encoding.UTF8; mySerialPort.ReceivedBytesThreshold = 1;//DataReceived触发前内部输入缓冲器的字节数 mySerialPort.DataReceived += new SerialDataReceivedEvenHandler(DataReceive_Method); mySerialPort.Open(); 串口发送信息 // Write a string port.Write("Hello World"); // Write a set of bytes port.Write(new byte[] { 0x0A, 0xE2, 0xFF }, 0, 3); // Close the port port.Close(); 串口接收信息 string serialReadString; private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { serialReadString = port.ReadExisting()); this.txt1.Invoke( new MethodInvoker(delegate { this.txt1.AppendText(serialReadString); })); } 循环接收数据 void com_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Use either the binary OR the string technique (but not both) // Buffer and process binary data while (com.BytesToRead > 0) bBuffer.Add((byte)com.ReadByte()); ProcessBuffer(bBuffer); // Buffer string data sBuffer += com.ReadExisting(); ProcessBuffer(sBuffer); } private void ProcessBuffer(string sBuffer) { // Look in the string for useful information // then remove the useful data from the buffer } private void ProcessBuffer(List<byte> bBuffer) { // Look in the byte array for useful information // then remove the useful data from the buffer }

2025年7月22日 · 1 分钟 · 425 字

STAThread

[STAThread] STAThread:Single Thread Apartment Thread.(单一线程单元线程) []是用来表示Attributes; [STAThread] 是一种线程模型,用在程序的入口方法上(在C#和VB.NET里是Main()方法),来指定当前线程的ApartmentState 是STA。用在其他方法上不产生影响。在aspx页面上可以使用AspCompat = “true” 来达到同样的效果。这个属性只在 Com Interop 有用,如果全部是 managed code 则无用。简单的说法:[STAThread]指示应用程序的默认线程模型是单线程单元 (STA)。启动线程模型可设置为单线程单元或多线程单元。如果未对其进行设置,则该线程不被初始化。也就是说如果你用的.NET Framework,并且没有使用COM Interop,一般不需要这个Attribute。其它的还有MTA(多线程套间)、Free Thread(自由线程)。 ...

2025年7月22日 · 2 分钟 · 537 字

.ctor&.cctor&对象的构造过程

.ctor 构造函数,在类被实例化时,它会被自动调用。 .cctor 类型初始化器,是一个静态方法,无参数无返回值,不能直接调用,最多只有一个 在继承中对象构造过程,看下面这段程序: public class A { public int x = 1; public A() { m1(); } public void m1() { } } public class B : A { public int y = 2; public static string sb = "B"; public B() { m2(); } public void m2() { } } public class C : B { public int z = 3; public static string sc = "C"; public C() { m3(); } public void m3() { } } 可以看到三者都有一个.ctor,B、C中有.cctor,而A没有,打开B,C的.cctor,可以看到它们都负责初始化自己的静态字段,现在主要来看它们的.ctor。 ...

2025年7月22日 · 1 分钟 · 379 字

csharp中的析构函数

析构函数 析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数。析构函数往往用来做“清理善后” 的工作(例如在建立对象时用new开辟了一片内存空间,应在退出前在析构函数中用delete释放)。 ...

2025年7月22日 · 2 分钟 · 749 字