Click here to Skip to main content
16,019,435 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have an application which uses a number ot timer threads. These threads use a delegate to call functions on the main thread, and work fine. I am implementing a new thread (not a timer) and am having issues with the delegate, code below.

C#
//This is the decleration at the begining of the main thread used by timers and new thread
//delegate so worker threads can call local functions

        public delegate void DoSomething();


// this is part of the new thread code where the delegate is being called

                  if (this.InvokeRequired)
                    {
                        try
                        {

                            DoSomething d = new DoSomething(LoadChange);
                            this.Invoke(d);
                        }
                        catch (ObjectDisposedException r)
                        {
                            MessageBox.Show("caught Object Dispose {0}", r.ToString());
                        }
                        catch (NullReferenceException r)
                        {
                            MessageBox.Show("caught null ref {0}", r.ToString());
                        }
                        catch (IndexOutOfRangeException r)
                        {
                            MessageBox.Show("caught ind out of range {0}", r.ToString());
                        }
                    }
                    else
                        this.LoadChange();
                   
                    Thread.Sleep(frq);

                }
            }
            
            catch (Exception ex)
            {
                MessageBox.Show("Error in repeat send routine." + ex);
            }
            START.BackColor = Color.LightSkyBlue;
        
        }

//this is function in the main thread to be called by the delegate

        private void LoadChange()
        {
            string current = repeat;
            if (!current.Equals(repeat))
            {
                Message2.Text += repeat;
                current = repeat;
            }
            
        }


I do not have a lot of experience with delegates and threads. The timer threads are exactly like the code in the new thread(cut and paste) except the new thread is calling a different function. When I compile and run the new thread I get no errors but the LoadChange function never gets called, at least the break point in the LoadChange function never gets hit. Any help would be appreciated.
Dave
Posted
Updated 6-Mar-14 10:49am
v2
Comments
Sergey Alexandrovich Kryukov 6-Mar-14 20:06pm    
What issues, exactly?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900