我为我的公司编写了一个应用程序,该应用程序基本上将记录从文本或 CSV 文件以 100 条记录的数组发送到 Web 服务,然后它返回响应,同样以 100 条记录的数组形式将它们写入另一个文件。目前,它是单线程的(使用 Windows Pro Forms 中的 Backgroundworker 顺序处理),但我希望使用线程池和并发队列对其进行多线程处理。
#region NonDataCollectionService
if (!userService.needsAllRecords)
{
ConcurrentQueue<Record[]> outputQueue = new ConcurrentQueue<Record[]>();
while ((!inputFile.checkForEnd()) && (!backgroundWorker1.CancellationPending))
{
//Get array of (typically) 100 records from input file
Record[] serviceInputRecord = inputFile.getRecords(userService.maxRecsPerRequest);
//Queue array to be processed by threadpool, send in output concurrentqueue to be filled by threads
ThreadPool.QueueUserWorkItem(new WaitCallback(sendToService), new object[]{serviceInputRecord, outputQueue});
}
}
#endregion
void sendToService(Object stateInfo)
{
//The following block is in progress, I basically need to create copies of my class that calls the service for each thread
IWS threadService = Activator.CreateInstance(userService.GetType()) as IWS;
threadService.errorStatus = userService.errorStatus;
threadService.inputColumns = userService.inputColumns;
threadService.outputColumns = userService.outputColumns;
threadService.serviceOptions = userService.serviceOptions;
threadService.userLicense = userService.userLicense;
object[] objectArray = stateInfo as object[];
//Send input records to service
threadService.sendToService((Record[])objectArray[0]);
//The line below returns correctly
Record[] test123 = threadService.outputRecords;
ConcurrentQueue<Record[]> threadQueue = objectArray[1] as ConcurrentQueue<Record[]>;
//threadQueue has records here
threadQueue.Enqueue(test123);
}
但是,当我检查顶部块中的“outputQueue”时,它是空的,即使 threadQueue 有记录排队。这是因为我是按值传递而不是按引用传递吗?如果是这样,我将如何使用 Threadpool.QueueUserWorkItem 在语法上做到这一点?