Microsoft Office Excel instance does not close.
Hi,
I found an issue, when an user quit an Excel instance created with Microsoft Office Interop library. The process does not exit properly, and i can't manange this issue with the calling application.
To set the context : The main application is an Addin for Office (Excel, Word and PowerPoint) and one action launches a new instance of Excel where the user can see and do some actions. And that's with this new instance that we encountered the issue.
Moreover the Microsoft.Office.Interop.Excel.Application does not have event handlers to manage "Quit" or "Close" events of the application, so i had to found another way to kill my process =)
So here is the code used to create a new Excel instance :
1: Microsoft.Office.Interop.Excel.Application xlApp = null;
2:
3: // Launch New Excel instance
4: xlApp = new Microsoft.Office.Interop.Excel.Application();
5:
6: if (xlApp == null)
7: throw new Exception("EXCEL could not be started. Check that your office installation and project references are correct.");
8:
9: xlApp.Visible = true;
10: xlApp.UserControl = true;
11: System.Threading.Thread.CurrentThread.CurrentCulture =
12: System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
13:
14: // Open the file
15: Workbook wb = xlApp.Workbooks.Open(filepath, Type.Missing,
16: Type.Missing, Type.Missing, Type.Missing,
17: Type.Missing, Type.Missing, Type.Missing,
18: Type.Missing, Type.Missing, Type.Missing,
19: Type.Missing, Type.Missing, Type.Missing,
20: Type.Missing);
21:
22: Worksheet ws = (Worksheet)wb.Worksheets[1];
23:
24: if (ws == null)
25: throw new Exception(
26: "Worksheet could not be created. Check that your office installation and project references are correct.");
And here the class used to manage the quit process :
1: public class SubclassHWND : NativeWindow
2: {
3: protected override void WndProc(ref Message m)
4: {
5: if (m.Msg == 0x10)
6: {
7: Process.GetCurrentProcess().Kill();
8:
9: // Confirmation message if needed
10: //if (MessageBox.Show("Would you like to quit?", "Close?", MessageBoxButtons.YesNo) == DialogResult.Yes)
11: // Process.GetCurrentProcess().Kill();
12: //else
13: // return;
14: }
15: base.WndProc(ref m);
16: }
17: }
And the code to attach the application handler :
1: if (OfficeApplicationManager.OfficeApplication != null)
2: {
3: s = new SubclassHWND();
4: s.AssignHandle(new IntPtr(OfficeApplicationManager.OfficeApplication.GetApplicationHwnd()));
5: }
The OfficeAppliaction is the same as Microsoft.Office.Interop.Excel.Application
FluentNHibernarte.Search 0.3 Beta Released Now Support Fluent XML-Less Configuration
Just released FluentNHibernate.Search 0.3 Beta which now support Fluent XML-Less Configuration available on CodePlex.
No need to configure NHibernate.Search in web.config or app.config, you can know configure it like FluentNHibernate do for NHibernate.
1: Configuration nhcfg = FluentSearch.Configure()
2: .DefaultAnalyzer().Standard()
3: .DirectoryProvider().FSDirectory()
4: .IndexBase("~/Index")
5: .IndexingStrategy().Event()
6: .MappingClass<SearchMappingImpl>()
7: .BuildConfiguration();
FHNS support both builtin Analyzers and Directory Providers :
Builtin Analyzers :
- StandardAnalyzer
- KeywordAnalyzer
- SimpleAnalyzer
- StopAnalyzer
- WhitespaceAnalyzer
1: // Some Builtin Analyzers
2: FluentSearch.Configure().DefaultAnalyzer().Standard()
3: FluentSearch.Configure().DefaultAnalyzer().Simple()
4: FluentSearch.Configure().DefaultAnalyzer().Whitespace()
5: ...
6:
7: // Custom Analyzers
8: FluentSearch.Configure().DefaultAnalyzer().Custom<MyAnalyzer>()
Builtin Directory Providers :
- FSDirectoryProvider
- RAMDirectoryProvider
1: // Builtin Directory Providers
2: FluentSearch.Configure().DirectoryProvider().FSDirectory()
3: FluentSearch.Configure().DirectoryProvider().RAMDirectory()
4: ...
5:
6: // Custom Directory Providers
7: FluentSearch.Configure().DirectoryProvider().Custom<MyDirectoryProvider>()
Working with FluentNHibernate for NHibernate Configuration ?
You can get FluentNHibernate.Search and FluentNHibernate in touch like this :
1: // FluentNHibernate.Search Configuration
2: var nhcfg = FluentSearch.Configure()
3: .DefaultAnalyzer().Standard()
4: .DirectoryProvider().FSDirectory()
5: .IndexBase("~/Index")
6: .IndexingStrategy().Event()
7: .MappingClass<SearchMappingImpl>()
8: .BuildConfiguration();
9:
10: // FluentNHibernate Configuration
11: var fcfg = Fluently.Configure(nhcfg);
12:
13: var sessionFactory = fcfg.Database(MsSqlConfiguration.MsSql2008.ConnectionString(_settings.ConnectionString)
14: .Cache(p => p.UseQueryCache().ProviderClass<SysCacheProvider>())
15: .Dialect<MsSql2008Dialect>().UseReflectionOptimizer()
16: .AdoNetBatchSize(50)
17: .ProxyFactoryFactory<ProxyFactoryFactory>())
18: .BuildSessionFactory();
19:
Beta 0.3 is available on CodePlex : http://fnhsearch.codeplex.com/
Have fun :)
FluentNHibernarte.Search 0.2 Beta Released
I’ve just released FluentNHibernate.Search 0.2 Beta which is available on CodePlex and support the following new features.
Field Mapping without specifying "Name"
Actually we needed to specify Field name using .Field(“FieldName”) :
1: public class BookSearchMap : DocumentMap<Book>
2: {
3: public BookSearchMap()
4: {
5: Id(p => p.BookId).Field("BookId").Bridge().Guid();
6: }
7: }
Now if we don’t specify this, the field name will be automaticaly taken from the mapped property name.
1: public class BookSearchMap : DocumentMap<Book>
2: {
3: public BookSearchMap()
4: {
5: Id(p => p.BookId).Bridge().Guid();
6: }
7: }
Id Mapping without specifiying "Field"
Actually to we needed to specify Field name using .Name(“FieldName”) for Field mapping, now we can forget this, it will be taken directly from the mapping property name.
However we can overrides that name using .Name(“FieldName”) method.
1: public class BookSearchMap : DocumentMap<Book>
2: {
3: public BookSearchMap()
4: {
5: Id(p => p.BookId).Bridge().Guid();
6: Name("Book");
7:
8: Map(x => x.Title)
9: .Store().Yes()
10: .Index().Tokenized();
11: Map(x => x.Description)
12: .Store().Yes()
13: .Index().Tokenized();
14: }
15: }
Embedded Mapping
FluentNHibernate.Search now support embedded mapping using the Embedded method in DocumentMap.
1: public class AuthorSearchMap : DocumentMap<Author>
2: {
3: public AuthorSearchMap()
4: {
5: Id(p => p.AuthorId)Bridge().Guid();
6: Name("Author");
7:
8: Map(x => x.Name)
9: .Store().Yes()
10: .Index().Tokenized();
11:
12: Embedded(x => x.Books).AsCollection();
13: }
14: }
Introducing FluentNhibernate.Search Mapping Interface
Coming back for a long time…
I announce today a new project i’ve hosted on CodePlex about NHibernate.Search, a Lucene.NET provider implementation for NHibernate which is a part of the NHibernate Contrib project.
Actually NHibernate.Search is attributes based mapping.
1: [Indexed]
2: public class Book
3: {
4: [DocumentId]
5: public Guid BookId { get; set; }
6:
7: [Field(Index=Index.Tokenized, Name="Title", Store=Store.Yes)]
8: public string Title { get; set; }
9:
10: [Field(Index = Index.Tokenized, Name = "Description", Store = Store.Yes)]
11: public string Description { get; set; }
12: }
In my recent projects I needed to keep my domain entites true POCO and persistant ignorant.
Since there was no Fluent inteface to map Lucene.NET documents without attributes outside domain entities, I decided to start this project which I hope should make us feel better :)
FluentNHibernate.Search mapping is quite equivalent to FluentNHibernate for NHibernate XML-Less entity mapping initialy developped by Jeremy Miller.
Mapping Lucene.NET documents are now very simple, attribute-less, here is a sample :
1: public class Book
2: {
3: public Guid BookId { get; set; }
4: public string Title { get; set; }
5: public string Description { get; set; }
6: }
Woow, that’s clean :)
And here is the mapping class :
1: public class BookSearchMap : DocumentMap<Book>
2: {
3: public BookSearchMap()
4: {
5: Id(p => p.BookId).Field("BookId").Bridge().Guid();
6: Name("Book");
7: Boost(500);
8: Analyzer<StandardAnalyzer>();
9:
10: Map(x => x.Title)
11: .Analyzer<StandardAnalyzer>()
12: .Boost(500);
13:
14: Map(x => x.Description)
15: .Boost(500)
16: .Name("Description")
17: .Store().Yes()
18: .Index().Tokenized();
19: }
20: }
Pretty nice isn’t it ? :)
FluentNHibernate.Search is still in developpement but I hope I will release a stable version soon.
Until a stable version be released you can check the current source code at CodePlex and give feedbacks !