Link to home
Start Free TrialLog in
Avatar of Richard Payne
Richard PayneFlag for Bulgaria

asked on

C#, VS2008, threadpool & window form, issue with non responsive form

Below is the sample code based on earlier query, however during the message pop up, it does not response to a control from when I tried to move it. I have included a sleep loop with update to keep form alive for user response, it seem not to work, why and what is the solution?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace WebHouComic
{
    public class MsgBox
    {
        Form fmessage;
        bool bExtendSleep;                     // if true, extend box lifetime by 10 second.
        int iThreadCounter;

        public int iTimeOutx { get; set; }     // setup timeout period before close window. 2 second default
        public string sMessagex { get; set; }
        public string sTopNamex { get; set; }
        public MsgBox()
        {
           iThreadCounter = 0;
        }

        private void DisplayMessages(object oParams)
        {
            fmessage = new Form();
            fmessage.Width = 300;
            fmessage.Height = 100;

            fmessage.Text = sMessagex;
            fmessage.Show();
            for (int i = 0; i <= 500; i++)
            {
                Thread.Sleep(10);       //10mSec
                fmessage.Update();
            }
            fmessage.Close();
        }

        public void Show(string sMessage)
        {
            sMessagex = sMessage;
            ThreadPool.QueueUserWorkItem(DisplayMessages, iThreadCounter);
            iThreadCounter++;
        }
    }
}
Avatar of BIOGENIUS
BIOGENIUS
Flag of Egypt image

why do you make it in that way?? if you show the form with showdialog(this) and make in onTop then it will not close till the user responed to it. if that is your purpose.
Avatar of Richard Payne

ASKER

No BIOGENIUS, it need to close automatically after timeout. That is my primary plan.

The intention is to display message without user to click button to close it.

Note:- I cannot use backgroundworker thread since it is used elsewhere.



the user will interact on this form then it will close...right?
if so you can do what i told you before then do the close after all items in the form are interacted with or filled.
Avatar of kaufmed
Is there a reason why you are not inheriting from Form and instead have a Form member object that you are showing?
Hi BIOGENIUS

User can move the form around and then it close itself.

The form close itself after timeout when message information is outdated.
Instead, doing something like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace WindowsFormsApplication1
{
    public class MsgBox : Form
    {
        System.Windows.Forms.Timer closeTimer;
        bool bExtendSleep;                     // if true, extend box lifetime by 10 second.
        int iThreadCounter;       

        public int iTimeOutx { get; set; }     // setup timeout period before close window. 2 second default
        public string sMessagex { get; set; }
        public string sTopNamex { get; set; }

        public MsgBox() : base()
        {
            this.Width = 300;
            this.Height = 100;

            iThreadCounter = 0;
            this.closeTimer = new System.Windows.Forms.Timer();
            this.closeTimer.Tick += new EventHandler(closeTimer_Tick);
        }

        void closeTimer_Tick(object sender, EventArgs e)
        {
            this.closeTimer.Stop();
            this.Hide();
        }

        public void Show(string sMessage)
        {
            sMessagex = sMessage;
            this.closeTimer.Interval = iTimeOutx;
            this.closeTimer.Start();

            this.Text = sMessagex;
            this.Show();
        }
    }
}

Open in new window

The above uses a timer instead of the ThreadPool as you were attempting. It also inherits from Form.
Hi Kaufmed, thank will try it out now.

"Is there a reason why you are not inheriting from Form and instead have a Form member object that you are showing?"

I trying to make sense "inheriting from Form" and "Form member object", apologize for being dim, what you mean and how can I learn more about it?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Inside the MsgBox class, because you inherited Form, you have access to all the non-private members that Form exposes. That is what allows me to call the Show() method on customForm above.

To go further, you would also have access to the Text property in customForm, so you could set the text in the title bar of customForm. That is exaclty what happened in the Show() method that I posted previously. The line

    this.Text = sMessagex;

is actually setting the Text property of Form, because MsgBox inherits it.

The discussion goes way deeper than this due to overloading, overriding, polymorphism, and so on...  Definately do a search on Object Oriented programming if you are not familiar with it.

:)
Yes I studied OOP and design pattern 2-3 year ago, I trying to recover from my non-programming period.

This is interesting as I have not considered OOP method on window forms. thank for refresher, I shall look again OOP from my past text study.

This solution you provided works very nicely....I can now finish the application.

 
NP. Glad to help :)
Polymorphism is a kind of word that I never get understanding (even I have head up book on design pattern 2-3 year ago). I fears "polymorphism" as it sound like vampire sucking my blood !!

Silly init !