【发布时间】:2009-01-30 16:13:17
【问题描述】:
我有一个 winform 应用程序,它创建 5 个线程以连接到数据库并以非常慢的连接(某些查询需要 90 秒)从数据库中检索信息。每个线程都有自己的类实例,用于执行 sql 查询。当查询的数据被检索到时,主线程会收到运行查询的类触发的事件的通知。接收到事件后,主线程的各个组件都会更新,例如显示项目或只是保存数据以供以后使用的数据表。根据查询的信息类型,查询会以不同的间隔重复。
一切都很好......但我不开心。我觉得应该换一种方式,但我不确定是哪种方式。
以下是我目前如何设置每个线程:
string ConnectionString = @"Data Source=mySrv;Initial Catalog=myTbl;Connect Timeout=30;UID=sa;pwd=mypwd";
//thread #1
SQL_Requests ReasonRequests;
Thread Reason_Thread;
ReasonRequests = new SQL_Requests();
ReasonRequests.ProcessFinished += new SQL_Requests.ProcessFinished(ReasonRequests_Completed);
Reason_Thread = new Thread(ReasonRequests.ExecuteQuery);
ReasonRequests.DBQueryString = "select * from ReasonTable where staralingment = goodalignment"
ReasonRequests.DBConnection = ConnectionString;
//thread #2
SQL_Requests EventRequests;
Thread Event_Thread;
EventRequests = new SQL_Requests();
EventRequests.ProcessFinished += new SQL_Requests.ProcessFinished(EventRequests_Completed);
Event_Thread= new Thread(EventRequests.ExecuteQuery);
EventRequests.DBQueryString = "select * from EventTable where somefield = somevalue"
EventRequests.DBConnection = ConnectionString;
每个 Thread.start 的间隔不同。
有什么建议吗?
【问题讨论】:
标签: c# multithreading