-1

I am writing a PowerPoint addin in C# with a TaskPane.

At a moment I want to print something on the pane - wait 5 sec - and print something different.

I have a problem with the waiting process.

  • If I use System.Threading.Thread.Sleep(5000), I freeze all PowerPoint interface, not only my pane. PowerPoint interface and my task pane are one the same thread

  • If I use a System.Timers.Timer, then I have a second thread created and an error triggered saying that I am trying to access an object on a different thread.

This is my code for the 2nd case:

    private void waitThisTime(int givenTime)
    {
        timer = new System.Timers.Timer(givenTime);
        timer.Elapsed += OnTimedEvent;
        timer.AutoReset = false;
        timer.Enabled = true;
    }

    private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
    {
        DisplayMessage(currentState); // DisplayMessage modifies textbox and button displayed on the panel
    }

Do you know how to freeze only the panel but without triggering an exception?

EDIT

This question here deals with access problem with multiple thread. My question is a little bit different because it deals with timer and I want to freeze only a part of my application (only panel not entire user interface)

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Freddlow
  • 167
  • 11
  • Wich timer exactly do you use? There are 5+ all with the same name, and they varry in if the Counting is done on and/or the Tick event is raised on a Worker Thread. This looks like one of the "raise Event on other Thread" ones. The convention that we should not write the GUI across Thread borders was intentionally added in 2.0: http://stackoverflow.com/a/14703806 – Christopher May 05 '18 at 12:40
  • `but without trigering an exeception ?` It would help to know what that exception is – Ňɏssa Pøngjǣrdenlarp May 05 '18 at 13:10
  • @Christopher : I don't know there are many System.Timers. how can I know which timer it is ? – Freddlow May 05 '18 at 13:15
  • @Plutonix : System.InvalidOperationException: The calling thread can not access this object because another thread owns it – Freddlow May 05 '18 at 13:17
  • The first thing you should do when you get and exception you dont understand is to do a search on the error message. If you toss a `NET` or `c#` in with it you will likely get a highly answer from here, as I just did in about 3 seconds. Then you might want to read the message - the timer is not to blame - your code is . Use the Winforms timer. – Ňɏssa Pøngjǣrdenlarp May 05 '18 at 13:20
  • Possible duplicate of [The calling thread cannot access this object because a different thread owns it](https://stackoverflow.com/questions/9732709/the-calling-thread-cannot-access-this-object-because-a-different-thread-owns-it) – Ňɏssa Pøngjǣrdenlarp May 05 '18 at 13:21

1 Answers1

1

Try this:

System.Threading.Timer()
random
  • 415
  • 1
  • 7
  • 17
  • I used System.Threading.TimerCallback cb = new System.Threading.TimerCallback(doAfterTimer); System.Threading.Timer theTimer = new System.Threading.Timer(cb, null, 0, 5000); and it seems to work. But it is repeating every 5 sec and I don't find any parameter to disable repeat. Do you know if it is possible to set it to execute 1 time only ? – Freddlow May 05 '18 at 13:08
  • Set the period time to Timeout.Infinite – random May 05 '18 at 13:11
  • System.Threading.Timer theTimer = new System.Threading.Timer(cb, null, 5000, Timeout.Infinite); works well ! – Freddlow May 05 '18 at 13:27