I have a windows form, on which I display data from a local database.
I'd like to also connect to a remote database, and display some other data from there.. however, this remote database could be down, or slow.
I don't want the UI to freeze while I try to connect to this remote database.
So knowing nothing about threading or thread-safety, here's my hamfisted example:
RemoteDataContext rdt;
private void GetRemoteDataContext() {
rdt = new RemoteDataContext(RemoteServerConnectionString);
}
private void FillFromRemoteDataContext() {
lblTest.text = rdt.TestTable.First().TestField;
}
private void Form1_Shown(object sender, EventArgs e) {
Thread t = new Thread(new ThreadStart(new delegate {
try {
GetRemoteDataContext();
FillFromRemoteDataContext();
} catch { } // ignore connection errors, just don't display data
);
t.Start;
}
So you should be able to tell from that what it is that I want to acheive.
My question is, what is the correct way to go about it?
Update: Thanks guys, now I have (in Form1Shown
):
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler((sender, e) => {
e.Result = null;
try {
e.Result = new RemoteDataContext(RemoteServerConnectionString);
} catch { } // ignore connection errors, just don't display data
});
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler((sender, e) => {
if (e.Result != null) {
rdt = (RemoteDataContext)e.Result;
FillFromRemoteDataContext();
}
});
bw.RunWorkerAsync();
And it works, and as far as I understand it, there should be no threading related errors.