Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / WinForms / Managed / System / WinForms / ApplicationContext.cs / 1 / ApplicationContext.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Windows.Forms { using System; using System.Diagnostics; using System.ComponentModel; ////// /// ApplicationContext provides contextual information about an application /// thread. Specifically this allows an application author to redifine what /// circurmstances cause a message loop to exit. By default the application /// context listens to the close event on the mainForm, then exits the /// thread's message loop. /// public class ApplicationContext : IDisposable { Form mainForm; object userData; ////// /// Creates a new ApplicationContext with no mainForm. /// public ApplicationContext() : this(null) { } ////// /// Creates a new ApplicationContext with the specified mainForm. /// If OnMainFormClosed is not overriden, the thread's message /// loop will be terminated when mainForm is closed. /// public ApplicationContext(Form mainForm) { this.MainForm = mainForm; } ///~ApplicationContext() { Dispose(false); } /// /// /// Determines the mainForm for this context. This may be changed /// at anytime. /// If OnMainFormClosed is not overriden, the thread's message /// loop will be terminated when mainForm is closed. /// public Form MainForm { get { return mainForm; } set { EventHandler onClose = new EventHandler(OnMainFormDestroy); if (mainForm != null) { mainForm.HandleDestroyed -= onClose; } mainForm = value; if (mainForm != null) { mainForm.HandleDestroyed += onClose; } } } ///[ SRCategory(SR.CatData), Localizable(false), Bindable(true), SRDescription(SR.ControlTagDescr), DefaultValue(null), TypeConverter(typeof(StringConverter)), ] public object Tag { get { return userData; } set { userData = value; } } /// /// /// Is raised when the thread's message loop should be terminated. /// This is raised by calling ExitThread. /// public event EventHandler ThreadExit; ////// /// Disposes the context. This should dispose the mainForm. This is /// called immediately after the thread's message loop is terminated. /// Application will dispose all forms on this thread by default. /// public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ///protected virtual void Dispose(bool disposing) { if (disposing) { if (mainForm != null) { if (!mainForm.IsDisposed) { mainForm.Dispose(); } mainForm = null; } } } /// /// /// Causes the thread's message loop to be terminated. This /// will call ExitThreadCore. /// public void ExitThread() { ExitThreadCore(); } ////// /// Causes the thread's message loop to be terminated. /// protected virtual void ExitThreadCore() { if (ThreadExit != null) { ThreadExit(this, EventArgs.Empty); } } ////// /// Called when the mainForm is closed. The default implementation /// of this will call ExitThreadCore. /// protected virtual void OnMainFormClosed(object sender, EventArgs e) { ExitThreadCore(); } ////// Called when the mainForm is closed. The default implementation /// of this will call ExitThreadCore. /// private void OnMainFormDestroy(object sender, EventArgs e) { Form form = (Form)sender; if (!form.RecreatingHandle) { form.HandleDestroyed -= new EventHandler(OnMainFormDestroy); OnMainFormClosed(sender, e); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DivideByZeroException.cs
- PathFigure.cs
- DesignerAttribute.cs
- TextBox.cs
- DesignerActionVerbItem.cs
- PixelFormatConverter.cs
- RestClientProxyHandler.cs
- storagemappingitemcollection.viewdictionary.cs
- RightsManagementInformation.cs
- NetworkAddressChange.cs
- ToolboxItemAttribute.cs
- SimpleExpression.cs
- TypeConverterAttribute.cs
- HandleRef.cs
- cryptoapiTransform.cs
- RoleGroup.cs
- ListDataBindEventArgs.cs
- FlowDocumentPage.cs
- DetailsViewDeleteEventArgs.cs
- DataShape.cs
- StrongName.cs
- DataGridViewDataConnection.cs
- BaseServiceProvider.cs
- FloatMinMaxAggregationOperator.cs
- System.Data.OracleClient_BID.cs
- ResourceAssociationTypeEnd.cs
- StrokeDescriptor.cs
- BasicExpressionVisitor.cs
- FragmentQueryProcessor.cs
- Tuple.cs
- TypeViewSchema.cs
- DataObjectAttribute.cs
- PropertyGroupDescription.cs
- PermissionListSet.cs
- TreeNodeCollection.cs
- TextRenderer.cs
- XmlEncoding.cs
- ListViewGroupCollectionEditor.cs
- RightsManagementUser.cs
- QilLiteral.cs
- Library.cs
- LazyTextWriterCreator.cs
- MetadataPropertyvalue.cs
- HtmlInputImage.cs
- JsonEnumDataContract.cs
- SqlDataAdapter.cs
- TrackingValidationObjectDictionary.cs
- EpmAttributeNameBuilder.cs
- EntityClassGenerator.cs
- RSACryptoServiceProvider.cs
- ValidatedControlConverter.cs
- RewritingProcessor.cs
- ResourceDisplayNameAttribute.cs
- AsyncCompletedEventArgs.cs
- WebServiceClientProxyGenerator.cs
- SecurityDescriptor.cs
- EndpointAddressProcessor.cs
- NativeCppClassAttribute.cs
- ContentPropertyAttribute.cs
- ScrollableControl.cs
- TextElementEnumerator.cs
- QueryOperationResponseOfT.cs
- Rss20ItemFormatter.cs
- OleDbException.cs
- ExportOptions.cs
- WebHttpBindingElement.cs
- SettingsAttributeDictionary.cs
- ContractMethodInfo.cs
- ProfileModule.cs
- SchemaContext.cs
- StrokeNodeOperations2.cs
- ObjectDataSourceDisposingEventArgs.cs
- httpstaticobjectscollection.cs
- StylusPoint.cs
- RootBrowserWindowProxy.cs
- EncodingTable.cs
- ObjectListTitleAttribute.cs
- TextRenderer.cs
- XamlValidatingReader.cs
- SynthesizerStateChangedEventArgs.cs
- StaticFileHandler.cs
- EncodingNLS.cs
- UIElement3DAutomationPeer.cs
- SupportingTokenAuthenticatorSpecification.cs
- Exceptions.cs
- DataBinding.cs
- DataListItemEventArgs.cs
- Crypto.cs
- DecoderBestFitFallback.cs
- ProtocolsConfigurationEntry.cs
- OrderedDictionary.cs
- SoapMessage.cs
- FontFaceLayoutInfo.cs
- TextElementEditingBehaviorAttribute.cs
- ImageListDesigner.cs
- SparseMemoryStream.cs
- LazyTextWriterCreator.cs
- XmlIncludeAttribute.cs
- LinqDataSourceDisposeEventArgs.cs
- BitFlagsGenerator.cs