Multiple event Args in VB 2005

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
cmdolcet69 said:
Can anyone tell me if its possible to do multiple event args in
vb.net?

Is "multiple event args" a term I should know? If not, what's it's meaning?


Armin
 
Yes you can

if you mean with that a event with a argument signature of (n) elements

like this for instance

Public Event eHelloWorld(ByVal a As Date, ByVal b As String, ByVal c As
Integer, ByVal d As Object, ByVal e As System.EventArgs)


just declare it with the wanted arguments this can be value types or
reference types


hth

Michel Posseth [MCP]
 
Yes you can

if you mean with that a event with a argument signature of (n) elements

like this for instance

Public Event eHelloWorld(ByVal a As Date, ByVal b As String, ByVal c As
Integer, ByVal d As Object, ByVal e As System.EventArgs)

just declare it with the wanted arguments this can be value types or
reference types

hth

Michel Posseth [MCP]

cmdolcet69 said:
Can anyone tell me if its possible to do multiple event args in vb.net?

Ok so the sub StartWorkerThreadDualDSI1 is suppose to create (2) com
ports so that I can read reading from (2) sources at the same
time….currently this only reads one source at a time…. On the bottom
of the sub I create the two COM Ports currentserial and currentserial2
then it goes into the DoDualDSIWorkCOM! Sub and passing in only the e
argument for the first COM port my question is how can I pass a second
e argument into the DoDualDSIWorkCOM1? Also while in that sub it
calls the sub GetDSIInputCOM1 the serialport is only being passed COM1
and never COM 2 how can I get both COM’s to be passed in?????


Public Sub StartWorkerThreadDualDSI1(ByVal gageType As String)
Dim expectedBaudRate As String = "57600"
Select Case gageType
Case "DSI"
AddHandler dataBackgroundWorker.DoWork, AddressOf
DoDualDSIWorkCOM1
AddHandler dataBackgroundWorker.RunWorkerCompleted,
AddressOf DualDSIWorkComplete
expectedBaudRate = "9600"

End Select
currentSerial = GetSerialPortUsed(1)
currentSerial2 = GetSerialPortUsedCOM2(2)
ChangeCOMBaudRateIfNecessary(expectedBaudRate)
dataBackgroundWorker.RunWorkerAsync(New Object()
(currentSerial})
dataBackgroundWorker2.RunWorkerAsync(New Object()
{currentSerial2})
Me.tmrBackgroundWorker.Enabled = True
Me.tmrBackgroundWorker2.Enabled = True




Private Sub DoDualDSIWorkCOM1(ByVal sender As Object, ByVal e As
DoWorkEventArgs)
Try
Dim args() As Object = e.Argument
Dim intloop As Integer
CType(args(0), IO.Ports.SerialPort).DiscardInBuffer()
muxClass.GetDualDSIInputCOM1(CType(args(0),
IO.Ports.SerialPort))
Catch ex As Exception
tListener.AddMethodError(ex)
End Try
End Sub



Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
Dim intloop As Integer
Try
serialPort.ReadTimeout = 500
serialPort.DiscardInBuffer()

Dim strDataCOM1 As String = String.Empty
If Not serialPort.IsOpen Then
strDataCOM1 = String.Empty
Else
strDataCOM1 = serialPort.ReadLine
End If
If strDataCOM1.Length > 0 Then
COM1Active = 1
Dim tempArray() As String
strDataCOM1 = strDataCOM1.Replace(vbCrLf,
vbTab).Replace(Chr(26), "").Replace(Chr(12), "").Replace(Chr(13), "")
'parses out the information gathered from the COM port
to a temp location
tempArray = Split(strDataCOM1, vbTab)

If serialPort.BaudRate = 9600 Then
DSICollector = "585 Plus"
Get585PlusData(tempArray)
Else
DSICollector = "501/440"
Get501Data(tempArray)
End If
End If

Catch tex As TimeoutException
'do nothing as this will happen while waiting
Catch ioError As System.IO.IOException
'do nothing as this will occur sometimes when closing the
thread
Catch ex As Exception
tListener.AddMethodError(ex)
End Try
End Sub
 
cmdolcet69 said:
AddHandler dataBackgroundWorker.DoWork, AddressOf
DoDualDSIWorkCOM1

In the code you posted, I don't see where you add the handler to
dataBackgroundWorker2.DoWork:

AddHandler dataBackgroundWorker2.DoWork, AddressOf DoDualDSIWorkCOM1

Can this be the actual problem? A general answer to your question: If the
event is defined not by yourself, you can not change the number of
arguments. The event handler must have the same number of arguments.



Armin
 
In the code you posted, I don't see where you add the handler to
dataBackgroundWorker2.DoWork:

AddHandler dataBackgroundWorker2.DoWork, AddressOf DoDualDSIWorkCOM1

Can this be the actual problem? A general answer to your question: If the
event is defined not by yourself, you can not change the number of
arguments. The event handler must have the same number of arguments.

Armin

Ok sorry i did make a mistake in the first sub and have now added the
handler to dataBackgroundWorker2 however how do i add this to the
DodualDSIWorkerCOM1 Sub ? how can I pass in the values of the second
databackgroundworker
?
 
In the code you posted, I don't see where you add the handler to
dataBackgroundWorker2.DoWork:

AddHandler dataBackgroundWorker2.DoWork, AddressOf DoDualDSIWorkCOM1

Can this be the actual problem? A general answer to your question: If the
event is defined not by yourself, you can not change the number of
arguments. The event handler must have the same number of arguments.

Armin
Ok sorry i did make a mistake in the first sub and have now added the
handler to dataBackgroundWorker2 however how do i add this to the
DodualDSIWorkerCOM1 Sub ? how can I pass in the values of the second
databackgroundworker
?


You wrote:

dataBackgroundWorker.RunWorkerAsync(New Object() (currentSerial})
dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2})

You already use the first port with the first BGW and the second port with
the second BGW. Consequently, inside DoDualDSIWorkCOM1, args(0) is the first
or the second port. It is the first port if the method ist executed in the
1st BGW and the 2nd port if executed in the 2nd BGW. So, I think there is no
problem (in this part).


Armin
 
You wrote:

dataBackgroundWorker.RunWorkerAsync(New Object() (currentSerial})
dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2})

You already use the first port with the first BGW and the second port with
the second BGW. Consequently, inside DoDualDSIWorkCOM1, args(0) is the first
or the second port. It is the first port if the method ist executed in the
1st BGW and the 2nd port if executed in the 2nd BGW. So, I think there isno
problem (in this part).

Armin

ok then how can i have them both running at the same time meaning that
in the sub below: my serialPort is always COM port 1 I need to ahve
COm 1 and COM 2 be open and reading data at the same time.... Is this
possible and does this make sense?

Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
Dim intloop As Integer
Try
serialPort.ReadTimeout = 500
serialPort.DiscardInBuffer()

Dim strDataCOM1 As String = String.Empty
If Not serialPort.IsOpen Then
strDataCOM1 = String.Empty
Else
strDataCOM1 = serialPort.ReadLine
End If
If strDataCOM1.Length > 0 Then
COM1Active = 1
Dim tempArray() As String
strDataCOM1 = strDataCOM1.Replace(vbCrLf,
vbTab).Replace(Chr(26), "").Replace(Chr(12), "").Replace(Chr(13), "")
'parses out the information gathered from the COM port
to a temp location
tempArray = Split(strDataCOM1, vbTab)

If serialPort.BaudRate = 9600 Then
DSICollector = "585 Plus"
Get585PlusData(tempArray)
Else
DSICollector = "501/440"
Get501Data(tempArray)
End If
End If

Catch tex As TimeoutException
'do nothing as this will happen while waiting
Catch ioError As System.IO.IOException
'do nothing as this will occur sometimes when closing the
thread
Catch ex As Exception
tListener.AddMethodError(ex)
End Try
End Sub
 
cmdolcet69 said:
ok then how can i have them both running at the same time meaning
that in the sub below: my serialPort is always COM port 1 I need to
ahve
COm 1 and COM 2 be open and reading data at the same time.... Is
this possible and does this make sense?

Yes, it makes sense. You are already creating two BGWs (background workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.

The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).

How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretends to be
COM1 but it isn't always.


Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
COM1Active = 1


Armin
 
ok then how can i have them both running at the same time meaning
that in the sub below: my serialPort is always COM port 1 I need to
ahve
COm 1 and COM 2 be open and reading data at the same time.... Is
this possible and does this make sense?

Yes, it makes sense. You are already creating two BGWs (background workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.

The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).

How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretends to be
COM1 but it isn't always.
Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
                COM1Active = 1

Armin

I see it when i put a watch on the SerialPort variable........what i
want to do is take be able to take both serial port data at the same
time but when I go into the GetDualDSIInputCOM1 it always seems to be
passing in COM1 and not COM1 and COM2 at the same time

Can you help very urgent!!!!
 
cmdolcet69 said:
Yes, it makes sense. You are already creating two BGWs (background
workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.

The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).

How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretends to
be
COM1 but it isn't always.
Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
COM1Active = 1

Armin

I see it when i put a watch on the SerialPort variable........

And then? How do you see the difference? Do they have different property
values that are unique to each instance?
what i
want to do is take be able to take both serial port data at the same
time but when I go into the GetDualDSIInputCOM1 it always seems to be
passing in COM1 and not COM1 and COM2 at the same time

Can you help very urgent!!!!

The two threads do run at the same time. GetDualDSIInputCOM1 is executed
twice, one time in each thread. In thread 1 it handles port 1 and in thread
2 it handles port 2. That's how I expect it to work. I don't see an error in
this regard.

Try this:

In StartWorkerThreadDualDSI1:

dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial,"port
1"})
dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2,"port
2"})
....


In DoDualDSIWorkCOM1:

Try
Dim args() As Object = e.Argument
Debug.Print(args(1).ToString)
...

Run it without interrupting or setting breakpoints. Do you only get "port 1"
in the debug/output window?


Armin
 
Yes, it makes sense. You are already creating two BGWs (background
workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.
The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).
How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretendsto
be
COM1 but it isn't always.
Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
COM1Active = 1
Armin
I see it when i put a watch on the SerialPort variable........

And then? How do you see the difference? Do they have different property
values that are unique to each instance?
what i
want to do is take be able to take both serial port data at the same
time but when I go into the GetDualDSIInputCOM1 it always seems to be
passing in COM1 and not COM1 and COM2 at the same time
Can you help very urgent!!!!

The two threads do run at the same time. GetDualDSIInputCOM1 is executed
twice, one time in each thread. In thread 1 it handles port 1 and in thread
2 it handles port 2. That's how I expect it to work. I don't see an errorin
this regard.

Try this:

In StartWorkerThreadDualDSI1:

    dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial,"port
1"})
    dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2,"port
2"})
...

In DoDualDSIWorkCOM1:

        Try
            Dim args() As Object = e.Argument
            Debug.Print(args(1).ToString)
            ...

Run it without interrupting or setting breakpoints. Do you only get "port1"
in the debug/output window?

Armin

Running exactly what you say it always hits my error log and give the
following:

Error Information
Error Target - RunWorkerAsync
Error Message - This BackgroundWorker is currently busy and cannot
run multiple tasks concurrently.
Exception Type - System.InvalidOperationException: This
BackgroundWorker is currently busy and cannot run multiple tasks
concurrently.
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
Stack Trace
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
 
Yes, it makes sense. You are already creating two BGWs (background
workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.
The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).
How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretendsto
be
COM1 but it isn't always.
Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
COM1Active = 1
Armin
I see it when i put a watch on the SerialPort variable........

And then? How do you see the difference? Do they have different property
values that are unique to each instance?
what i
want to do is take be able to take both serial port data at the same
time but when I go into the GetDualDSIInputCOM1 it always seems to be
passing in COM1 and not COM1 and COM2 at the same time
Can you help very urgent!!!!

The two threads do run at the same time. GetDualDSIInputCOM1 is executed
twice, one time in each thread. In thread 1 it handles port 1 and in thread
2 it handles port 2. That's how I expect it to work. I don't see an errorin
this regard.

Try this:

In StartWorkerThreadDualDSI1:

    dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial,"port
1"})
    dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2,"port
2"})
...

In DoDualDSIWorkCOM1:

        Try
            Dim args() As Object = e.Argument
            Debug.Print(args(1).ToString)
            ...

Run it without interrupting or setting breakpoints. Do you only get "port1"
in the debug/output window?

Armin

Running exactly what you say it always hits my error log and give the
following:

Error Information
Error Target - RunWorkerAsync
Error Message - This BackgroundWorker is currently busy and cannot
run multiple tasks concurrently.
Exception Type - System.InvalidOperationException: This
BackgroundWorker is currently busy and cannot run multiple tasks
concurrently.
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
Stack Trace
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
 
cmdolcet69 said:
Running exactly what you say it always hits my error log and give the
following:

Did you get the same error before inserting my debug code?


Armin
 
Yes, it makes sense. You are already creating two BGWs (background
workers),
and you create two SerialPort objects. Each port is handled in it's own
thread. That's the correct approach IMO.
The name of the Sub is confusing because it ends with "COM1", though it
should handle any port. Also the names inside (strDataCOM1, COM1Active).
How do you see that it is always port 1? Maybe because you are watching
variable "COM1Active", which is not local, anywhere else? It pretendsto
be
COM1 but it isn't always.
Public Sub GetDualDSIInputCOM1(ByVal serialPort As
IO.Ports.SerialPort)
[...]
COM1Active = 1
Armin
I see it when i put a watch on the SerialPort variable........

And then? How do you see the difference? Do they have different property
values that are unique to each instance?
what i
want to do is take be able to take both serial port data at the same
time but when I go into the GetDualDSIInputCOM1 it always seems to be
passing in COM1 and not COM1 and COM2 at the same time
Can you help very urgent!!!!

The two threads do run at the same time. GetDualDSIInputCOM1 is executed
twice, one time in each thread. In thread 1 it handles port 1 and in thread
2 it handles port 2. That's how I expect it to work. I don't see an errorin
this regard.

Try this:

In StartWorkerThreadDualDSI1:

    dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial,"port
1"})
    dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2,"port
2"})
...

In DoDualDSIWorkCOM1:

        Try
            Dim args() As Object = e.Argument
            Debug.Print(args(1).ToString)
            ...

Run it without interrupting or setting breakpoints. Do you only get "port1"
in the debug/output window?

Armin

Running exactly what you say it always hits my error log and give the
following:

Error Information
Error Target - RunWorkerAsync
Error Message - This BackgroundWorker is currently busy and cannot
run multiple tasks concurrently.
Exception Type - System.InvalidOperationException: This
BackgroundWorker is currently busy and cannot run multiple tasks
concurrently.
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
Stack Trace
at System.ComponentModel.BackgroundWorker.RunWorkerAsync(Object
argument)
at
LMIObjectLibrary.NewRuntimeMethods.StartWorkerThreadDualDSI2(String
gageType) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 2395
at LMIObjectLibrary.NewRuntimeMethods.FindWhatGagesToSelect(Gages
gage) in C:\LMI\Programming\Software\Universal Gage Interface
\Universal Gage Interface 5.0\LMIObjectLibrary
\NewRuntimeMethods.vb:line 860
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
System.IO.Ports.SerialPort
 
Did you get the same error before inserting my debug code?

Armin

Sorry it was some code I left in while test....After re adding your
code I had to make a change with the Debug.Print(args(0).ToString)
the args(1) to (0) because it only has 1 position in the
array....however looking at the output window I can;t see where its
writing the comport????
 

You had these two lines:

dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial})
dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2})

I thought it is obvious that I only added two members to the array. You
don't have to add the two lines again because they are already there.

So, again, you just have to _change_ this

dataBackgroundWorker.RunWorkerAsync(New Object() {currentSerial})
dataBackgroundWorker2.RunWorkerAsync(New Object() {currentSerial2})

to this:

dataBackgroundWorker.RunWorkerAsync( _
New Object() {currentSerial,"port 1"} _
)

dataBackgroundWorker2.RunWorkerAsync( _
New Object() {currentSerial2,"port 2"} _
)

Do NOT insert the two lines again, only CHANGE them. Then the error must go
away. Run it without breakpoints and without interruption, and then answer
the question, if only "port 1" is shown in the output/debug window.


Armin
 
Sorry it was some code I left in while test....After re adding your
code I had to make a change with the Debug.Print(args(0).ToString)
the args(1) to (0) because it only has 1 position in the
array....however looking at the output window I can;t see where its
writing the comport????

=====

No! It must have two items because I did add them here:

.... New Object() {currentSerial,"port 1"})

You see that the array has two items now. They are referred to by args(0)
and args(1) in sub DoDualDSIWorkCOM1.


I wrote "debug/output window". Did you also look in the debug window?


Armin
 
Did you get the same error before inserting my debug code?

Armin

Actaully it didn;t print out to my output window however I was able to
see that there is no sequence for the COM1 or COM2 ports being
called.....There could be 5 COM2 in a row or it could alternate COM1
and COM2 there is no pattern what I can see..... So in this
GetDualDSIInputCOM1 Sub how can I read COM1 data and COM 2 data at
the same time????
 
Back
Top