473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BackgroundWorker.ReportProgress() Output Issue

Hello all,

I have an app that uses a BackgroundWorker() object to read USB data
that is being fed in serially (FT245R chip, www.ftdichip.com). It
looks just like a serial data stream. Anyhow, it reads in the bytes
and finds the header and footer and pulls packets out of the data
stream. Nice, easy stuff. It then stuffs the packet into into a
class I made up to pull the data out through the ProgressChanged
event. The class is pretty simple, consisting of the following:

Public Class WorkerReturn
Public FullPacketBytes, PayloadSize As Integer
Public FullPacket(256) As Byte
Public Payload(256) As Byte

Public Sub WorkerReturn()
FullPacketBytes = 0
PayloadSize = 0
FullPacket.Initialize()
Payload.Initialize()
End Sub
End Class

So the BackgroundWorker passes the object out through the
ReportProgress method. This raises the ProgressChanged event:

Private Sub ReadUSBWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) _
Handles ReadUSBWorker.ProgressChanged

Dim WkrRtn As WorkerReturn
WkrRtn = e.UserState

ProcessPacketType(WkrRtn)
End Sub

So far so good. All the main logic code worked just dandy, but when I
tried outputting some debug data (sent through the USB stream) to a
TextBox I got "slurred" output, apparently because the textbox takes a
while to update & redraw.

My input stream is giving me test packets whose payload simply
increments in number from 0 to 63. I can debug inside the
BackgroundWorker, and it is seeing the input and processing it 100%
correctly. The output SHOULD look like:

" 1
2
3
4
5
6
7"

etc.. But it showed up as:

" 5
5
7
10
10
10
12
12"

etc. I finally figured out that even though it's passing the object
ByVal through the ProgressChanged event (ByVal e As
ProgressChangedEventArgs), which SHOULD be making a distinct copy of
the object each time it calls it, there is some kind of threading
issue going on here with the object. When I change the
ReportProgress() call from:

worker.ReportProgress(0, WkrRtn)

To:

worker.ReportProgress(0, WkrRtn)
Thread.Sleep(100)

The numbers show up sequentially, just like they should.

Obviously I don't want to leave a 100ms delay in there, and timing on
slower computers could be a real issue. Does this make sense? Need
to see more code maybe? Any ideas?

Thanks,
Ray

Jan 29 '07 #1
3 4165
I don't think ByVal is working the way you think it is. When used with
value types (numerics like integer and double, and structures), it makes a
copy and sends it in. When used with reference types (classes, objects,
strings, etc.), it makes a copy of the *reference* to the object and sends
in the reference, not a copy of the object.

Does this information help you?

Robin S.
-----------------------------------------------
<ra********@gmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
Hello all,

I have an app that uses a BackgroundWorker() object to read USB data
that is being fed in serially (FT245R chip, www.ftdichip.com). It
looks just like a serial data stream. Anyhow, it reads in the bytes
and finds the header and footer and pulls packets out of the data
stream. Nice, easy stuff. It then stuffs the packet into into a
class I made up to pull the data out through the ProgressChanged
event. The class is pretty simple, consisting of the following:

Public Class WorkerReturn
Public FullPacketBytes, PayloadSize As Integer
Public FullPacket(256) As Byte
Public Payload(256) As Byte

Public Sub WorkerReturn()
FullPacketBytes = 0
PayloadSize = 0
FullPacket.Initialize()
Payload.Initialize()
End Sub
End Class

So the BackgroundWorker passes the object out through the
ReportProgress method. This raises the ProgressChanged event:

Private Sub ReadUSBWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) _
Handles ReadUSBWorker.ProgressChanged

Dim WkrRtn As WorkerReturn
WkrRtn = e.UserState

ProcessPacketType(WkrRtn)
End Sub

So far so good. All the main logic code worked just dandy, but when I
tried outputting some debug data (sent through the USB stream) to a
TextBox I got "slurred" output, apparently because the textbox takes a
while to update & redraw.

My input stream is giving me test packets whose payload simply
increments in number from 0 to 63. I can debug inside the
BackgroundWorker, and it is seeing the input and processing it 100%
correctly. The output SHOULD look like:

" 1
2
3
4
5
6
7"

etc.. But it showed up as:

" 5
5
7
10
10
10
12
12"

etc. I finally figured out that even though it's passing the object
ByVal through the ProgressChanged event (ByVal e As
ProgressChangedEventArgs), which SHOULD be making a distinct copy of
the object each time it calls it, there is some kind of threading
issue going on here with the object. When I change the
ReportProgress() call from:

worker.ReportProgress(0, WkrRtn)

To:

worker.ReportProgress(0, WkrRtn)
Thread.Sleep(100)

The numbers show up sequentially, just like they should.

Obviously I don't want to leave a 100ms delay in there, and timing on
slower computers could be a real issue. Does this make sense? Need
to see more code maybe? Any ideas?

Thanks,
Ray

Jan 29 '07 #2
Hmm, yes that does help. It sounds like I need to implement a copy
function within the class and copy it to a new instance every time its
progress is reported.

On Jan 29, 12:53 am, "RobinS" <Rob...@NoSpam.yah.nonewrote:
I don't think ByVal is working the way you think it is. When used with
value types (numerics like integer and double, and structures), it makes a
copy and sends it in. When used with reference types (classes, objects,
strings, etc.), it makes a copy of the *reference* to the object and sends
in the reference, not a copy of the object.

Does this information help you?

Robin S.
-----------------------------------------------<ray.ack...@gmail.comwrote in messagenews:11*********************@a75g2000cwd.go oglegroups.com...
Hello all,
I have an app that uses a BackgroundWorker() object to read USB data
that is being fed in serially (FT245R chip,www.ftdichip.com). It
looks just like a serial data stream. Anyhow, it reads in the bytes
and finds the header and footer and pulls packets out of the data
stream. Nice, easy stuff. It then stuffs the packet into into a
class I made up to pull the data out through the ProgressChanged
event. The class is pretty simple, consisting of the following:
Public Class WorkerReturn
Public FullPacketBytes, PayloadSize As Integer
Public FullPacket(256) As Byte
Public Payload(256) As Byte
Public Sub WorkerReturn()
FullPacketBytes = 0
PayloadSize = 0
FullPacket.Initialize()
Payload.Initialize()
End Sub
End Class
So the BackgroundWorker passes the object out through the
ReportProgress method. This raises the ProgressChanged event:
Private Sub ReadUSBWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) _
Handles ReadUSBWorker.ProgressChanged
Dim WkrRtn As WorkerReturn
WkrRtn = e.UserState
ProcessPacketType(WkrRtn)
End Sub
So far so good. All the main logic code worked just dandy, but when I
tried outputting some debug data (sent through the USB stream) to a
TextBox I got "slurred" output, apparently because the textbox takes a
while to update & redraw.
My input stream is giving me test packets whose payload simply
increments in number from 0 to 63. I can debug inside the
BackgroundWorker, and it is seeing the input and processing it 100%
correctly. The output SHOULD look like:
" 1
2
3
4
5
6
7"
etc.. But it showed up as:
" 5
5
7
10
10
10
12
12"
etc. I finally figured out that even though it's passing the object
ByVal through the ProgressChanged event (ByVal e As
ProgressChangedEventArgs), which SHOULD be making a distinct copy of
the object each time it calls it, there is some kind of threading
issue going on here with the object. When I change the
ReportProgress() call from:
worker.ReportProgress(0, WkrRtn)
To:
worker.ReportProgress(0, WkrRtn)
Thread.Sleep(100)
The numbers show up sequentially, just like they should.
Obviously I don't want to leave a 100ms delay in there, and timing on
slower computers could be a real issue. Does this make sense? Need
to see more code maybe? Any ideas?
Thanks,
Ray
Jan 29 '07 #3
You're going to want to implement ICloneable. If you create a new instance
and try to manually copy the data from one to the other by doing
new.Product = old.Product, if the type is a reference type, it will copy
the reference. (I know, grrrrr.) If they are value types, it will be okay.
Check out ICloneable and the Memberwise method of clone.

Robin S.
----------------------------------
<ra********@gmail.comwrote in message
news:11**********************@a34g2000cwb.googlegr oups.com...
Hmm, yes that does help. It sounds like I need to implement a copy
function within the class and copy it to a new instance every time its
progress is reported.

On Jan 29, 12:53 am, "RobinS" <Rob...@NoSpam.yah.nonewrote:
>I don't think ByVal is working the way you think it is. When used with
value types (numerics like integer and double, and structures), it makes
a
copy and sends it in. When used with reference types (classes, objects,
strings, etc.), it makes a copy of the *reference* to the object and
sends
in the reference, not a copy of the object.

Does this information help you?

Robin S.
-----------------------------------------------<ray.ack...@gmail.com>
wrote in
messagenews:11*********************@a75g2000cwd.g ooglegroups.com...
Hello all,
I have an app that uses a BackgroundWorker() object to read USB data
that is being fed in serially (FT245R chip,www.ftdichip.com). It
looks just like a serial data stream. Anyhow, it reads in the bytes
and finds the header and footer and pulls packets out of the data
stream. Nice, easy stuff. It then stuffs the packet into into a
class I made up to pull the data out through the ProgressChanged
event. The class is pretty simple, consisting of the following:
Public Class WorkerReturn
Public FullPacketBytes, PayloadSize As Integer
Public FullPacket(256) As Byte
Public Payload(256) As Byte
Public Sub WorkerReturn()
FullPacketBytes = 0
PayloadSize = 0
FullPacket.Initialize()
Payload.Initialize()
End Sub
End Class
So the BackgroundWorker passes the object out through the
ReportProgress method. This raises the ProgressChanged event:
Private Sub ReadUSBWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) _
Handles ReadUSBWorker.ProgressChanged
Dim WkrRtn As WorkerReturn
WkrRtn = e.UserState
ProcessPacketType(WkrRtn)
End Sub
So far so good. All the main logic code worked just dandy, but when I
tried outputting some debug data (sent through the USB stream) to a
TextBox I got "slurred" output, apparently because the textbox takes a
while to update & redraw.
My input stream is giving me test packets whose payload simply
increments in number from 0 to 63. I can debug inside the
BackgroundWorker, and it is seeing the input and processing it 100%
correctly. The output SHOULD look like:
" 1
2
3
4
5
6
7"
etc.. But it showed up as:
" 5
5
7
10
10
10
12
12"
etc. I finally figured out that even though it's passing the object
ByVal through the ProgressChanged event (ByVal e As
ProgressChangedEventArgs), which SHOULD be making a distinct copy of
the object each time it calls it, there is some kind of threading
issue going on here with the object. When I change the
ReportProgress() call from:
worker.ReportProgress(0, WkrRtn)
To:
worker.ReportProgress(0, WkrRtn)
Thread.Sleep(100)
The numbers show up sequentially, just like they should.
Obviously I don't want to leave a 100ms delay in there, and timing on
slower computers could be a real issue. Does this make sense? Need
to see more code maybe? Any ideas?
Thanks,
Ray

Jan 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6153
by: Tim Anderson | last post by:
I've been experimenting with the BackgroundWorker class. As a test, I've built an application that searches for files matching a RegEx and populates a listbox with the results. My approach is to...
1
5338
by: Jakanapes | last post by:
Is it possible to suspend the background worker without cancelling it or letting it finish and restarting? My application runs in a loop and I'd like to have a pause button, but I'm having...
5
14099
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
3
3034
by: Hardy Wang | last post by:
Hi all, I am migrating a Windows Form application from .Net 1.1 to 2.0. I try to use BackgroundWorker object to handle a very lengthy process. I have a separated class to handle some very complex...
1
3214
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
14
6352
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to...
9
18003
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
3
4808
by: vulpes | last post by:
How do I implement the "incremental results" with a BackgroundWorker? Many pages, http://msdn2.microsoft.com/en-us/library/wewwczdw.aspx for example, tell that I should subclass the...
0
1855
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.