Windows Develop Bookmark and Share   
 index > Windows Forms General > C#: Child Windows Form Locked Up
 

C#: Child Windows Form Locked Up

Hello all,

I am actually a web developer by trade. However, I decided to take on this Windows project and it has been a bear.

Anyways, my program, among other things, launches a process, using the WatiN component, which opens a website and, using records stored in a database, performs searches and then imports the information from the result pages. That works great!

Now, the client added an additional requirement where if a particular lookup takes more than 30 seconds, they want a timer to appear that counts up to 15 minutes. If the timer reaches the 15 minutes, then I need to stop the lookup, log an error and begin the lookup all over again.

So, I create a new windows form for my timer and coded a simply timer display using the Windows.Timer object.

However, when my screen scrape process is running and it opens the timer form, the forms displays nothing but white blocks where the label controls would be.

I have tried many ways of working around this, including using the BackgroundWorker control, Invoking the timer winform as a delgate,  and even moving all of the screen scrape code into the timer form, itself. None of this has yielded good results. 

I CAN get a background timer working using the System.Timers.Timer object, which at least allows me to determine if the thing is taking too long.

Any suggestions on how to either a) get the timer winform to work concurrently with the screen scrape form or b) display the timer in another way, using a messagebox or some other mechanism?

Any help is great appreciated.

Thanks.

Bob
Bob Gibilaro  20 hours 3 minutes ago

Hi Bob,

 

Based on my understanding, you want to cancel the query job with a timer control. I think you have done right to use a back ground thread. According to my knowledge, I would recommend you to use BackgroundWorker along with a Timer control on the form.

 

Here is the sample code

public partial class Form2 : Form

    {

        private Timer timer;

        private BackgroundWorker worker;

 

        public Form2()

        {

            InitializeComponent();

            worker = new BackgroundWorker();

            worker.DoWork += new DoWorkEventHandler(worker_DoWork);

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

 

            timer = new Timer();

            timer.Interval = 15000;

            timer.Tick += new EventHandler(timer_Tick);

            timer.Enabled = true;

 

            worker.WorkerSupportsCancellation = true;

            worker.RunWorkerAsync();

        }

 

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            timer.Enabled = false;

            this.Close();

        }

 

        void worker_DoWork(object sender, DoWorkEventArgs e)

        {

            // Simulate the query process

            while (true)

            {

                if (worker.CancellationPending == true)

                {

                    e.Cancel = true;

                    return;

                }

            }

        }

 

        void timer_Tick(object sender, EventArgs e)

        {

            // Write log and close

            worker.CancelAsync();

            worker.Dispose();

            this.Close();

        }

    }

 

The Form2 is the child form which will be raised by Form1. When Form2 is created, it will start a BackgroundWork thread to do a lot of query job. The timer on Form2 will calculate the time. When it reaches 15 seconds, the work will be canceled while Form2 writing log. After that, Form2 will be forced to close.

 

Do I misunderstand something? Please feel free to tell me if you need any help.

 

Sincerely,

Kira Qian

Send us any feedback you have about the help from MSFT at fbmsdn[At]microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework!
Kira Qian  7 hours 41 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• MDI child form to maximize to the size of parent free space
• How do I give a TextBox focus when a Form opens?
• OutOfMemory exception when loading large images
• WebBrowser control and Document.Cookie
• How to get the Position of an element using DOM
• Moving controls to a panel results in losing it's events!
• DataGridViewComboBoxCell
• User Settings not Sensible to Versioning II
• Copy and Past Excel spreadsheet
• How to compress pathtypes