Click here to Skip to main content
15,914,500 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to serialize System.Drawing.Pen and Brush objects? Pin
Andy Moore11-May-06 8:23
Andy Moore11-May-06 8:23 
GeneralRe: How to serialize System.Drawing.Pen and Brush objects? Pin
Sirinao11-May-06 11:07
Sirinao11-May-06 11:07 
AnswerRe: How to serialize System.Drawing.Pen and Brush objects? Pin
Robert Rohde11-May-06 10:33
Robert Rohde11-May-06 10:33 
QuestionConnect to remote Access DB Pin
Julie Wittwer11-May-06 7:45
Julie Wittwer11-May-06 7:45 
AnswerRe: Connect to remote Access DB Pin
NaNg1524111-May-06 7:54
NaNg1524111-May-06 7:54 
QuestionC# Screen Refresh Pin
tjschilling11-May-06 7:17
tjschilling11-May-06 7:17 
AnswerRe: C# Screen Refresh Pin
Ravi Bhavnani11-May-06 7:24
professionalRavi Bhavnani11-May-06 7:24 
QuestionMultithreaded splash form causing weird problems. Need help, code inlcuded. Pin
LongRange.Shooter11-May-06 6:44
LongRange.Shooter11-May-06 6:44 
My intent is to construct a very simple splash screen that can be used both at project start as well as a status screen for long-running tasks. Here is the guts of the code (SplashScreen is private so that it can only be accessed via the Load class)
    public delegate void SetTextDelegate(string message, string action);
    partial class SplashScreen : Form
    {

        #region Private variables and Properties

        string actionText = string.Empty;
        string messageData = string.Empty;
        int messageLength;

        bool loaded;
        /// <summary>
        /// Gets the load status of the form
        /// </summary>
        public bool ScreenLoaded { get { return loaded; } }

        #endregion

        #region .ctor logic
        /// <summary>
        /// Constructor logic
        /// </summary>
        /// <param name="message"></param>
        /// <param name="action"></param>
        /// <param name="title"></param>
        public SplashScreen(string message, string action, string title)
        {
            InitializeComponent();
            messageData = message;
            actionText = action;
            this.title.Text = title;
            SetText();
            
        }
#endregion

        #region Form Thread methods

        /// <summary>
        /// Sets the text in the message area
        /// </summary>
        private void SetText()
        {
            if (InvokeRequired)
                Invoke(new MethodInvoker(SetText));
            this.messageText.Text = String.Concat(messageData, " : ", actionText);
            this.messageLength = this.messageText.Text.Length;
            if (loaded)
                this.Invalidate();
        }
        /// <summary>
        /// Appends a '.' to the message area
        /// </summary>
        private void ExpandText()
        {
            if (InvokeRequired)
                Invoke(new MethodInvoker(ExpandText));
            this.messageText.Text += ".";
            this.Invalidate();
        }
        /// <summary>
        /// Delegated access point for SetText
        /// </summary>
        /// <param name="message"></param>
        /// <param name="action"></param>
        private void InvokeSetText(string message, string action)
        {
            messageData = message;
            actionText = action;
            SetText();
        }



        /// <summary>
        /// Ensures that the form is shown.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            this.Activate();
            base.OnLoad(e);     // let the base raise the Load event
        }

        /// <summary>
        /// Process for each tick on the timer within this forms thread
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void threadTimer_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Thread timer has ticked.");
            if (this.messageText.Text.Length > messageLength + 4)
            {
                SetText();
            }
            else
            {
                ExpandText();
            }
        }

        /// <summary>
        /// Load event -- activate the timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplashScreen_Load(object sender, EventArgs e)
        {
            this.threadTimer.Enabled = true;
            this.threadTimer.Interval = 100;
            this.threadTimer.Start();
        }

        /// <summary>
        /// Make sure this thread ends!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplashScreen_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.threadTimer.Stop();
        }

        /// <summary>
        /// When screen if finally shown for the first time, mark the form loaded!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SplashScreen_Shown(object sender, EventArgs e)
        {
            this.loaded = true;
        }

        #endregion

        #region External Thread methods
        /// <summary>
        /// External access (for threads) to change data on the form
        /// </summary>
        /// <param name="message"></param>
        /// <param name="action"></param>
        public void SetMessageData(string message, string action)
        {
            if (InvokeRequired)
            {
                SetTextDelegate invoker = new SetTextDelegate(InvokeSetText);
                Invoke(invoker, new object[] { message, action });
            }
            else   // do nothing -- illegal call to this method 
            { }
        }
        #endregion

    }

    public static class Loader
    {
        #region Static methods and data
        static SplashScreen screen;
        static Thread SplashThread;

        /// <summary>
        /// Displays the splash screen in the center of the parent.
        /// </summary>
        static public void ShowSplashScreen(string message, string action, string title)
        {
            if (SplashThread != null)
                return;
            screen = new SplashScreen(message, action, title);

            SplashThread = new Thread(new ThreadStart(ShowScreen));
            SplashThread.IsBackground = true;
            SplashThread.SetApartmentState(ApartmentState.STA);
            SplashThread.Start();
            // wait for the form to be loaded and ready for caller to consume
            while (screen.ScreenLoaded == false && SplashThread.ThreadState == ThreadState.Running)
            { }
        }

        [STAThread]
        static void ShowScreen()
        {
            if (screen != null)
            {
                Application.Run(screen);
            }
        }

        /// <summary>
        /// Update the message and action
        /// </summary>
        /// <param name="message"></param>
        /// <param name="action"></param>
        public static void UpdateScreen(string message, string action)
        {
            screen.SetMessageData(message, action);
        }

        /// <summary>
        /// Closes the splashscreen
        /// </summary>
        public static void TerminateScreen()
        {
            if (screen == null) return;
            if (SplashThread == null) return;

            screen.Invoke(new MethodInvoker(screen.Close));
            screen = null;
            SplashThread = null;
        }


I have a testbed application with one button that does a synchronous search and a second one that does a mutli-threaded search. Both use the splashscreen to display steps being performed. My intent for the splashscreen is that a timer ticks every 100 ms and adds a '.' to the end of the message until there are five '.' and then it starts all over again.

Problem: In the synchronous search the timer does not seem to ever get started even though it should be on a totally separate thread and started when the form loads. Confused | :confused:

Problem: In asynchronous search mode the timer does start, but I get a CrossThreadException the first time I go to add a '.' !! Cry | :((

Does anyone have a suggestion as to where I messed up in my code????

-- modified at 13:11 Thursday 11th May, 2006
AnswerRe: Multithreaded splash form causing weird problems. Need help, code inlcuded. Pin
Judah Gabriel Himango11-May-06 9:31
sponsorJudah Gabriel Himango11-May-06 9:31 
GeneralRe: Multithreaded splash form causing weird problems. Need help, code inlcuded. Pin
LongRange.Shooter13-May-06 5:13
LongRange.Shooter13-May-06 5:13 
GeneralRe: Multithreaded splash form causing weird problems. Need help, code inlcuded. Pin
Office Lineman12-May-06 23:03
Office Lineman12-May-06 23:03 
GeneralRe: Multithreaded splash form causing weird problems. Need help, code inlcuded. Pin
LongRange.Shooter13-May-06 5:14
LongRange.Shooter13-May-06 5:14 
QuestionDynamically load DLL class to the project Pin
Virtek11-May-06 6:37
Virtek11-May-06 6:37 
AnswerRe: Dynamically load DLL class to the project Pin
LongRange.Shooter11-May-06 6:49
LongRange.Shooter11-May-06 6:49 
GeneralRe: Dynamically load DLL class to the project Pin
Virtek11-May-06 8:00
Virtek11-May-06 8:00 
QuestionBinding a checkbox to a bool? Pin
iswoolley11-May-06 5:53
iswoolley11-May-06 5:53 
AnswerRe: Binding a checkbox to a bool? Pin
J4amieC11-May-06 6:03
J4amieC11-May-06 6:03 
GeneralRe: Binding a checkbox to a bool? Pin
iswoolley11-May-06 7:16
iswoolley11-May-06 7:16 
QuestionOnly able to print once? Pin
melanieab11-May-06 5:33
melanieab11-May-06 5:33 
AnswerRe: Only able to print once? Pin
LongRange.Shooter11-May-06 6:56
LongRange.Shooter11-May-06 6:56 
GeneralRe: Only able to print once? Pin
melanieab11-May-06 9:15
melanieab11-May-06 9:15 
GeneralRe: Only able to print once? Pin
melanieab12-May-06 6:05
melanieab12-May-06 6:05 
GeneralRe: Only able to print once? Pin
Martin#16-Nov-06 22:57
Martin#16-Nov-06 22:57 
Questionoverrride onpaint Pin
Sasuko11-May-06 4:27
Sasuko11-May-06 4:27 
AnswerRe: overrride onpaint Pin
Ravi Bhavnani11-May-06 4:55
professionalRavi Bhavnani11-May-06 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.