English 中文(简体)
另一班级的校对时
原标题:Cancelling Thread Timer from another class
  • 时间:2012-04-28 13:12:52
  •  标签:
  • c#
  • wpf
  • timer

Im 试图为校对实施国家管理网例(http://msdn.micro f.com/en-us/library/swx5ahy. asx)。 我自己的《手法》中的时间。

我希望能够在某个用户采取行动时取消时间,但我无法处置时间,我怀疑,这是因为我说,我从另一类人那里寻找一种方法,因此我需要调整;但我不知道什么地方。

除此以外,行凶者还罚款。 谁能看到,为什么当有人叫作假时我的时间不会取消?

www.un.org/spanish/ecosoc FYI I m 转换工人工序为时间活动:。

public partial class Xservt : Window
        {

            internal class TimerStateObjClass
            {
                public int SomeValue;
                public System.Threading.Timer SqlUpdateFromTwitterTimerReference;
                public bool TimerCanceled;
            }


internal void SomeMethod(){


                    TimerStateObjClass stateObj = new TimerStateObjClass();
                    stateObj.TimerCanceled = false;
                    stateObj.SomeValue = 100;
                    System.Threading.TimerCallback timerDelegate =
                        new System.Threading.TimerCallback(twit.hometimelineclass._sqlUpdateFromTwitterWorker_DoWork);

                    var sqlUpdateFromTwitterTimer = new Timer(timerDelegate, stateObj, 0,20000);
                    stateObj.SqlUpdateFromTwitterTimerReference = sqlUpdateFromTwitterTimer;

    }
}


//action to perform which disposes the timer
private void btnconfigOpenConfig(object sender, RoutedEventArgs e)
            {

                TimerStateObjClass timerState = new TimerStateObjClass();
                timerState.TimerCanceled = true;
    }


//Actions the timer is calling, in another class
internal static void _sqlUpdateFromTwitterWorker_DoWork(object StateObj)
            {

                Xservt.TimerStateObjClass state = (Xservt.TimerStateObjClass) StateObj;

                if(state.TimerCanceled)
                {
                    state.SqlUpdateFromTwitterTimerReference.Dispose();

                }

    //some work
    }
最佳回答

正如汉斯在评论中指出的,你需要经常提及时国。 ObjClass 你最初创建。 然后,你可以利用这一方法确定时间。

public partial class Xservt : Window
{

    internal class TimerStateObjClass
    {
        public int SomeValue;
        public System.Threading.Timer SqlUpdateFromTwitterTimerReference;
        public bool TimerCanceled;
    }

    TimerStateObjClass stateObj;  //THIS IS THE ORIGINAL STATE OBJ
    internal void SomeMethod()
    {
        stateObj = new TimerStateObjClass();
        stateObj.TimerCanceled = false;
        stateObj.SomeValue = 100;
        System.Threading.TimerCallback timerDelegate = new System.Threading.TimerCallback(twit.hometimelineclass._sqlUpdateFromTwitterWorker_DoWork);

        var sqlUpdateFromTwitterTimer = new Timer(timerDelegate, stateObj, 0, 20000);
        stateObj.SqlUpdateFromTwitterTimerReference = sqlUpdateFromTwitterTimer;
    }

    //action to perform which disposes the timer
    private void btnconfigOpenConfig(object sender, RoutedEventArgs e)
    {
        //HERE WE CAN GET AT THE ORIGINAL STATE OBJ
        stateObj.TimerCanceled = true;
    }
}
    //Actions the timer is calling, in another class
    internal static void _sqlUpdateFromTwitterWorker_DoWork(object StateObj)
    {
        Xservt.TimerStateObjClass state = (Xservt.TimerStateObjClass)StateObj;

        if (state.TimerCanceled)
        {
            state.SqlUpdateFromTwitterTimerReference.Dispose();
        }

        //some work
    }
问题回答
  1. You need to store reference to your timer (or class that references the timer) somewhere in your class.
  2. To stop the timer there is not need to dispose it. You can just call timer.Change(-1, -1);. That will allow to re-enable timer again by calling timer.Change(dueTimeInMs, intervalInMs);

你们的法典应当像:

public partial class Xservt : Window
{
   private Timer timer = new Timer(o => DoSomething());

   private void StartTimer()
   {
     var period = 5 * 1000; // 5 sec
     timer.Change(0, period);
   }

   private void StopTimer()
   {
     timer.Change(-1, -1);
   }
}

然后打电话Starttimer,以便操作,Stoptimer,以便分别停用。

Also note that if there is any chance that DoSomething will run longer than timer interval that would result in running that method in more than one thread concurrently. To avoid that DO NOT use Timer s interval but use dueTime instead:

   private Timer timer = new Timer(o => {
                                         DoSomething(); 
                                         StartTimer();
                                         });

   private void StartTimer()
   {
     var period = 5 * 1000; // 5 sec
     timer.Change(period, 0);
   }

此时此刻,只有一次开车,但每次开车后再开车。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签