[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

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++;
        }
    }
}
0
Richard Payne
Asked:
Richard Payne
  • 6
  • 5
  • 2
1 Solution
 
BIOGENIUSCommented:
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.
0
 
Richard PayneChief Technology EngineerAuthor Commented:
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.



0
 
BIOGENIUSCommented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
käµfm³d 👽Commented:
Is there a reason why you are not inheriting from Form and instead have a Form member object that you are showing?
0
 
Richard PayneChief Technology EngineerAuthor Commented:
Hi BIOGENIUS

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

The form close itself after timeout when message information is outdated.
0
 
käµfm³d 👽Commented:
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

0
 
käµfm³d 👽Commented:
The above uses a timer instead of the ThreadPool as you were attempting. It also inherits from Form.
0
 
Richard PayneChief Technology EngineerAuthor Commented:
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?
0
 
käµfm³d 👽Commented:
In your code, you had a class (called MsgBox) that had a member variable of type Form (Form fmessage). In my code, I took your class and made it inherit the properties, methods and member data of the Form class (MsgBox : Form). This is called inheritance. It is one of the features of Object Oriented programming (you can google this).

By inheriting the Form object in your class, the MsgBox class now has all the attributes a default form would have (in a manner of speaking). As such, the two statements examples below would produce the same output.
// Form is built-in, so there is no code to show

// Define the custom class
public class MsgBox : Form // inherit Form
{

}

//  Example of Inheritance at work
public static void Main(string[] args)
{
    Form defaultForm = new Form();
    MsgBox customForm = new MsgBox();

    defaultForm.Show();
    customForm.Show();
}

Open in new window

0
 
käµfm³d 👽Commented:
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.

:)
0
 
Richard PayneChief Technology EngineerAuthor Commented:
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.

 
0
 
käµfm³d 👽Commented:
NP. Glad to help :)
0
 
Richard PayneChief Technology EngineerAuthor Commented:
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 !

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now