My notifyicon program is duplicated when using Form.Show () to call the preloader form

EDIT: Stupid me. My preloader actually appears all the time even with a simple one Loading.Show();, but it just was below my web browser. I have already posted the solution below. Please check it if you are interested.

My program does not have a form, but displays as a notification in the notification area (my main form is actually hidden for use as a dummy form for notifications and other controls). It is launched by calling from a web browser using the URI ( tkh) scheme . If my program is already running and the user called it from the browser, it will do something in accordance with its argument. For example, if a user called it using tkh:readCard, my program will work in a function readCard.

Here is my code for reading an argument from a URI scheme

public string CommandLine { get; set; }
        public bool CheckForProtocolMessage(Uri uri)
        {
            if (uri.ToString().Length > 1)
            {
                string[] args = uri.ToString().Split(':');
                CommandLine = args[1];
                if (args[0].Trim().ToUpper() == "TKH" && args.Length > 1)
                {
                    if (args[1].Length > 1)
                    {
                        switch (args[1].Trim().ToUpper())
                        {
                            case "READCARD":
                                if (hasCardReader == true)
                                {
                                    var bw_readCard = new BackgroundWorker { WorkerReportsProgress = true };                                        
                                    bw_readCard.DoWork += delegate
                                    {
                                        preloaderShow();
                                        readCard();
                                        preloaderClose();
                                    };
                                    bw_readCard.ProgressChanged += delegate { };
                                    bw_readCard.RunWorkerCompleted += delegate { };
                                    bw_readCard.RunWorkerAsync();
                                    bw_readCard.Dispose();
                                    return true;
                                }
                                else
                                {
                                    MessageBox.Show("Try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return false;
                                }


                            case "READCARD_IDONLY":
                                if (hasCardReader == true)
                                {
                                    var bw_readCard_IDOnly = new BackgroundWorker { WorkerReportsProgress = true };
                                    bw_readCard_IDOnly.DoWork += delegate
                                    {
                                        preloaderShow();
                                        readCard_IDonly();  
                                        preloaderClose(); 
                                    };
                                    bw_readCard_IDOnly.ProgressChanged += delegate { };
                                    bw_readCard_IDOnly.RunWorkerCompleted += delegate { };
                                    bw_readCard_IDOnly.RunWorkerAsync();
                                    bw_readCard_IDOnly.Dispose();
                                    return true;
                                }
                                else
                                {
                                    MessageBox.Show("Try again", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    return false;
                                }
                        }       
                    }
                }
            }
            return false;
        }

preloaderShow();and preloaderClose();are intended for display and closing of a form of preliminary tension. (My preloader form is named "Download")

Here is the function preloaderShow();

private void preloaderShow()
        {
            Loading Loading = new Loading();
            Loading.Show();
        }

and preloaderClose();function

 private void preloaderClose()
        {
            Loading Loading = new Loading();
            Application.OpenForms
                .OfType<Form>()
                .Where(form => String.Equals(form.Name, "Loading"))
                .ToList()
                .ForEach(form => form.Close());
        }

, , . , , readCard(); readCard_IDOnly(); .

preloaderShow(); ,

private void preloaderShow()
        {
            Loading Loading = new Loading();
            Form1 Form1 = new Form1(); // Declared this even without put Form1 into .Show() also make the program notifyicon duplicate.
            Loading.Show(Form1);
        }

, .

but notifyicon will be duplicated and cannot be closed

"" notifyicon, .

? .

+4
1

, , - . , , , ( Windows .)

protected override bool ShowWithoutActivation { get { return true; } }
protected override CreateParams CreateParams
{
    get
    {
        int WS_EX_TOPMOST = 0x00000008;
        CreateParams cp = base.CreateParams;
        cp.ExStyle |= WS_EX_TOPMOST;
        return cp;
    }
}

: fooobar.com/questions/507653/...

0

Source: https://habr.com/ru/post/1696044/


All Articles