I need to call addDownload()
method that is inside the main application class MainWindow.xaml.cs
from App.xaml.cs
Some info about the Application:
In my
App.xaml.cs
i have code to run only one instance of the application. (Not my Code. I found it)using System; using System.Diagnostics; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; using System.Windows; using DownloadManager; namespace DownloadManager { public partial class App : Application { private static readonly Semaphore singleInstanceWatcher; private static readonly bool createdNew; static App() { // Ensure other instances of this application are not running. singleInstanceWatcher = new Semaphore( 0, // Initial count. 1, // Maximum count. Assembly.GetExecutingAssembly().GetName().Name, out createdNew); if (createdNew) { // This thread created the kernel object so no other instance // of this application must be running. } else { // This thread opened an existing kernel object with the same // string name; another instance of this app must be running now. // Gets a new System.Diagnostics.Process component and the // associates it with currently active process. Process current = Process.GetCurrentProcess(); foreach (Process process in Process.GetProcessesByName(current.ProcessName)) { if (process.Id != current.Id) { NativeMethods.SetForegroundWindow( process.MainWindowHandle); NativeMethods.ShowWindow(process.MainWindowHandle, WindowShowStyle.Restore); break; } } // Terminate this process and gives the underlying operating // system the specified exit code. Environment.Exit(-2); } } private static class NativeMethods { [DllImport("user32.dll")] internal static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] internal static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow); } /// <summary> /// Enumeration of the different ways of showing a window.</summary> internal enum WindowShowStyle : uint { Hide = 0, ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3, Maximize = 3, ShowNormalNoActivate = 4, Show = 5, Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8, Restore = 9, ShowDefault = 10, ForceMinimized = 11 } } }
I need to pass Command Line Parameters to
addDownload()
method every time, whether the application is already running or not.
What i've tried so far:
First, i deleted the Startup entry point in App.xaml, so i can manage it inside code-behind
Then, if the Application is not running, create a new instance of MainWindow, and show it:
MainWindow MW = new MainWindow(); MW.Show();
Get Command Line Parameters, and pass it to the
addDownload()
method:string[] args = Environment.GetCommandLineArgs(); if(args.Length > 1) { MW.addDownload(args[1]); }
Ok, this part work perfectly.
But as i say, i need to pass Command Line Parameters also if the application is already running. Getting the command line parameters it's the same as before, but passing it to the addDownload()
method of the current MainWindow instance, is not, and i have troubles to find a working way;
I tried:
try
{
var main = App.Current.MainWindow as MainWindow;
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
{
main.addDownload(args[1]);
}
}
catch(Exception ex)
{
MessageBox.Show(String.Format("{0}\n{1}", ex.GetType().ToString(), ex.Message));
}
But i get a NullReferenceException.. i think on the main
declaration.. I don't know how to debug this particular situation inside Visual Studio.
Any help? What i'm doing wrong?