我的应用程序中使用SimpleInjector我的工作,在那里我有类似以下的东西:简单的注射器 - 访问来自后台线程注入的情况下
public class Foo : IFoo
{
private readonly Bar _bar;
public Foo(Bar bar)
{
_bar = bar;
}
public void DoSomething()
{
IEnumberable<Order> orders = _bar.Orders;
}
}
我的行为是Foo.DoSomething
被称为从后台线程(Task.Run)和Bar
注册在应用程序的主要方法(Windows窗体应用程序)与单身生活方式。我所关心的是如果Bar
提供给Foo
的实现不是线程安全的。
我的主要问题是Bar
的状态是Foo
所需要的,这个状态是在调用Foo.DoSomething
之前由主线程设置的。我已经四处寻找解决方案,我面临的这种情况,但我无法找到一个帮助(除非我只是走错了方向)。
我看过this页面上的建议,在后台线程上执行实例时使用装饰器。然而,这并没有帮助,因为Bar
的状态设置在不同的线程(主线程)上,并且使用装饰器将只创建一个没有状态的新实例Bar
。
所以我想我的问题是我只需要注册酒吧作为一个单身人士,并确保注册的实现是线程安全的,或者是有明显的解决方案,这个问题,正在盯着我在脸上但我似乎无法看到?
希望我提供的信息是足够的。如果您需要更多信息,请告诉我。
感谢
更新 Bar
只是包含信息的列表类在整个应用程序的需要。例如:
public class Bar: IBar
{
// Not using orders or products, but just for purpose of the example
// These are initialized early on in the App process because early
// steps of the app (which are on the main thread) need them.
public IEnumerable<Order> Orders { get; private set; }
public IEnumerable<Product> Products { get; private set; }
}
以下是我使用美孚形式应用:
public partial class App: Form
{
private readonly IFoo _foo;
public App(IFoo foo)
{
InitializeComponent();
_foo = foo;
}
public btn1_Click()
{
// This is just for the purpose of showing that the data inside Bar
// is loaded on the main thread before Foo.DoSomething is run. In
// the real app the Bar data is loaded at previous steps of the app
// (the app is a wizard like app).
LoadBarData();
Task.Run(() =>
{
_foo.DoSomething();
});
// The issue would be if for example Bar is changed here while the
// background thread is running. In my app it doesn't really change
// here, but I want to make sure no issues arise in all scenarios,
// whether it changes or not.
}
}
最后这里是我的主要方法:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (Container container = new Container())
{
container.Register<IBar, Bar>(Lifestyle.Singleton);
container.Register<IFoo, Foo>(Lifestyle.Singleton);
container.Register<App>();
}
Application.Run(container.GetInstance<App>());
}
请显示Bar和Main的相关代码。 – Steven
@Steven我编辑了这个问题以包含更多信息。希望能帮助到你。谢谢 –