开发者

Using different .config files for separate threads (.NET/C#)

Is this possible to do?

I'm using this call to instant开发者_如何学JAVAiate new thread:

this.RunningThread = new Thread(new ParameterizedThreadStart(this.Run));

Optionally, I'd like to pass a location of a different .config to it. Is it possible, if so, how?

Thank you!


No, a .config file is associated with an AppDomain, not a thread. On the default CLR host, the primary appdomain is hard-baked to the app.exe.config file, you cannot change it. New AppDomains can be configured with their own .config file through the AppDomainSetup.ConfigurationFile property.

Beware of the effort required to serialize data from one appdomain to another, this is something you only want to do if you are actually interested in isolating code. Whatever the reason you want to do this, surely there's a better way than an AppDomain to accomplish your goal.


just pass a delegate:

private void Run(string configPath)
{

}

...
Thread thread = new Thread(()=> this.Run("foo.config")) ;
thread.Start();


You could try using the ConfigurationManager.OpenExeConfiguration method which you pass the path of the config file and returns a Configuration object.

I don't think there's any way to have ConfigurationManager.AppSettings or ConfigurationManager.ConnectionStrings return different values on different threads, but you could keep a reference to the Configuration object returned by the OpenExeConfig method and use that within your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜