c# - 在工作线程中修改字符串然后在主线程中读取结果的正确方法是什么?
问题描述
所以我将一个空字符串从主线程传递给工作线程,然后让工作线程修改字符串,稍后主线程需要能够读取字符串。当然,字符串是按值而不是按引用传递的,因此无法修改。我可以通过传递一个大小为 1 的字符串数组来解决这个问题,但我想知道是否有更简洁的方法来解决这个问题。
//in main thread
string errorMessage="";
System.Threading.Thread thread = new System.Threading.Thread(() => {
ProcessImport(path, errorMessage); });
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start();
//in worker thread
if(errorDetected)
{
errorMesage= "some message";
}
//later in main thread
if(errorMessage!="")
{
CreateFeedbackMessageBox(erorrMesssage, 5000);
}
解决方案
推荐阅读
- python - 将过程应用于python中数据框中的某些行
- apex - 在自治数据库中创建序列
- react-native - 有没有办法在 React Native 中处理应用程序关闭/杀死/终止?
- javascript - 如何将隐藏数组中元素的值设置为零?
- python - 如何继承 OrderedDict 以便可以像属性一样访问其项目?
- go - container_memory_working_set_bytes 与 process_resident_memory_bytes 和 total_rss 的关系
- erlang - 非零整数的“bnot”结果是否总是翻转符号?
- pandas - 数据分析 - 视频游戏数据集
- dart - Dart - 条件运算符与 If 语句之间的功能差异
- wordpress - 使用 cpt 并使用 url 传递变量