Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Windows Timer event, with other program

Windows Timer event, with other program

Scheduled Pinned Locked Moved C#
csharpgraphicshelpquestion
15 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T T Willey

    I don't know why/how it compiles either, but it does. =D The CancelCommand does fire, but you are correct in that my DwgList isn't anything in the CancelCommand part. I thought by adding it to the main part of the class, that it could be used inside the code, and shared with other parts of code as long as it was within the same class. Guess I was wrong, back to reading some more. I guess I need to figure out how to have two global variable. One for DwgList and one for cnt. Or figure out a way to pass them between the calls to CommandCancel. Thanks for your help, I don't think I would have ever got this far without it.

    Tim

    N Offline
    N Offline
    Nader Elshehabi
    wrote on last edited by
    #6

    Hello Well, forgive me if I say that your code sin't Object oriented. It looks like you have good experience in languages before C++. Your style is very procedural and it sin't that bad. It's just that your applications doesn't even have an interface?!! All you do is that you show a dialog, ake an input then do some interop. Well, either you make a full functional windows application or wrap your code in a dll or something. I didn't mean to ofend you - sincerely-, I just wanted to be as honest as I can with you. Anyway, I wouldn't like to disrupt your code structure, so, the closest solution to get your code to work -though it would be better to remodel your code later to be more OO-, is simply to declare your variables as static thus you'd be able to call them by the class. Here is your code, modified:

    namespace Test
    {
    /// /// Description of TimeLimitOpen.
    ///
    public class TimeLimitOpenv02
    {

        \[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
            EntryPoint = "?acedPostCommand@@YAHPBD@Z")\]
        extern static public int acedPostCommand(string cmd);
        private static string\[\] DwgList;//<---Modification
        public Document NewDoc;
        DocumentCollection DocMan = AcadApp.DocumentManager;
        System.Timers.Timer CountDownTimer;
        private static int cnt = 1; //<---Modification
        //- Constructor: sets up var's etc for this class before use.
        public TimeLimitOpenv02()
        {
            CountDownTimer = new System.Timers.Timer();
            CountDownTimer.Elapsed += new ElapsedEventHandler(CancelCommand);
            CountDownTimer.Interval = 10000;
        }
    
        ~TimeLimitOpenv02() // <-- Destructor, do clean up here.
        {
            CountDownTimer.Elapsed -= new ElapsedEventHandler(CancelCommand);
        }
        public void CancelCommand(object sender, ElapsedEventArgs e)
        {
            acedPostCommand ("CancelCmd");
            //MessageBox.Show (DocMan.ToString());
            NewDoc = DocMan.MdiActiveDocument;
            //MessageBox.Show ("Testing");
            //MessageBox.Show (cnt.ToString());
            NewDoc.CloseAndDiscard();
            NewDoc = DocMan.Open (DwgList\[cnt\], true);
            if (NewDoc != DocMan.MdiActiveDocument) {
                DocMan.MdiActiveDocument = NewDoc;
            }
            ++TimeLimitOpenv02.cnt;//You access it by the class name
            if (TimeLimitOpenv02.cnt > TimeL
    
    T 2 Replies Last reply
    0
    • N Nader Elshehabi

      Hello Well, forgive me if I say that your code sin't Object oriented. It looks like you have good experience in languages before C++. Your style is very procedural and it sin't that bad. It's just that your applications doesn't even have an interface?!! All you do is that you show a dialog, ake an input then do some interop. Well, either you make a full functional windows application or wrap your code in a dll or something. I didn't mean to ofend you - sincerely-, I just wanted to be as honest as I can with you. Anyway, I wouldn't like to disrupt your code structure, so, the closest solution to get your code to work -though it would be better to remodel your code later to be more OO-, is simply to declare your variables as static thus you'd be able to call them by the class. Here is your code, modified:

      namespace Test
      {
      /// /// Description of TimeLimitOpen.
      ///
      public class TimeLimitOpenv02
      {

          \[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
              EntryPoint = "?acedPostCommand@@YAHPBD@Z")\]
          extern static public int acedPostCommand(string cmd);
          private static string\[\] DwgList;//<---Modification
          public Document NewDoc;
          DocumentCollection DocMan = AcadApp.DocumentManager;
          System.Timers.Timer CountDownTimer;
          private static int cnt = 1; //<---Modification
          //- Constructor: sets up var's etc for this class before use.
          public TimeLimitOpenv02()
          {
              CountDownTimer = new System.Timers.Timer();
              CountDownTimer.Elapsed += new ElapsedEventHandler(CancelCommand);
              CountDownTimer.Interval = 10000;
          }
      
          ~TimeLimitOpenv02() // <-- Destructor, do clean up here.
          {
              CountDownTimer.Elapsed -= new ElapsedEventHandler(CancelCommand);
          }
          public void CancelCommand(object sender, ElapsedEventArgs e)
          {
              acedPostCommand ("CancelCmd");
              //MessageBox.Show (DocMan.ToString());
              NewDoc = DocMan.MdiActiveDocument;
              //MessageBox.Show ("Testing");
              //MessageBox.Show (cnt.ToString());
              NewDoc.CloseAndDiscard();
              NewDoc = DocMan.Open (DwgList\[cnt\], true);
              if (NewDoc != DocMan.MdiActiveDocument) {
                  DocMan.MdiActiveDocument = NewDoc;
              }
              ++TimeLimitOpenv02.cnt;//You access it by the class name
              if (TimeLimitOpenv02.cnt > TimeL
      
      T Offline
      T Offline
      T Willey
      wrote on last edited by
      #7

      Thanks for you help, and you can be as blunt as you want to be. I know I'm learing as I go, and my only background with programming is within the AutoCAD enviroment, and that was with Lisp. It is going to be run for a presentation on 3d drawings. It will only allow the people to use the 3dorbit command from within Acad for a limited amout of time per drawing. It will be ran from the command line, and loaded in as a dll file. Thanks again for all the help. I will let you know when I get it working.

      Tim

      1 Reply Last reply
      0
      • N Nader Elshehabi

        Hello Well, forgive me if I say that your code sin't Object oriented. It looks like you have good experience in languages before C++. Your style is very procedural and it sin't that bad. It's just that your applications doesn't even have an interface?!! All you do is that you show a dialog, ake an input then do some interop. Well, either you make a full functional windows application or wrap your code in a dll or something. I didn't mean to ofend you - sincerely-, I just wanted to be as honest as I can with you. Anyway, I wouldn't like to disrupt your code structure, so, the closest solution to get your code to work -though it would be better to remodel your code later to be more OO-, is simply to declare your variables as static thus you'd be able to call them by the class. Here is your code, modified:

        namespace Test
        {
        /// /// Description of TimeLimitOpen.
        ///
        public class TimeLimitOpenv02
        {

            \[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "?acedPostCommand@@YAHPBD@Z")\]
            extern static public int acedPostCommand(string cmd);
            private static string\[\] DwgList;//<---Modification
            public Document NewDoc;
            DocumentCollection DocMan = AcadApp.DocumentManager;
            System.Timers.Timer CountDownTimer;
            private static int cnt = 1; //<---Modification
            //- Constructor: sets up var's etc for this class before use.
            public TimeLimitOpenv02()
            {
                CountDownTimer = new System.Timers.Timer();
                CountDownTimer.Elapsed += new ElapsedEventHandler(CancelCommand);
                CountDownTimer.Interval = 10000;
            }
        
            ~TimeLimitOpenv02() // <-- Destructor, do clean up here.
            {
                CountDownTimer.Elapsed -= new ElapsedEventHandler(CancelCommand);
            }
            public void CancelCommand(object sender, ElapsedEventArgs e)
            {
                acedPostCommand ("CancelCmd");
                //MessageBox.Show (DocMan.ToString());
                NewDoc = DocMan.MdiActiveDocument;
                //MessageBox.Show ("Testing");
                //MessageBox.Show (cnt.ToString());
                NewDoc.CloseAndDiscard();
                NewDoc = DocMan.Open (DwgList\[cnt\], true);
                if (NewDoc != DocMan.MdiActiveDocument) {
                    DocMan.MdiActiveDocument = NewDoc;
                }
                ++TimeLimitOpenv02.cnt;//You access it by the class name
                if (TimeLimitOpenv02.cnt > TimeL
        
        T Offline
        T Offline
        T Willey
        wrote on last edited by
        #8

        It looks like the problem now is that it doesn't know how to get the current drawing NewDoc = DocMan.MdiActiveDocument;. Even if I put it in the long way NewDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;. It still won't be able to find it, and in turn execute the commands on it. Is the timer taking control away from Acad? Is that why it can't find it? If you have any ideas, I'm will to try them, as I'm totally lost now. Thanks.

        Tim

        N 1 Reply Last reply
        0
        • T T Willey

          It looks like the problem now is that it doesn't know how to get the current drawing NewDoc = DocMan.MdiActiveDocument;. Even if I put it in the long way NewDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;. It still won't be able to find it, and in turn execute the commands on it. Is the timer taking control away from Acad? Is that why it can't find it? If you have any ideas, I'm will to try them, as I'm totally lost now. Thanks.

          Tim

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #9

          Ooops!!:-> I missed that Docman and NewDoc too should be declared as static if you want to share them between main and CancelCommand. Don't forget to call them by the class name.

          Regards:rose:

          T 1 Reply Last reply
          0
          • N Nader Elshehabi

            Ooops!!:-> I missed that Docman and NewDoc too should be declared as static if you want to share them between main and CancelCommand. Don't forget to call them by the class name.

            Regards:rose:

            T Offline
            T Offline
            T Willey
            wrote on last edited by
            #10

            No problem. I made the DocMan static, but didn't think I need to make the NewDoc static also. After I did this, it was able to read the NewDoc, and I was able to get a property, but when I tried to use the method CloseAndDiscard() it crashed with and unhandled access issue. I will have to look into this tomorrow, as I'm going home, and don't have Acad there yet, so can't test anything. Thanks for all your help today Nader it is really appreciated!!

            Tim

            N 1 Reply Last reply
            0
            • T T Willey

              No problem. I made the DocMan static, but didn't think I need to make the NewDoc static also. After I did this, it was able to read the NewDoc, and I was able to get a property, but when I tried to use the method CloseAndDiscard() it crashed with and unhandled access issue. I will have to look into this tomorrow, as I'm going home, and don't have Acad there yet, so can't test anything. Thanks for all your help today Nader it is really appreciated!!

              Tim

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #11

              You're welcome. And if you need something tommorow, just reply and I'll get an email notification. Good luck with your demo.;)

              Regards:rose:

              T 1 Reply Last reply
              0
              • N Nader Elshehabi

                You're welcome. And if you need something tommorow, just reply and I'll get an email notification. Good luck with your demo.;)

                Regards:rose:

                T Offline
                T Offline
                T Willey
                wrote on last edited by
                #12

                I hate to have to ask you for somemore help, but I can't see to find an answer to what I thought would be a simple question. How can you set the focus to an application? I think this might be a problem with my code. I think I need to set the focus back to Autocad in the event handler portion. Thanks again. -- modified at 13:54 Friday 15th September, 2006 Nevermind. It doesn't seem to work that way. Back to the drawing board. I found a way on this website. http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=3&t=3[^]

                Tim

                N 1 Reply Last reply
                0
                • T T Willey

                  I hate to have to ask you for somemore help, but I can't see to find an answer to what I thought would be a simple question. How can you set the focus to an application? I think this might be a problem with my code. I think I need to set the focus back to Autocad in the event handler portion. Thanks again. -- modified at 13:54 Friday 15th September, 2006 Nevermind. It doesn't seem to work that way. Back to the drawing board. I found a way on this website. http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=3&t=3[^]

                  Tim

                  N Offline
                  N Offline
                  Nader Elshehabi
                  wrote on last edited by
                  #13

                  Hello Sorry I saw your post late. Here is the way -if you still want it-. First, you can't do that using C# alone. you have to make some Windows API unmanaged functions. The functions import code is:

                  [DllImport("User32.dll", CharSet=CharSet.Auto)]
                  private static extern bool SetForegroundWindow(IntPtr hWnd);

                  Now this loop will go through all top-level windows, check their title, then activate the one that has "Autocad" word on it:

                  foreach(Process P in Process.GetProcesses())
                  {
                  if (P.MainWindowTitle.Length > 0 && P.MainWindowTitle.IndexOf("Autocad") != -1)
                  SetForegroundWindow(P.MainWindowHandle);
                  }

                  One flow is that all windows that has "Autocad" word on it will be activated sequentily. If you know more word in a single phrase -ie. as a sentence- this should narrow this error margin.

                  Regards:rose:

                  T 1 Reply Last reply
                  0
                  • N Nader Elshehabi

                    Hello Sorry I saw your post late. Here is the way -if you still want it-. First, you can't do that using C# alone. you have to make some Windows API unmanaged functions. The functions import code is:

                    [DllImport("User32.dll", CharSet=CharSet.Auto)]
                    private static extern bool SetForegroundWindow(IntPtr hWnd);

                    Now this loop will go through all top-level windows, check their title, then activate the one that has "Autocad" word on it:

                    foreach(Process P in Process.GetProcesses())
                    {
                    if (P.MainWindowTitle.Length > 0 && P.MainWindowTitle.IndexOf("Autocad") != -1)
                    SetForegroundWindow(P.MainWindowHandle);
                    }

                    One flow is that all windows that has "Autocad" word on it will be activated sequentily. If you know more word in a single phrase -ie. as a sentence- this should narrow this error margin.

                    Regards:rose:

                    T Offline
                    T Offline
                    T Willey
                    wrote on last edited by
                    #14

                    Thank you for the code. I think I need to submit it to the Autocad people (they have a network of developers that help others) because I think I have gotten as far as I can with just your great C# help. I think it is an Autocad thing now, and if you don't use it (code for it, like it looks like you don't) then I don't think one would know how to fix it so that it will work. Thanks again for your patience, and help. It's greatly appreciated (hope your not sick of reading this type of stuff, because it is sincere). If you want I will post the code that does work, sometimes people just like to see what was wrong. Who knows.

                    Tim

                    N 1 Reply Last reply
                    0
                    • T T Willey

                      Thank you for the code. I think I need to submit it to the Autocad people (they have a network of developers that help others) because I think I have gotten as far as I can with just your great C# help. I think it is an Autocad thing now, and if you don't use it (code for it, like it looks like you don't) then I don't think one would know how to fix it so that it will work. Thanks again for your patience, and help. It's greatly appreciated (hope your not sick of reading this type of stuff, because it is sincere). If you want I will post the code that does work, sometimes people just like to see what was wrong. Who knows.

                      Tim

                      N Offline
                      N Offline
                      Nader Elshehabi
                      wrote on last edited by
                      #15

                      Hello Thank you for your kind complements. I'm always happy to help you anytime. What I got from your post is that your code didn't work out!! Why?? I though it did display that orbit thing, and all your problem was with the timer?? Sorry I don't have knowledge of Autocad, but I hope I helped you enough with the C# part!! Anyway, if you still want to work out your code, just tell me what a non-autocad problem -if any- would be. If you found a working code, I'd be happy to see it. Anyway, I wish you luck.

                      Regards:rose:

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups