Results 1 to 5 of 5

Thread: Looking for a better way to do this

  1. #1

    Thread Starter
    New Member rodan_13's Avatar
    Join Date
    Feb 2012
    Location
    Dallas, TX area
    Posts
    2

    Looking for a better way to do this

    First let me state that I have not used Visual Basic for around 3 years...and that was VB6. I am rusty with the language since I've been focusing on SQL since that time, but my current position requires that I get involved with general development again. I've worked on exactly 1 project since I began working with VB again, and while I can do it the code is ugly.

    The code in question is in place, and it works. The project is all but wrapping up. That being said, I would love to find a better way to run this section of code in an x64 system so I have the solution ready for future projects. What I currently have in place is as follows:

    Code:
    Function TimeDelay(vDelay)
    Dim vTimmer, vTargetTime
    
    vTimmer = TimeValue(Now)
    vTargetTime = DateAdd(DateInterval.Second, vDelay, TimeValue(Now))
    
    Do While vTimmer <= vTargetTime
      vTimmer = TimeValue(Now)
    Loop
    
    End Function
    Now I know the easy way to achieve my goal would be to use the timer object in Visual Studio. The problem I ran into is I was developing an automated command line program. As such there was nothing I could attach the timer to (that I'm aware of). I just needed to pause the program for a few seconds to allow the DTSX package (SSIS file if you're not familiar) to load the file before I remove it from the staging folder.

    While I was doing research for this issue I came across the old Sleep() function (referenced to a library in kernel32 as I understand it...never used this before either). When I tried to use this on my current system (64-bit system) I received stack overflow errors. Additional research lead to something called wow64, but I did not have enough time to fully research what this is / how to use it. Any thoughts?

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Looking for a better way to do this

    You definitely need a different solution than what you have. What you are doing is a busy wait, which has horrible implications. Threading.Sleep is one option, but if you call that on the UI thread, it will block the thread (no user interactions) for the duration.

    A timer is almost certainly the right way to go. I don't know what you mean about not having anything to attach it to. Do you mean that there isn't a form for the timer to be dropped on? If that is the case, it isn't necessary to have a form. You can use a Windows.Forms.Timer in any class just by declaring it the way you would any other variable (except that you have to declare it WithEvents to get the tick event). Of course, you can't double click on it and have a event handler stub built for you automatically the way you would if it was on a form, but that's pretty minor. Just write the event handler stub yourself, and use AddHandler to tell the timer which method to call when the event is raised.

    Alternatively, you could use the Threading.Timer, which is kind of a different animal, and seems unlikely to be ideal in your case, but it might be worth a look.
    My usual boring signature: Nothing

  3. #3
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Looking for a better way to do this

    dont bother with wow32 it is going to be gone real soon!

    the implications of busy waiting are always talked about here...

    you need to be able to know that some thing has happened before you delete the file... do you really need to delete it while the program is running?

    can you not delete it at some arbitary point int eh process when you are 100&#37; sure it is not needed?

    if you need the name for an output, why not use another and do a "kill swap" file at the end of the process?

    working in the dark here as i really do not know what your re actually doing!

    here to help

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Looking for a better way to do this

    Welcome to the forum!

    The loop you have is CPU intensive - that's not a proper solution.

    Threading.Thread.Sleep(x) where x is ms's to sleep is probably best - as you said this is a console application - so having it pause (and appear frozen) should not matter.

    I just did something similar in a web service - and did a lot of research before feeling comfortable with my solution...

    Look at post #13 in this thread

    http://www.vbforums.com/showthread.php?t=671789

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    New Member rodan_13's Avatar
    Join Date
    Feb 2012
    Location
    Dallas, TX area
    Posts
    2

    Re: Looking for a better way to do this

    Thank you for the suggestions so far. To help understand the overall goal check out my post on another forum. That also has the entire program attached in .txt format.

    http://www.sqlservercentral.com/Foru...2753-23-1.aspx

    As for the suggestions given I'm going to play with them to see if I can learn how they work. As you'll see in the other forum's post, I am still VERY rusty in my VB skills.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width