开发者

Find out if running in WPF or console mode (c#)

Well

this is a really simple question, the search tearms are ju开发者_如何学Pythonst not that great.

How do I check in some library if I am currently running as a console application, vs. a WPF window application?

Thanks for any tips,

Chris


You can check whether the current thread is a WPF UI thread by checking Dispatcher.Current.


There's more, what if your library method is called from a worker thread? You didn't tell why you need to know, preventing a good answer. One approach is that the app that uses your library never has any trouble knowing whether its console or WPF. Expose a property to allow it to tell you. Another is using events so the app can simply implement the event handler to its liking. Dependency Injection is another.


You can check if the executed statements are running in a WPF host with the following statement:

if (System.Windows.Application.Current != null)
{
//statements for WPF mode
}
else
{
//statements for non WPF mode...
}  

For this you must reference PresentationFramework.dll


ILDasm will have an entry in the manifest as follows :

.subsystem 0x0003       // WINDOWS_CUI

.subsystem 0x0002       // WINDOWS_GUI

based on the subsystemtype you can tell if its GUI or CUI.

This information is also available from the following command :

dumpbin ConsoleApplication1.exe /headers

From your library query for entry assembly and get its full path(Assembly.GetEntryAssembly().CodeBase) and then you can issue any of these command to know the subsystem.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜