在 Program.cs 中,我有以下方法正在检查以及将 5 个 SQL 数据库与中央服务器同步。每一个都是相互独立的,所以我想通过让它们同时运行来加快我的程序的加载时间。
不幸的是,一次工作非常不稳定,下一次就不行了。本地数据库是 SQLExpress 2005,中央数据库是 SQL Server Standard 2005。
其中任何一个可以有多少个连接有限制吗?后台工作者怎么样,我一次只能运行这么多吗?我确信有一种更雄辩的方式来做到这一点,我很想听听(看到)他们。
这就是我在 Program.cs 的 Main() 中调用它的方式 -->
if(IsSqlAvailable())
SyncNow();
internal static void SyncNow()
{
#region ConnectDB Merge Sync Background Thread
BackgroundWorker connectBW = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
connectBW.DoWork += new DoWorkEventHandler(connectBW_DoWork);
if (connectBW.IsBusy != true)
connectBW.RunWorkerAsync();
#endregion
#region aspnetDB Merge Sync Background Thread
BackgroundWorker aspBW = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
aspBW.DoWork += new DoWorkEventHandler(aspBW_DoWork);
if (aspBW.IsBusy != true)
aspBW.RunWorkerAsync();
#endregion
#region MatrixDB Merge Sync Background Thread
BackgroundWorker matrixBW = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
matrixBW.DoWork += new DoWorkEventHandler(matrixBW_DoWork);
if (matrixBW.IsBusy != true)
matrixBW.RunWorkerAsync();
#endregion
#region CMODB Merge Sync Background Thread
BackgroundWorker cmoBW = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
cmoBW.DoWork += new DoWorkEventHandler(cmoBW_DoWork);
if (cmoBW.IsBusy != true)
cmoBW.RunWorkerAsync();
#endregion
#region MemberCenteredPlanDB Merge Sync Background Thread
BackgroundWorker mcpBW = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
mcpBW.DoWork += new DoWorkEventHandler(mcpBW_DoWork);
if (mcpBW.IsBusy != true)
mcpBW.RunWorkerAsync();
#endregion
}
static void mcpBW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
try
{
MergeRepl mcpMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MemberCenteredPlan", "MemberCenteredPlan", "MemberCenteredPlan");
mcpMergeRepl.RunDataSync();
areAllInSync += 1;
}
catch (Exception)
{
if (worker != null) worker.CancelAsync();
}
}
static void cmoBW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
try
{
MergeRepl cmoMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CMO", "CMO", "CMO");
cmoMergeRepl.RunDataSync();
areAllInSync += 1;
}
catch (Exception)
{
if (worker != null) worker.CancelAsync();
}
}
static void connectBW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
try
{
MergeRepl connectMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "CONNECT", "Connect", "Connect");
connectMergeRepl.RunDataSync();
areAllInSync += 1;
}
catch (Exception)
{
if (worker != null) worker.CancelAsync();
}
}
static void matrixBW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
try
{
MergeRepl matrixMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "MATRIX", "MATRIX", "MATRIX");
matrixMergeRepl.RunDataSync();
areAllInSync += 1;
}
catch (Exception)
{
if (worker != null) worker.CancelAsync();
}
}
static void aspBW_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
try
{
MergeRepl aspnetdbMergeRepl = new MergeRepl(SystemInformation.ComputerName + "\\SQLEXPRESS", "WWCSTAGE", "aspnetdb", "aspnetdb", "aspnetdb");
aspnetdbMergeRepl.RunDataSync();
areAllInSync += 1;
}
catch (Exception)
{
if (worker != null) worker.CancelAsync();
}
}