|
I'm wondering why the sleep command is there? If you are just building a string, then do you need the sleep?
Also, reporting progress - I would try cut that down a lot! Perhaps using:
if (i mod 255)=0 then
worker.ReportProgress(i)
end if
This will report progress a lot less often, so fewer thread switches etc.
|
|
|
|
|
Hi Jasey9,
I have a few other threads running and the sleep(1) is just there to force the scheduler to switch threads so this one doesn't hog up all the execution time. Give other threads a chance so to speak.
Thanks I'll try your suggestion.
|
|
|
|
|
frylord wrote: System.Threading.Thread.Sleep(1)
Might be the culprit. I understand you may need a delay to the device, but during each iteration?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
Hi Paul,
It's not the sleep(1). The reason I know this is at first I expected this process to complete within a "reasonable" (not letting the user think the GUI got stuck) amount of time so I did this process under a button. Then when I noticed it's taking a lot of time to complete I put it in a thread with a nice GUI and such.
The sleep(1) is just there to force the scheduler to switch threads to give other threads some execution time as well.
Thanks for the effort.
|
|
|
|
|
frylord wrote: sleep(1) is just there to force the scheduler to switch threads to give other threads some execution time as well.
Looking at what Luc said, it makes sense, and the other poster's idea of using the mod might help.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
frylord wrote: The sleep(1) is just there to force the scheduler to switch threads to give other threads some execution time as well.
That does not make sense to me. Either the code is running in the GUI thread, and Sleep is a no-no;
or it runs in another thread, and you should put Sleep calls when they are necessary. You don't need
to help Windows, Windows will reschedule threads when it feels like it, even when
they don't cooperate. You don't expect a single while(1){} to hog the CPU for 100% and
forever, do you? (I am not implying you should do such things, just saying Windows does not care
about the meaning of your code).
Just pointing out Windows will switch away from any thread, in favor of another thread:
- with higher priority
- with same priority, out of fairness concerns
- and even occasionally with lower priority, to maybe escape from an accidental deadlock situation.
BTW: the last paragraph applies more to desktop versions, and less to server versions of Windows.
|
|
|
|
|
Well to be honest I don't know. Somehow my college teachers indoctrinated the idea that you should block your current thread so other threads can get some execution time aswel. The same idea continued into my university years and has sticked with me ever since and I've also seen the same in alot of online tutorials and examples. I think this idea of blocking one thread to give others a chance could be left over from the days when schedulers weren't as complex.
|
|
|
|
|
Hi,
if you have a lot of work that needs to be done but is not urgent, then you should organize things
in such a way that other, possibly more urgent things, can be done first. This is accomplished
either by using a thread with lower priority (recommended when available, it is on Windows),
or by "throttling" yourself, i.e. forcing the OS to try and do something else from time to time.
Now giving away the CPU after copying 8 characters is not a good idea, since a thread switch would
cost anywhere between 10 and 1000 times as much as the work you are doing. So the reduction by
255 as suggested by one reply is appropriate.
But as I said before, it only takes some 40 msec althogether, so don't bother; just don't do it
in your own GUI thread!
BTW: GUI threads have a slightly higher priority than other threads at "normal priority" anyway.
And yes, it would be different on simpler operating systems, as well as on real-time environments,
as could be found in embedded systems.
|
|
|
|
|
Hi,
I have read your message again, and feel a need to add some:
- I am not sure the elapsed time is caused by the code you have shown, it merely loads a StringBuilder,
it does not send anything to your external device;
- if your connection is say a serial port (you did not specify) then it is operating at some baud rate;
when we assume 115200 Bd then that means a maximum of some 13K characters per second; so your
500K characters would require 35 seconds at least. So the transmission itself could be the bottleneck.
- if your (serial?) port uses some kind of handshaking (you did not specify), then maybe
it is the device itself that can't run any faster, and is telling the PC to slow down. In this case
the target device is the bottleneck.
If any of the above applies, it becomes unclear why you feel the need to first build a huge
string. You might consider outputting the numbers one by one, or a few KB at a time.
Finally, are you sure you need the comma-space combination? If one of those characters is redundant,
it takes one-eight of the bandwidth (that is 5 seconds in your case). And if you see a chance to
turn the assumed serial port to 7-bit instead of 8-bit that would shave of another 5 seconds or so
(this requires both sides to agree on it). Also, maybe the device does not need the 0.0000 format;
if it does not care about trailing zeroes, why send them?
|
|
|
|
|
Hi Luc,
Once again thank you.
The device is connected through a TCP/IP connection.
The thing is that the SDK doesn't allow for sending the data one by one, you need to send it in one line. So what I do is build the string I need to send using string builder and then afterwards send it using the SDK function and the stringbuilder ToString function.
I think I'll have to explain a bit more about the device, atleast I owe you that much I guess. The so called "device" is a arbitrary waveform generator. here's an excerpt from the manual.
You can download from 1 point (a dc signal) to 65,536 (64K) points per
waveform. You can download the points as floating-point values. Use the SendArbData function to
download floating-point values from -1.0 to +1.0.
And here's the bit on how the data should be.
The following statement shows how to use the SendArbData function to
download seven points to volatile memory.
SendArbData("1, .67, .33, 0, -.33, -.67, -1")
So basically it wants me to format a huge string of 65536 data points in CSV format and then use the SendArbData function to send it to the device.
|
|
|
|
|
OK,
so for N float numbers:
- use StringBuilder, give it initial capacity of 7*N
- fill it with your floats and try to keep it as short as possible, hence:
- try if it works without the space
- do some formatting (to avoid stupid decimals as in 0.3333333333333333(
- don't provide unnecessary resolution (if 0.00 is sufficient, don't use 0.0000)
- chop off trailing zeroes
- don't put sleeps, GUI stuff, etc in the fill loop
- update something on the GUI when the StringBuilder is filled
- give it to SendArbDate
- update something on the GUI when that method returns
- don't worry about some extra code in the fill loop, as long as it is not sleep, thread related
or GUI related.
I ran a simple filler for a megabyte stringbuilder; it took less than 50 msec, so that is
definitely not where the problem is.
You still haven't told much about the interface though; TCP/IP may or may not indicate Ethernet;
you can run it on a serial port too!
Warning: the device or its library may dislike a terminating ", " so strip it off before turning
StringBuilder into string.
Final remark: if either the library or the target device does a long computation (such as a
Fast Fourier Transform, which isn't always fast!) SendArbData might not return for a long time.
|
|
|
|
|
The manual isn't really clear on this but by trying I figured out that the device needs 4 decimals after the comma to get an accurate output.
For example when I set the Vmax to 9 volts and I want it to output 3 volts (1/3) then I need to set the data point to 0.3333333333333333333 if I had infinite accuracy. But just by trying I found out that by setting the data point to 0.33 i gives me 2.7 volts and when I set it to 0.3333 it gives me 3.01 volts.
I think you might be onto something with the GUI thing.
To be honest my first version wasn't with stringbuilder I think. When I first wrote the code I put it under a button using normal string. Then when I found that was too slow I built the thread thing then later on I put stringbuilder in there.
Okay after some trying I found that the culprit in this case was the progressbar which was being updated every loop. When I just use the stringbuilder it's quite fast.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
Dim Rnd As Random = New Random()<br />
Dim Dbl_RndData(65535) As Double<br />
Dim Str_RndData As StringBuilder<br />
Dim i As Integer = 0<br />
<br />
Str_RndData = New StringBuilder()<br />
<br />
' Fill the Double array with random data<br />
For i = 0 To 65535<br />
Dbl_RndData(i) = Rnd.NextDouble<br />
Next<br />
<br />
' Convert the data to a string using stringbuilder<br />
For i = 0 To 65535<br />
Str_RndData.Append(Dbl_RndData(i).ToString("0.0000"))<br />
Str_RndData.Append(", ") ' Append a comma to get CSV format<br />
Next<br />
End Sub
Thanks for the help.
|
|
|
|
|
You're welcome.
Just out of curiosity, how fast is it now?
|
|
|
|
|
Using this code:
Dim Rnd As Random = New Random()<br />
Dim Dbl_RndData(65535) As Double<br />
Dim Str_RndData As StringBuilder<br />
Dim i As Integer = 0<br />
Dim StpWtch As Stopwatch = New Stopwatch<br />
<br />
<br />
Str_RndData = New StringBuilder()<br />
<br />
' Fill the Double array with random data<br />
For i = 0 To 65535<br />
Dbl_RndData(i) = Rnd.NextDouble<br />
Next<br />
<br />
StpWtch.Start()<br />
' Convert the data to a string using stringbuilder<br />
For i = 0 To 65535<br />
Str_RndData.Append(Dbl_RndData(i).ToString("0.0000"))<br />
Str_RndData.Append(", ") ' Append a comma to get CSV format<br />
Next<br />
StpWtch.Done()<br />
<br />
TextBox1.Text = StpWtch.ElapsedTime.ToString
And this Stopwatch counter:
http://www.codeproject.com/KB/vb/vbnetstopwatch.aspx[^]
I get 0.196240863014713 seconds
|
|
|
|
|
I am retrieving data from database with list Box control. I like to show different items are different color depending upon condition. For example if I presenting student name in list box, when populating all student name, the student name who passed show in green color and who failed in red color. Please provide help in vb.bet.
|
|
|
|
|
Hi,
This article[^] demonstrates the nice things a ListBox can do. It is C# code, but the same applies to VB.NET
In essence, the listed items don't have to be strings, they could be any kind of object;
and a ListBox supports user-drawing, so you decide how things look.
|
|
|
|
|
HowI can hide the selction of a button disabled after then mouse is outside it? (VB.net)
thanks
|
|
|
|
|
I am not quite sure what you are asking.
Are you referring to: changing focus, hiding the button?
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my homepage Oracle Studios
Discounted or Free Software for Students:
DreamSpark - downloads.channel8.msdn.com
MSDN Academic Alliance - www.msdnaa.com
|
|
|
|
|
Hi,
Is there a good instruction how to create on add in and install it on someone else his/here pc. About 20 persons will use the same add in. So where must i copy it? How do I activate it? and so on.
some examples are apriciated
Jan
|
|
|
|
|
Hi Jan,
Tye to use VSTO (Visual Studio Tools for Office) for VS 2008. This provides many features to developers to deal with Office application development.
Hope this helps .
Regards,
John Adams
ComponentOne LLC
|
|
|
|
|
|
Hiii
I m working on outlook Addins using vb.net 2005 and i want to select the profile (Sender User from ) My outlook.
Like
I Have 4 Profiles in outlook and when i send the mail i want to select the sender user in outlook.
I am using a custom windows form for sending mail. not a default outlook mail editor.
Please suggest how i select the profile.
Thanx
~Khatri Mitesh
|
|
|
|
|
Hi,
I create a simple program that can open a image in excel. But there is a problem, I want it to position a little bit lower because I have some data that i want to write above that image. And also how do I re size the image that i put in the excel?
Here is my code:
Dim xlsApp As New Microsoft.Office.Interop.Excel.Application
Dim xlsWSheet As New Microsoft.Office.Interop.Excel.Worksheet
Dim excelFiles As String = "c:\Test.xlsx"
Dim pic As String = "c:\MsC.bmp"
xlsApp.Visible = True
xlsApp.Workbooks.Open(excelFiles)
xlsApp.ActiveSheet.Pictures.Insert(pic)
xlsApp = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
Anyone change help me how to do the position of image.
Thanks
Regards,
Jessie
if(you type your code here) {
Messagebox.Show("You help me a lot!");
}
else {
You help me = null;
}
modified on Monday, July 28, 2008 4:55 AM
|
|
|
|
|
hi all
when i open at a time more then 5 report in crystal report by vb 6.0 then it give's error which error discreption below read it.
Run-time error '-2147189547'(80047cd5)
there are not enough concurrent access licenses to log you on. This system has 5 concurrent access license. Please try again later, or contact your system administrator to obtain more licenses. Licenses can be purchased direct from Seagate Software or through the Segate Software Online Store.
but i don't want to purchase, I want trap this error in vb 6.0 & give it only our msg for the same.
If any one know this solution then replay me if possible the replay with source code.
Thanx
|
|
|
|
|
does vb6 not have try/catch ?
Christian Graus
No longer a Microsoft MVP, but still happy to answer your questions.
|
|
|
|