Backgroundworker failing to execute

G-Oker

Member
Joined
Jan 21, 2021
Messages
8
Programming Experience
1-3
Hello,
I am trying to get a messagebox to "click" the enter key if left open for a a specified amount of time.
I can do this (of a fashion) by using a timer, but the issue is that if there IS user input within the time frame, the key is still"pushed" by the timer (NB: this should not be an issue as there is nothing else on the app that the enter key should effect.
Because its neater, and to err on the side of caution, I am trying to get backgroundworker to do it instead, but I cannot get ti to fire/execute.

Would someone here be able to look at my code and point out what I am/could be doing wrong ?

C#:
            private void timer1_Tick(object sender, EventArgs e)
                {
                timer1.Enabled = false;
                SendKeys.Send("{ENTER}"); // SendWait as alternative
                }
                
                        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                {
    
                //timer1.Interval = 10000;
                //timer1.Enabled = true;
                backgroundWorker1.RunWorkerAsync();
    
                if (MessageBox.Show("Are you sure you want to close?", "Leave ? ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                    e.Cancel = false;
                    }
                else
                    {
                    e.Cancel = true;
                    }
                }
    
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
                {
                Thread.Sleep(5000);
                SendKeys.Send("{ENTER}"); //or Esc
                }

Thank you.
 
Did you set a breakpoint on line 27 to truly prove to yourself that the background worker is not running?

Or is the issue really that the background worker is working, but the SendKeys() is not actually sending the Enter key to the UI thread?
 
Did you set a breakpoint on line 27 to truly prove to yourself that the background worker is not running?

Or is the issue really that the background worker is working, but the SendKeys() is not actually sending the Enter key to the UI thread?
Thank you for replying :)

No, I didn't set a breakpoint, I complied the code, and opened the messagbox, but it remained open. You raise a goof point though, and I will try something here to see if it is firing, jut the sendkey is not working. Thankyou
 
You'll get into all kinds of trouble with that approach. For a MessageBox with timeout I liked one of the posts here: Close a MessageBox after several seconds
The code goes like this:
C#:
var autoclose = false;
var w = new Form() { Size = new Size(0, 0) };
Task.Delay(TimeSpan.FromSeconds(3))
    .ContinueWith((t) => { w.Close(); autoclose = true; }, TaskScheduler.FromCurrentSynchronizationContext());

var result = MessageBox.Show(w, "Are you sure you want to close?", "Leave ? ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
e.Cancel = !autoclose && result == DialogResult.No;
An owner window is created and a Task used to close it. The dialog will automatically close if the owner is closed first. Dialog result will be No if that happens, so I added a variable to see if timeout happened and use that in e.Cancel expression. The logic is kind of inverted here since true cancels closing :rolleyes:
 
You'll get into all kinds of trouble with that approach. For a MessageBox with timeout I liked one of the posts here: Close a MessageBox after several seconds
The code goes like this:
C#:
var autoclose = false;
var w = new Form() { Size = new Size(0, 0) };
Task.Delay(TimeSpan.FromSeconds(3))
    .ContinueWith((t) => { w.Close(); autoclose = true; }, TaskScheduler.FromCurrentSynchronizationContext());

var result = MessageBox.Show(w, "Are you sure you want to close?", "Leave ? ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
e.Cancel = !autoclose && result == DialogResult.No;
An owner window is created and a Task used to close it. The dialog will automatically close if the owner is closed first. Dialog result will be No if that happens, so I added a variable to see if timeout happened and use that in e.Cancel expression. The logic is kind of inverted here since true cancels closing :rolleyes:
You Sir, are a STAR. This works perfectly. Thank you VERY much
 
Back
Top Bottom