|
Hey I am trying to use System.Threading.Thread.Sleep() and I am not getting expected results.
I am trying to use it as a delay
turn RTS on<br />
System.Threading.Thread.Sleep(5);<br />
turn RTS off
I have an o-scope hooked to the RTS line and I would expect that the RTS should go on and off anytime over 5 milliseconds (due to windows timing issues), but I would not expect to see it take anytime less than 5 milliseconds.
However, I am seeing results anywhere from just under 1 millisecond up to 20 milliseconds. Again if it was just 4 milliseconds (because of the resolution, i thought maybe we could see something as long as it was greater than 4) to whatever, I would be fine with that, but it is returning in less than 4 milliseconds down to one millisecond even!
Just for giggles I set up this
<br />
Using System.Diagnostics;<br />
Using System.Threading;<br />
<br />
long startTime,finishTime,diff;<br />
float elapsed;<br />
<br />
for (int x = 1; x <=10 ; x++)<br />
{<br />
startTime = Stopwatch.GetTimeStamp();<br />
Thread.Sleep(x);<br />
finishTime = Stopwatch.GetTimeStamp();<br />
diff = finishTime - startTime; <br />
elapsed = diff / 10000; <br />
Console.Write(x + " > " + elapsed + "ms > " + diff);<br />
}
And am seeing very similar (and stragne) results.
Is this just a bug in the the sleep function or what? Is there another function that I can call instead? (Don't even think about writing that I should be polling---don't get me started--, I am working in an environment where milliseconds difference are noticable, polling would be such a time killer)
Thanks
Brandy
|
|
|
|
|
aei_totten wrote: Is this just a bug in the the sleep function or what?
I don't believe so. I believe it is documented to have inconsistent results for anything less that 55 mills. Granted I haven't looked at it for many years but I think that's what it said at one time. I also believe that are higher resolution timers[^] you should use for the type of requirements you have.
led mike
|
|
|
|
|
|
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Windows is not a real-time O/S. The resolution of those timers is not guaranteed and can vary from system to system, and even vary due to system load.
If you want to grab analog/digital data at a consistance rate, you'd have to use hardware dedicated to the task. This would be something like an Analog/Digital converter board or some other PCI/PCIx data acquisition board that comes with an API to do the data capture/buffering independant of the system CPU.
|
|
|
|
|
aei_totten wrote: Thread.Sleep() not working
Of course, it is Thread.Sleep(), it isn't Thread.Work().
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
Hi,
I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as
Form2 frm = New Form2();
But I just cannot get Form2 on that Class
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Different namespace? Different project? Form2 already compiled?
|
|
|
|
|
Form2 is already Added to the Project.
Code file Global.cs and Namespace Globals
And in that file I want a common procedure that I can use on any form in my App.
Hope this helps.
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Are the Global class and the Form2 class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.
Need more info.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
Thanks Scott, I had a spelling mistake in namespace
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
|
|
|
|
|
Out of curiosity, which IDE/editor are you using?
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
carbon_golem wrote: Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.
Why is this bad?, Because of hackers etc? or...
Regards,
Gareth.
|
|
|
|
|
gareth111 wrote: Why is this bad?, Because of hackers etc? or...
No, not because of hackers. You don't do this because it's horribly bad design. There might be people comming in behind you that get stuck maintaining your code, and, after seeing this, might want to hunt you down and throw you into a vat of boiling lead.
|
|
|
|
|
I should have given a reason for that...
OOP has a few important best-practices guidelines that usually appears in the beginning of text books that sadly are forgotten by the end of them.
Objects should hide data, encapsulate data, and ensure data integrity to name a few. Objects serve a function of providing a structure around important data and work with other Objects to support a few inter-object relationships. (Is-A:association and Has-A:inheritance) Objects should also be coherent meaning if your problem calls for a Person and Pet, you would want to construct Person and Pet classes, and not put them into one 'God Object'. Global objects are not resistant to change either.
I hope you're starting to see the reasoning for this. If you have a library with Person, and Pet classes, you don't have to write them again for one, but now the program is coherent when read.
Tomes upon tomes have been written on the subject. I have given you a barely-adequate explanation, so I strongly recommend getting a book on object oriented programming, it's not just a bunch of fluff, it will save you effort bigtime.
Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
|
|
|
|
|
If you're doing this, it's an indication of bad design, violating OOP rules for encapsulation. Why would your "Global" class need to know anything about a form, or the controls on it? Hint: In proper design - the "Global" class shouldn't know or care about anything else outside of itself.
If you want to send data and status information back to Form2, the Globals class should expose events or delegates that an instance of Form2 should subscribe to to get notification of these things.
|
|
|
|
|
Thanks for that Dave.
Regards,
Gareth.
|
|
|
|
|
"Allow service to interact with Desktop"?
What does this mean??
I want to start an application through a windows service (say notepad).. So when I check this option (Allow service to interact with Desktop), I am able to start it, otherwise not..
Does it mean that for GUI application, this option should be checked? What If i want to run a console application? Will checking this option let my console application run or not??
Why is that? And also, how can I programmaticaly check this option for the service that I am creating??
Is it bad for services to interact with the desktop??
Som
|
|
|
|
|
novice1115 wrote: "Allow service to interact with Desktop"?
What does this mean??
In simple terms, Windows Services run under a seperate, non-visible desktop. Checking this option allows the service to interact with the users visible desktop instead of the normal hidden service desktop. Screen Savers run under another seperate desktop, and so does the UI for unlocking the workstation when you hit Ctrl-Alt-Del.
novice1115 wrote: I want to start an application through a windows service (say notepad).. So when I check this option (Allow service to interact with Desktop), I am able to start it, otherwise not..
Of course. If this option is not checked, then Notepad will still launch, but it's window will show up on the service desktop, which you can't see.
novice1115 wrote: Does it mean that for GUI application, this option should be checked? What If i want to run a console application? Will checking this option let my console application run or not??
If the option is checked or not, the process you launch, no matter what type it is, will still launch. It's just a matter of under which desktop the window is going to show up. If this option is not checked, the user will never wee the new application's windows, even if it's a console app.
novice1115 wrote: Is it bad for services to interact with the desktop??
Generally, yes, it's bad practice. Think about it. What if there's no user logged in??? How are you going to tell?? How are you going to handle this situation?? What if the user logs out during an operation your service is performing?? There are just a few of the questions you have to ask yourself if you are going to write an interactive service.
|
|
|
|
|
Thanks a lot for clearing my doubts..
|
|
|
|
|
Hello Experts!
Well, I am in a newer predicament lately. This, after I managed to get some help on customising a listview control.
<a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3089588&SiteID=1"><a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3089588&SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3089588&SiteID=1</a></a>[<a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=3089588&SiteID=1" target="_blank" title="New Window">^</a>]
The problem I am facing now is that i try to scroll the customised scroll I am facing a StackOverflow Exception.
This is how I am trying to communicate the scroll across
//starts here
if (e.Orientation == ScrollOrientation.VerticalScroll)
{
if (lo == 5)
{
if (SetScrollPos(this.listViewEx2.Handle, SBS_VERT, hi, true) != 0)
{
SendMessage(this.listViewEx2.Handle, WM_VSCROLL, (IntPtr)(4 + 0x10000 * hi), IntPtr.Zero);
}
}
else
{
SendMessage(this.listViewEx2.Handle, WM_VSCROLL, e.WParam, IntPtr.Zero);
}
}
if (e.Orientation == ScrollOrientation.HorizontalScroll)
{
if (lo == 5)
{
SetScrollPos(this.listViewEx2.Handle, SBS_HORZ, hi, true);
SendMessage(this.listViewEx2.Handle, WM_HSCROLL, (IntPtr)(4 + 0x10000 * hi), IntPtr.Zero);
}
else
{
SendMessage(this.listViewEx2.Handle, WM_HSCROLL, e.WParam, IntPtr.Zero);
}
}
//ends here
Someone please help me! All I have is one last day at hand!
-
Roger
|
|
|
|
|
This code runs in your scroll event handler ? That will do it, you send a message to scroll, then it fires an event, then it sends a message, etc, until the stack overflows
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Oh! You mean me's actually performing a loop!
What way do i not loop that? What i need to perform here is when ListView1 is scrolled, ListView2 also gets scrolled to the same level!
|
|
|
|
|
Fixed it!
All it needed was a PostMessage() instead of a SendMessage()!
Thanks guys!
|
|
|
|
|
Dear all,
I have to use a paretrized function as example sum(x,y),
i dont know how to use this piece of code for such function can anybody help me here..
ThreadStart job = new ThreadStart(sum);//what to do for parametrized functions
Thread thread = new Thread(job);
thread.Start();
please help me here...
Thanks in advance...
vikas da
|
|
|
|
|