Deactivate event not firing if i run exe outside Visual Studio - c#

I have below line of code in my Windows application.
Below is the code which get trigger on click of tray icon click, and NotifyForm is normal Windows form.
ExceptionManager.Process(
() =>
{
if (notifyForm == null)
{
notifyForm = new NotifyForm();
notifyForm.Deactivate += (o, args) =>
{
notifyForm.Close();
};
notifyForm.Disposed += (o, args) =>
{
notifyForm = null;
};
}
if (!notifyForm.Visible)
{
notifyForm.ShowPopup();
}
else
{
notifyForm.Close();
}
}, "Policy");
Form is closing fine, when I run it through Visual Studio, if I navigate to Release folder and run exe explicitly, Deactivate event never fires. Help on this really appreciated.
Just for more information, notifyForm is notification pop up window which shows up above the tray, on click of tray icon.
Also, I have tried running application in release mode through visual studio, and it works fine. Only problem start happening when i run it outside of studio.
I have verified exe is up to date.
Edit - Basically I have form which i open on click of tray icon, and expecting clicking on desktop or some where should trigger deactivate event, which is not happening. Not sure why. :(

You need to compile your code in release to update the .exe!
EDIT: Maybe this link can help you

Thanks all for your help.
Calling below method, after showing pop up solved my issue.
notifyForm.Activate();
Thanks again.

Because VS runs code in a nice debug environment. Even in release mode. It keeps references alive longer than when running outside of the debugger.
Store notifyForm in a scope that will be alive at the time deactivate might be called. Easiest is a class variable.

Related

UWP Cannot open an external window after changing visibility of an inner element

I have a UWP app written in C#, where I need to open a second window.
Inside this second window, there is an element that I need to toggle the visibility of (in some cases).
Please find an MVCE here: https://github.com/cghersi/UWPExamples/tree/master/AppWindowAndVisibility
If you look into it, you will see an element m_searchGrid which we can change the visibility (using method SwitchSearchBarVisibility())
To open a new window, I use the primitives AppWindow.TryCreateAsync and ElementCompositionPreview.SetAppWindowContent.
Now, the following procedure results in an Exception when calling ElementCompositionPreview.SetAppWindowContent:
Open a new window (in the app, click on the button "Open AppWindow")
Change the visibility of an element in the newly opened window (in the app, in the new window that is opened, click on the button "Show/hide search bar")
Close the second window
Reopen again another window (in the app, re-click on the button "Open AppWindow")
From a code perspective, at step 4 I can see that the method ElementCompositionPreview.SetAppWindowContent throws an Exception (see line 108 of MainPage.xaml.cs, in the method ShowAsync()).
Do you have any clue on what could be the cause?
Maybe I should configure the window or the element in different ways?
Thanks!!
UWP Cannot open an external window after changing visibility of an inner element
The problem is m_wrapperCanvas was used by previous m_appWindow, it was not released even you called m_appWindow = null.
For solved this problem we suggest set m_wrapperCanvas = null, and re-new the m_wrapperCanvas before ElementCompositionPreview.SetAppWindowContent
private void AppWindow_OnClosed(AppWindow sender, AppWindowClosedEventArgs args)
{
m_appWindow.Closed -= AppWindow_OnClosed;
m_appWindow = null;
m_wrapperCanvas = null;
}
..........
try
{
Init();
ElementCompositionPreview.SetAppWindowContent(m_appWindow, m_wrapperCanvas);
}

Give focus to WPF application if other instances are running

I want to create a Spotify-like behavior for my WPF application. When the application is running, but minimized in the taskbar and the user tries to start a new instace, it should restore it from the taskbar, and give focus to the already running instance. The problem is stumble upon is to open to application from minimized state in the taskbar to be in focus. It works if the application is open, but other windows are placed over it.
var processHndl = FindWindow(null, "MyApplicationName!");
if (processHndl != IntPtr.Zero)
{
var isIconic = IsIconic(processHndl);
MessageBox.Show("Iconic: " + isIconic);
if (IsIconic(processHndl))
{
ShowWindow(processHndl, 9);
}
SetForegroundWindow(processHndl);
}
Any ideas?
Please try
SwitchToThisWindow
Windows Function Reference.

Access denied when killing process

I have an app that uses TabTip. When i want to close it i do this:
public void CloseTabTip()
{
foreach (Process pkiller in Process.GetProcesses())
{
if (pkiller.ProcessName.ToString() == "TabTip")
{
pkiller.Kill();
return;
}
}
}
And it trows an exception: access denied. Now the strange thing is that it only does that when i close the TabTip manually. And even stranger than that it only does that when i close it manually using the touch screen. Because when i close it using the mouse it works fine.
The function CloseTabTip is called when i click a button. If i click the button after manually closing the TabTip with touch screen it trows exception but if i close it using mouse it does not.
Now, i have pondered that it was because of this, however even if i wait some seconds for the process to close it still trows this exception. I am thinking this is a Windows bug since it only give an error when i close TabTib using the touch screen.
Any ideas on how to "fix" this? I know i can use a try catch statement but that is not the most correct way of doing things.
Running app in WIN10

Unit Test WPF Control Get Access to Child Dialog

We are using NUnit to test a WPF control.
The text fixture basically opens a test window containing the control to be tested on a new thread. Then the Microsoft UI Automation (UIA) is used to interact with the control.
The new thread shows the window and starts the dispatcher. Things work as expected.
The issue we are running into is that this control can launch a dialog. Once the dialog is launched we need to interact with it and close it. I have been unable to get a reference to this dialog to accomplish this task.
One solution that does not work is to use Application.Current.Windows to get all the windows and then iterate through them until the dialog is found. This does not work because during unit testing Application.Current = null. Now if we only care about this test we can just instantiate an Application. This will however interfere with other tests, because the Application will automatically enter shutting down mode when our Application variable goes out of scope (at the end of the test). As a result other tests will fail (most notably because InitializeComponent typically calls System.Windows.Application.LoadComponent which can't be called during shutting down mode).
I suppose what we need is an alternative to Application.Current.Windows.
I found a working solution to my problem.
UIA fires a number of events. One of them indicates that a new window has opened.
Subscribe a handler to the WindowOpenedEvent:
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, new AutomationEventHandler(NewWindowHandler));
public void NewWindowHandler(Object sender, AutomationEventArgs e)
{
AutomationElement element = (AutomationElement)sender;
if (element.Current.Name == "PUT YOUR NAME HERE")
{
HwndSource hSource = HwndSource.FromHwnd(new IntPtr(element.Current.NativeWindowHandle));
MyWindow = hSource.RootVisual as WavefrontToolkit.FormulaEditor.FormulaEditor;
Assert.IsNotNull(_MyWindow );
}
}
}
In the handler you do not have a reference to the window that was opened. You can however get it from the Win32 handle.
The other issue I encountered is that the test will continue on as the Window is opening. Some of the test might be depended on that window. To deal with that I cause a delay until the Window is ready.
while (MyWindow == null)
{
System.Threading.Thread.Sleep(10);
}

Error with tao SimpleOpenGlControl

I am creating an usercontrol contains a "Tao.Platform.Windows.SimpleOpenGlControl".
In my control's constructor, I have
{
InitializeComponent();
simpleOpenGlControl1.InitializeContexts();
}
My problem:
When I use the control on a "Windows Forms Application" it's ok, but if I put the computer at hibernate or sleep mode, when visual studio is open and form that contains the control, is in design mode, the next time I turn it on this error comes up:
Fatal Error
can not activate the gl rendering context
and visual studio is not responding!
What's wrong here? I am doing something wrong?
I am using Tao framework.
After implementing OpenGL chart solution, I encounter those error.
Every time I tried to close form, error occurred.
After few times of debugging, I found the reason.
The reason is like this.
On my form closing, Draw function tried to use OpenGlControl object ONE MORE TIME.
So I make condition to check the additional flag.
I solved my problem in this way:
In control's InitializeComponent(); I removed simpleOpenGlControl1 Initialization and then in control's Load() function, I have
isDesignMode = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
if (!isDesignMode)
{
// init simpleOpenGlControl1
}
Now when my control is used in a project, there is no simpleOpenGlControl1 in design mode to make problem!

Categories