gpt4 book ai didi

c# - 类设计——如何管理多线程响应

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:35 33 4
gpt4 key购买 nike

我需要一些帮助,了解如何最好地设计一组类来管理我正在开发的多线程 C# 应用程序。我已经成功地创建了一个启动一系列线程的 ThreadContainer 类。Threads 从特定网站下载财务数据。对于每个通过的网站,他们将打开相同的基本 url,但使用不同的符号。所以有 # 个结果等于每个网站传递的符号数。然后根据请求的网站,线程结果必须以不同的方式处理......这是简化的 ThreadContainer 代码:

public class ThreadContainer
{
private int ConcurrentThreads;
private string Website;
public string URL;
private Queue SymQueue;

//Constructor
public ThreadContainer(string website, Queue symQueue)
{
Website = website;
SymQueue = symQueue;
}

//Start
public void start(int concurrent)
{
ConcurrentThreads = concurrent;

//Start the Concurrent Threads
for (int ThreadNum = 1; ThreadNum <= ConcurrentThreads; ThreadNum++)
{
//Get a symbol from the queue
Sym = SymQueue.Dequeue().ToString();

//Build the URL
URL = string.Format(Constants.URL(Website), Sym);

//Create a new Asynch thread
AsynchThread j = new AsynchThread(ThreadNum);

//Start the AsyncThread class with the BackgoundWorker
j.Start(Sym, URL);
}
}

//Method called when the Backgroundworker thread has terminated
private void AsynchThread1_JobCompleted(object sender, EventArgs e)
{
// Get the AsynchThread object
AsynchThread j = (AsynchThread)sender;

//Get the symbol name
String Sym = j.Sym;

//Get the result with the Webpage content
RunWorkerCompletedEventArgs re = e as RunWorkerCompletedEventArgs;
String result = re.Result.ToString();

/* HERE EACH THREAD RETURNS THE WEBSITE CONTENT.
* DEPENDING ON THE WEBSITE THAT WAS REQUESTED SEVERAL ACTIONS MAY BE EXECUTED.
*/
switch(website)
{
case "WebsiteA":
// With WebsiteA I would like to :
// 1. extract the symbol data
// 2. return the result for this symbol to the calling class.

case "WebsiteB":
// With WebsiteB I would like to:
// 1. extract the symbol data (Webpage design is different from WebsiteA)
// 2. store each symbol data in a database table

case "WebsiteC":
// With WebsiteB I would like to:
// 1. extract the symbol data (Webpage design is different from WebsiteA and WebsiteB)
// 2. put the results in a queue

case ...

default:
return "";
}

// Reuse the thread that has just completed
//If there are items left in the queue...
if (SymQueue.Count > 0)
//...get the new Sym value...
NewSym = SymQueue.Dequeue().ToString();

//If NewSym is valid ...
if (!String.IsNullOrEmpty(NewSym))
{
//...build the URL...
String NewURL = string.Format(Constants.URL(Website), NewSym);

//...then restart the thread with the new parameters
j.Start(NewSym, NewURL);
}
}
}

我想知道设计AsynchThread1_JobCompleted 的最佳方法是什么方法。当每个线程终止并应执行以下任务时调用此方法:

  1. 获取网页内容
  2. 从网页中提取数据
  3. 将数据发送回调用者或​​将数据保存到数据库或将数据插入队列。
  4. 检查队列是否还有项目并使用新 URL 重新启动线程,以便打开新网页。

我该如何设计方法,才不必每次需要添加新网站时都修改代码?另外,我应该使用下载页面的同一个 AsynchThread 来提取数据,保存到数据库等......还是另一个线程更好?我想让 ThreadContainer 类遵循一种单一职责原则...

谢谢

最佳答案

在 EventArgs 中,将委托(delegate)/操作传递给处理此站点的特定实现。

或者更好地隔离一个类站点,在其中放置 url 和方法来处理要对数据完成的特定工作。然后在线程的EventArgs中传入Site,在回调中调用该Site的具体方法。

这样您将删除 case 语句,并将代码移到它所属的位置。

关于c# - 类设计——如何管理多线程响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519547/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com