none
accessing static objects like System.Timers.Timer from other classes RRS feed

  • Question

  • I use static objects in my class and can access them from other classes, including from msmq callback methods and also networking callback methods(OnReceaive(...) etc). 

    Problem is when I try to access static objects like System.Timers.Timer

    In this case it ends up with exception

    Can someone refer to that please?

    Regards

    Izack

    Tuesday, October 11, 2011 7:07 PM

All replies

  • > Problem is when I try to access static objects like System.Timers.Timer


    System.Timers.Timer is not a static property. it is a class

    Tuesday, October 11, 2011 7:30 PM
  • Please provide more info on the exception you are getting?
    --Abhimanyu
    Click the 'Vote as Helpful' arrow if this post was helpful.
    Wednesday, October 12, 2011 6:38 AM
  • Thanks Malobukv

    I ment that I use it as follows

    static System.Timers.Timer timer1

    Modify properties of timer1 like timer1.Interval works OK from anywher but not from Msmq callback method.

    Have I calarified my quetion?

    Regards

    Izack

    Wednesday, October 12, 2011 11:41 AM
  • I'm not sure I understand why/how do you use Timer with MessageQueue. 
    would you please modify sample code below to clarify issue
    using System;
    using System.IO;
    using System.Messaging;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            MessageQueue _Queue;
    
            public Form1()
            {
                var path = @".\Private$\myQueue";
                _Queue = (MessageQueue.Exists(path) == false)
                        ? MessageQueue.Create(path)
                        : new MessageQueue(path);
    
                var b = new Button();
                b.Text = "Test";
                b.Click += (s, e) => _Queue.Send("test " + DateTime.Now);
                b.Parent = this;
    
                var stop = false;
                this.FormClosed += (s, e) => stop = true;
                Task.Factory.StartNew(() =>
                {
                    while (!stop)
                    {
                        var msg = _Queue.Receive();
                        using (var sr = new StreamReader(msg.BodyStream))
                        {
                            var str = sr.ReadToEnd();
                            System.Diagnostics.Trace.WriteLine(str, "Receive");
                        }
                    }
                });
            }
        }
    }
    
    
     
    Wednesday, October 12, 2011 1:56 PM
  • Thanks again

    But its better if I represent my code...

    I use events with my msmq. Hre is the definition

                       VlcPrivQue.ReceiveCompleted += new ReceiveCompletedEventHandler(Msmq_ReceiveCompleted);

    The handler looks like this
             private void Msmq_ReceiveCompleted(object sender,
                    System.Messaging.ReceiveCompletedEventArgs e)
    {
    timer1.Interval =2000;// here I get an exception
    ....
    }
    Hope this helps to clarify
    Regards
    Izack

    Wednesday, October 12, 2011 5:33 PM
  • ok. I have mix my code with your and I get no error message.

    using System;
    using System.Messaging;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            MessageQueue _Queue;
    
            public Form1()
            {
                var path = @".\Private$\myQueue";
                _Queue = (MessageQueue.Exists(path) == false)  ? MessageQueue.Create(path) : new MessageQueue(path);
    
                var b = new Button();
                b.Text = "Test";
                b.Click += (s, e) => _Queue.Send("test " + DateTime.Now);
                b.Parent = this;
    
                System.Diagnostics.Trace.WriteLine("", "start @ " + Thread.CurrentThread.ManagedThreadId);
    
                _Queue.ReceiveCompleted += ReceiveCompleted;
                _Queue.BeginReceive();
            }
    
            static System.Timers.Timer _Timer;
            static Form1()
            {
                _Timer = new System.Timers.Timer(1000);
                _Timer.Elapsed += new System.Timers.ElapsedEventHandler(_Timer_Elapsed);
                _Timer.Start();
            }
    
            static void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                System.Diagnostics.Trace.WriteLine(e.SignalTime, "timer @ " + Thread.CurrentThread.ManagedThreadId);
            }
    
            static void ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
            {
                var q = (MessageQueue)sender;
                var m = q.EndReceive(e.AsyncResult);
                System.Diagnostics.Trace.WriteLine(m, "message @ " + Thread.CurrentThread.ManagedThreadId);
                _Timer.Interval += 10;
                q.BeginReceive();
            }
        }
    }
    

    • Proposed as answer by Malobukv Thursday, October 13, 2011 6:34 AM
    Wednesday, October 12, 2011 8:39 PM
  • Ok,

    It seems that I do something wrong elswhere ,perhaps with the use of design pattern.

    Anyhow thanks for the the help so far.

    Regards

    Izack

    Thursday, October 13, 2011 6:29 AM
  • timer1.Interval =2000;// here I get an exception

    Can you post the Exception details?


    Click the 'Vote as Helpful' arrow if this post was helpful.
    Thursday, October 13, 2011 6:36 AM