我有一个表单,我建立了与Windows Servie的连接(我的项目中的服务参考)。然后,当用户按下按钮时,我从服务中获取最新数据。由于数据的检索需要一段时间,我正在考虑使用后台工作程序,因此我不会阻止UI。问题是:我可以使用已在表单加载时建立的连接吗?或者这会崩溃我的app / require调用?
class SomeWindowsForm
{
public ServiceClient client
onLoad()
{
client = new ServiceClient("Address"); //this is a Service Reference in my Project
client.Connect();
BackgroundWorker bw = new BackgroundWorker(do_Work);
bw.DoWork += new DoWorkEventHandler(finished_Work);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler();
}
timer_tick()
{
client.SendPing(); // this keeps the connection
}
buttonClick()
{
bw.RunWorkerAsync();// can I pass the client here to worker???
}
do_Work()
{
var log = client.getData(); // Is this allowed, I am callling the client which has been created in the form??
return log;
}
finished_Work()
{
textBox.Text = e.Argument;
}
}
答案 0 :(得分:0)
通常,BackgroundWorker的结果是这样分配的:
namespace Shape {
class Rectangle {
public:
friend std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
};
std::ostream &operator<<(std::ostream &os, const Rectangle &rectangle);
}