\u003ccode\u003eusing System.Threading.Tasks;\n\n// ...\n\n// 创建一个新的线程来执行代码\nTask.Run(() =>\n{\n // 在新线程中执行的代码\n string filePath = localPath + "\gpib.dat";\n rtbLog.LoadFile(filePath, RichTextBoxStreamType.PlainText);\n});\n\u003c/code\u003e\n这样,rtbLog.LoadFile方法将在新线程中执行,而不会阻塞主线程。请注意,如果您需要在新线程中更新UI元素(如RichTextBox),您可能需要使用Control.InvokeControl.BeginInvoke方法来在主线程上执行更新操作。

在 C# 中使用 Task 类将代码放到线程中执行

原文地址: https://www.cveoy.top/t/topic/pZoY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录