logo
down
shadow

C# How to detect kill event sender


C# How to detect kill event sender

Content Index :

C# How to detect kill event sender
Tag : chash , By : JoeKaras
Date : November 24 2020, 01:01 AM

I hope this helps . I have a FormClosing method for my program to ask the user if he want to exit the program before closing the form. This works great, but I don't want the dialog to show up when the program closes because of a system shutdown / logout. How can I detect that the kill command is sent by the system and not by the user clicking the x of my form? Envrionment.HasShutdownStarted does not work. , Try checking e.CloseReason, e.g.
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.WindowsShutDown)
    {
        if (MessageBox.Show(...) == DialogResult.No)
        {
            e.Cancel = true;
            this.Activate();
        }
    }   
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How to detect a Kill Process event


Tag : chash , By : robinator
Date : March 29 2020, 07:55 AM
seems to work fine if it's a winform, you can capture the event FormClosing and check the CloseReason:
None
WindowsShutDown
MdiFormClosing
UserClosing
TaskManagerClosing
FormOwnerClosing
ApplicationExitCall

How to detect a kill process event, a computer's shut down and computer's crash


Tag : cpp , By : Carter
Date : March 29 2020, 07:55 AM
should help you out You may handle WM_QUIT as suggested above. There is also WM_QUERYENDSESSION and its family.
For service that without GUI, use RegisterServiceCtrlHandlerEx to get notified.

Binding an event to a non object sender Event Handling


Tag : chash , By : Big Ant
Date : March 29 2020, 07:55 AM
Hope this helps Your EventHandler's first parameter should be of type object then use one of the following ways:
protected void AddEvent(object sender, EventArgs e)
{
    //Cast to LinkButton
    var linkButton = (LinkButton)sender;
    //Or use is keyword like below
    if (sender is LinkButton)
    {

    }
    //Or use as keyword
    LinkButton linkButton = sender as LinkButton;
    if (linkButton != null)
    {

    }
}

Casting the sender of an event to find the value in a Dictionary that fired the event


Tag : chash , By : user178709
Date : March 29 2020, 07:55 AM
should help you out It does indeed work as it is. One improvement could be wrapping the PropertyChanged event information in a new custom event and a new EventArgs of your own and have the Name and the opcVariable instance part of that event. This way you avoid any casting that can lead to runtime error.
Here is an example of the EventArgs:
public class YourEventArgs : EventArgs
{
    public string Name {get;set;}

    public int OpcVariableVtqValue  {get;set;}

    // If you still need it.
    public string PropertyName {get;set;}
}
public interface IAlsVariable
{
    event EventHandler<YourEventArgs> YourEvent;
}
public class SomeAlsVariableImpl : IAlsVariable
{
    public string Name {get;}

    public event EventHandler<YourEventArgs> YourEvent;

    //  Call this method any time you need to raise the event.
    private void RaiseYourEvent(int opcVariableVtqValue)
    {
        YourEvent?.Invoke(this, new YourEventArgs 
        {
             Name = Name,
             OpcVariableVtqValue = opcVariableVtqValue,
        }
    }
}  
        foreach (var clusterManager in clusterManagers)
        {
            var clusterName = clusterManager.Name;
            string opcDataSourceServer = "server/" + clusterName + ":[2:http://www.alstom.com/Transport/Iconis/S2K/Data]<Organizes>2:S2KServer<Organizes>2:S2KTerritoryMngt<Organizes>2:TASSObject<HasComponent>2:OperatorAudibleSev";
            var opcVariable = m_alsApplication.Database.GetVariable(opcDataSourceServer, null);
            opcVariable.YourEvent += OpcVariable_YourEvent;
            m_operatorAudibleSevServerVariables.Add(clusterManager.Name, opcVariable);                
        }
private void OpcVariable_YourEvent(object sender, YourEventArgs e)
{
    UpdateAlarmSound(e.Name, e.OpcVariableVtqValue));            
}

How to implement event sender - event receiver game objects in unity?


Tag : chash , By : wpoch
Date : March 29 2020, 07:55 AM
I wish this helpful for you I have class "A", which will send event "a". Classes that are subscribing to event "a" will react to this event. Other classes can subscribe to event "a" without changing anything in class "A"; , There are probably many ways to do this.
Public static List
public class A : MonoBehaviour
{
    public static List<A> AvailableAs = new List<A>();

    private void Awake()
    {
        if(!AvailableAs.Contains(this)) AvailableAs.Add(this);
    }

    private void OnDestroy()
    {
        if(AvailableAs.Contains(this)) AvailableAs.Remove(this);
    }

    public void SomePublicMethod()
    {
        // magic
    }
}
public class B : MonoBehaviour
{
    // run the method on all currently registered A instances
    public void DoIt()
    {
        foreach(var a in A.AvailableAs)
        {
            a.SomePublicMethod();
        }
    }
}
namespace ANamespace
{
    public static class AEventHandler
    {
        internal static event Action OnInvokeA;

        public static void InvokeAEvent()
        {
            if(OnInvokeA != null) OnInvokeA.Invoke();
        }
    }
}
namespace ANamespace
{
    public class A : MonoBehaviour { 

        private void Awake()
        {
            // it is save to remove a callback first even 
            // if it was not added yet. This makes sure it is
            // added only once always
            AEventHandler.OnIvokeA -= SomePrivateMethod;
            AEventHandler.OnIvokeA += SomePrivateMethod;
        }

        private void OnDestroy()
        {
            AEventHandler.OnIvokeA -= SomePrivateMethod;
        }

        private void SomePrivateMethod()
        {
            // magic
        }

    }  
}
// invoke the event and whoever is added as listener will do 
// whatever he registered
// in this case all A instances execute their method
AEventHandler.InvokeAEvent();
public class A : MonoBehaviour
{
    public UnityEvent OnSomethingHappened = new UnityEvent();

    private void SomeMethodIWillRun()
    {
        //...
        OnSomethingHappened.Invoke();
    }
}
public class B : MonoBehaviour
{
    public A referenceToA;

    private void Start()
    {
        referenceToA.OnSomethingHappened.AddCallback(OnASomethingHappened);
    }

    private void OnDestroy()
    {
        referenceToA.OnSomethingHappened.RemoveCallback(OnASomethingHappened)
    }

    private void OnASomethingHappened()
    {
        //
    }
}
Related Posts Related QUESTIONS :
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • "The attribute names could not be inferred from bind attribute 'bind-value'" exception in Blazor
  • How to fix ''System.ArgumentException" in c#?
  • C#. Ref returning delegate for ref extension method
  • Swashbuckle Swagger generate an actual guid
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com