我有一个可以通过SDK(项目中引用的.DLL
文件)控制的硬件组件。
我试图实现一个辅助类来处理对象的实例化,并为每个需要它的窗口提供所需的object reference
。
这就是我在窗口后面的代码中创建对象的方法:
//private fields
SystemConnector myConn;
MyHardware mySystem; // this is the object i need a reference to
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SystemDiscoverer SystemDiscoverer = new SystemDiscoverer();
SystemDiscoverer.Discovered += (sysInfo) =>
{
myConn = new SystemConnector(sysInfo.IPAddress);
if (myConn != null)
mySystem = new MyHardware(myConn);
};
SystemDiscoverer.Discover();
}
private void WindowBase_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (myConn != null)
myConn.Dispose();
if (mySystem != null)
mySystem.Dispose();
}
我想把这个逻辑转移到帮助类中,但我有一些问题我无法找到解决方案:
答案 0 :(得分:0)
要从事件处理程序处理创建,请尝试:
LazyInitializer.EnsureInitialized(...)
它是线程安全的,所以你应该能够为你的单身人士初始化一个静态属性。
MSDN文章:http://blogs.msdn.com/b/jolud/archive/2010/04/02/initialization-of-shared-resources.aspx
一个例子,来自ASP.NET MVC 4标准项目模板。
public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute {
private static SimpleMembershipInitializer _initializer;
private static object _initializerLock = new object();
private static bool _isInitialized;
public override void OnActionExecuting(ActionExecutingContext filterContext) {
// Ensure ASP.NET Simple Membership is initialized only once per app start
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
}
private class SimpleMembershipInitializer {
public SimpleMembershipInitializer() {
Database.SetInitializer<UsersContext>(null);
try {
using(var context = new UsersContext()) {
if(!context.Database.Exists()) {
// Create the SimpleMembership database without Entity Framework migration schema
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
}
catch(Exception ex) {
throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
}
}
}
}
答案 1 :(得分:0)
我会用MyHardware
方法将async
类包装在一个包含SystemDiscoverer.Discovered
事件的public sealed class MyHardwareSingleton
{
static MyHardwareSingleton()
{ }
private MyHardwareSingleton()
{ }
private static readonly MyHardwareSingleton _myHardware = new MyHardwareSingleton();
private SystemConnector _myConn;
private MyHardware _mySystem;
public MyHardwareSingleton Instance
{
get { return _myHardware; }
}
public Task<MyHardware> GetHardwareAsync()
{
if (_mySystem != null)
{
return Task.FromResult(_mySystem);
}
var tcs = new TaskCompletionSource<MyHardware>();
SystemDiscoverer SystemDiscoverer = new SystemDiscoverer();
SystemDiscoverer.Discovered += (sysInfo) =>
{
myConn = new SystemConnector(sysInfo.IPAddress);
if (myConn != null)
{
mySystem = new MyHardware(myConn);
tcs.TrySetResult(mySystem);
return tcs.Task;
}
// This indicated that myConn came back null.
tcs.TrySetResult(null);
return tcs.Task;
};
// Make SystemDiscoverer run asynchrnously. We will await it so when it completes we will get the desired MyHardware instance.
SystemDiscoverer.DiscoverAsync();
return tcs.Task;
}
}
方法中:
SystemConnector
可以在public Task DiscoverAsync()
{
return Task.Run(() => Discover());
}
类中添加DiscoverAsync,如下所示:
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
var myHardware = await MyHardwareSingleton.Instance.DiscoverAsync();
}
然后您可以通过以下代码从代码中调用它:
{{1}}
当然,我没有处理任何异常处理/取消。这只是使用TAP
可以完成的示例