BackgroundWorker is not being called
I'm trying to preload assemblies in a background thread and it seems that when the PreLoadAssemblies function is being called nothing happen and like the thread is dead or something (but I do see the thread in the threads list). When I run the code in single threaded environment, it's works fine.
Here is where I create the background call:
BackgroundWorker backgroundWorker = new BackgroundWorker();
//[Threaded]
private void InitlaizePoint()
{
backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundInitalization);
backgroundWorker.RunWorkerAsync();
}
Here is the background initialization function. It stops (meaning, nothing happen and I see my dialog window progress bar running "on empty") after I try to step-in through the PReLoadAssemblies).
private void BackgroundInitalization(object sender, DoWorkEventArgs e)
{
try
{开发者_运维知识库
PreLoadAssemblies();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
here is how preloadassemblies looks like:
private static void PreLoadAssemblies()
{
//statusController.PublishStatus("pre-loading assemblies");
var missingAssemblies = new ArrayList();
var loadedAssemblies = new ArrayList();
LoadDependencies(Assembly.GetExecutingAssembly().GetName(), missingAssemblies, loadedAssemblies);
}
private static void LoadDependencies(AssemblyName name, ArrayList missingAssemblies, ArrayList loadedAssemblies)
{
try
{
//statusController.PublishStatus("Loading dependencies");
Assembly a = Assembly.Load(name);
loadedAssemblies.Add(name.FullName);
foreach (AssemblyName depends in a.GetReferencedAssemblies())
{
if (!IsAssemblyLoaded(depends.FullName, loadedAssemblies))
LoadDependencies(depends, missingAssemblies, loadedAssemblies);
}
}
catch (Exception)
{
missingAssemblies.Add(name);
}
}
private static bool IsAssemblyLoaded(String name, ArrayList preloadedAssemblies)
{
if (preloadedAssemblies.IndexOf(name) == -1)
return false;
return true;
}
Let me know if you have any idea.
Thanks
Where exactly are you calling this method? The BackgroundWorker class needs the message pump already running, and will not work if you call it too early in your application (before Application.Run()).
If you are doing this from something like a splash screen that is run before the main application starts, unfortunately, you will need to do your own threading instead of using BackgroundWorker.
Note: As Henk Holterman points out, if you don't have a message pump at all (because you have a console app instead of Winforms or WPF), than BackgroundWorker will not work at all in your app.
We are looking at a edited version of your code. You removed the bits that cause the deadlock. Some notes:
- You mention a 'status progress bar' but your code shows no evidence for it. Getting that bar updated provides ample ways for deadlock.
- The commented calls to PublishStatus are a red flag
- There is no evidence of a RunWorkerCompleted event handler even though you'd need one to get your splash screen closed. The BGW will deadlock when your main thread is blocking on the BGW and not pumping messages
- There's a corner case for circular dependencies between assemblies. Your code will hang in an endless loop. Diagnose with Build + Clean, Build + Build.
- Remote, but several .NET assemblies as well as C++/CLI assemblies have a module initializer. It may not necessarily act properly when the assembly is loaded on a non-STA worker thread.
RunWorkerCompleted is my best guess until you show an unedited version of your code.
You cannot pre-load assemblies that easily. Take a look at my question.
精彩评论