c# - Different instancens in a thread, or threadpool? -
i have project starts central logic class (which uses other .dll's check on hardware or connect database). after that, wpf form started. form uses information of central logic.
currently, application being started this:
public void startthewholebunch() { thread thread = new thread(() => { applicationlogic = new applicationlogic(); application app = new application(); app.run(new mainwindow(applicationlogic)); }); thread.isbackground = true; thread.setapartmentstate(apartmentstate.sta); thread.start(); thread.join(); } the mainwindow 1 of 2 wpf applications want use. second 1 join in fun o later stage.
the current setup working. communicates each other , stuff, no problems here. wondering if use of thread correct. when leave applicationlogic = new applicationlogic(); out of thread, things bound go wrong (for example creating messagebox popups, whole application freeze here).
should keep in 1 thread here? or better practice split and/or create threadpool? how can approach best way?
the applicationlogic supposed run indefinitely.
i think you're mixing need globally existent class instance , threads. don't need separate thread this, need applicationlogic singleton.
public class applicationlogic { private static applicationlogic _instance = new applicationlogic(); public static applicationlogic instance { { return _instance; } } private applicationlogic() { } } further, performing immediate thread.join();, you're making thread moot point. don't need thread, start main form. , if wanted load form, it, create new instance , show it:
var otherform = new otherform(); otherform.show(); and making applicationlogic singleton, doing other thread (kind of), can access this:
applicationlogic.instance.dosomething();
Comments
Post a Comment