Multitasking TcpListener.AcceptSocket - multithreading

In the code below, StartSocketListener has been started as a Thread. Sometimes, the processing of a message takes long enough that a couple of messages stack up (or at least that appears to be the case). I thought I'd take the socket and start another thread with it. I thought that passing the _HostConnection into the task would make concurrent instances of MessageFunction thread safe. Not so, it appears. I regularly get two kinds of errors: one is at _HostReader.ReadString, where I either read past the end of the stream or read only a portion of the stream (as if something else has consumed part of it). The other is at _HostWriter.Write, where I get "an established connection was aborted." Is it obvious where I've failed to get the thread safety that I was expecting? [I am a C# developer, so the VB lambda may look awkward :) ]
Private Sub StartSocketListener()
Dim _Listener As TcpListener = Nothing
Dim _HostConnection As Socket
Try
_Listener = New TcpListener(IPAddress.Parse(GetIPAddress), _TCPListenerPort)
_Listener.Start()
While _KeepListening
_HostConnection = _Listener.AcceptSocket
Dim MessageFunction = Sub(socket As Socket)
Dim _ClientIP As String = socket.RemoteEndPoint.ToString
Dim _HostSocketStream As NetworkStream = New NetworkStream(socket)
Dim _HostWriter As BinaryWriter = New BinaryWriter(_HostSocketStream)
Dim _HostReader As BinaryReader = New BinaryReader(_HostSocketStream)
Try
Dim _MsgXML As String = _HostReader.ReadString
If _MsgXML.Trim <> "" Then
If _KeepListening Then
Dim _RetXML As String = ProcessMessage(_MsgXML, _ClientIP)
Try
_HostWriter.Write(_RetXML)
Catch ex As Exception
...
End Try
End If
End If
Catch ex As Exception
...
End Try
_HostReader.Close()
_HostWriter.Close()
_HostSocketStream.Close()
socket.Close()
End Sub
Task.Factory.StartNew(Sub() MessageFunction(_HostConnection))
End While
_Listener.Stop()
Catch ex As Exception
....
End Try
End Sub

Related

Events causing cross-thread error in backgroundworker_progresschanged and backgroundworker_complete

My VB.NET winforms app runs a timer which creates a background worker to update the objects in an ObjectListView.
In the timer loop, a number of 'device' objects are added to an observable collection (in the backgroundworker_progresschanged event) and (in the backgroundworker_complete event), I use an OLV.SetObjects(allDevices, true) to populate them.
This all works flawlessly. However, the currently selected items in the OLV are lost during the OLV.setobjects so I need to restore them.
To do this, (in the backgroundworker_complete event), I want to access the selecteditems property of the OLV but I keep getting a "Cross-thread operation not valid: Control 'DeviceListView1' accessed from a thread other than the thread it was created on." All attempts at trying to read the selected listviewitems (either by OLV.selecteditems or a loop reading them from the OLV) fail with the cross-thread exception.
I may misunderstand but I thought I could access GUI elements on the backgroundworker_progresschanged and backgroundworker_complete events?
Here's the relevant code:
The PopulateDevices sub is called when the timer is started and will not run again until a specific time has passed. It runs the RunWorkerAsync of the Worker.
Public Sub PopulateDevices()
' Debug
_UpdateCount += 1
' Pause the Update Timer
UpdateTimer.Stop()
' Get the Starting Time of this Update
StartTime = DateTime.Now
' Stop updating the DeviceListView1 ObjectListView
ControlHelper.ControlInvoke(DeviceListView1, Sub() DeviceListView1.BeginUpdate())
' Clear Existing Devices from the List
AllDevices = New TrulyObservableCollection(Of DeviceItem)
' Get the selected devices
'_SelectedDevices = GetSetSelectedDevices(DeviceListView1)
' Prep the BackgroundWorker
PopulateDevicesWorker = New BackgroundWorker
PopulateDevicesWorker.WorkerReportsProgress = True
' Add the Event Handlers
AddHandler PopulateDevicesWorker.DoWork, AddressOf PopulateDevicesWorkerDoWork
AddHandler PopulateDevicesWorker.ProgressChanged, AddressOf PopulateDevicesWorkerProgressChanged
AddHandler PopulateDevicesWorker.RunWorkerCompleted, AddressOf PopulateDevicesWorkerCompleted
' Start the BackgroundWorker
If Not PopulateDevicesWorker.IsBusy Then
PopulateDevicesWorker.RunWorkerAsync()
End If
End Sub
The worker will read a list of devices from a SQLite DB and (in the progresschanged event) populate an observable collection (AllDevices):
Private Sub PopulateDevicesWorkerDoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs)
' We only continue if the \Clients\_Cache File exists and can be read
If Not File.Exists(CacheFilePath) Then
Exit Sub
End If
' Create a new SQLite Connection & Connect to database
Dim DBC As SQLiteDatabase = OpenDB(CacheFilePath)
If Not IsNothing(DBC) Then
' Count the Rows in the \Clients\_Cache file
Dim RowCount As Integer = CountTableRows(DBC, "_Cache")
' Set the SQL Query
SqlQuery = "SELECT * FROM _Cache WHERE Archived = #Archived"
' Create the SQLite Command
Using SQLitecmd As SQLiteCommand = New SQLiteCommand(SqlQuery, DBC.Connection)
SQLitecmd.Parameters.AddWithValue(String.Empty & "Archived", IIf(fMain.ButtonItem_VIEWARCHIVE.Checked, "True", "False"))
Using SQLiteReader = SQLitecmd.ExecuteReader()
Dim Counter As Integer = 0
' Read All Properties into the Array
While SQLiteReader.Read()
Using DeviceItem As New DeviceItem
With DeviceItem
' Get the Device Info here
End With
' Report progress at regular intervals
PopulateDevicesWorker.ReportProgress(CInt(100 * Counter / RowCount), DeviceItem)
' Increment the Counter (for Progress)
Counter += 1
End Using
End While
End Using
End Using
End If
CloseDB(DBC)
End Sub
Here is the WorkerProgressChanged event. It adds the current device (from the worker) into the observable collection (AlLDevices)
Private Sub PopulateDevicesWorkerProgressChanged(sender As Object, e As ProgressChangedEventArgs)
' Update Status
LabelItem_STATUS.Text = "Working.. (" & e.ProgressPercentage & "%)"
' Add the Device to Collection
AllDevices.Add(TryCast(e.UserState, DeviceItem))
End Sub
The WorkerCompleted event will set the objects in AllDevices to the OLV (DeviceListView1)
Private Sub PopulateDevicesWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) ' Handles PopulateDevicesWorker.RunWorkerCompleted
' This is producing Cross-Thread error
If Not IsNothing(_SelectedDevices) Then
For Each item As ListViewItem In _SelectedDevices
Debug.Print(item.Text)
Next
End If
' Populate the ObjectListView
ControlHelper.ControlInvoke(DeviceListView1, Sub() DeviceListView1.SetObjects(AllDevices, True))
' Re-enable Form Updates
ControlHelper.ControlInvoke(DeviceListView1, Sub() DeviceListView1.EndUpdate())
' If the refresh rate isn't already set, set it to the time taken to complete the Update PLUS the Seconds specified in the SETTINGS.INI File
Dim difference As TimeSpan = DateTime.Now.Subtract(StartTime)
If UpdateTimeInSeconds = -1 Then
UpdateTimer.Interval = (RefreshRate + difference.TotalSeconds) * 1000
End If
' Restart the Update Timer
UpdateTimer.Start()
End Sub
I was under the impression, that I can update the GUI (get the OLV selecteditems, etc.) from the WorkerProgressChanged and WorkerCompleted backgroundworker events but I get the darn cross-thread error.
I'm also having to INVOKE the BEGIN\END UPDATE as calling them directly produces error.
I have read that the olv.setobjects in ObjectListView 2.91 (the version I am using) should persist the selections but I haven't seen this at all.
Please! What am I missing? Its probably something daft or is there another way of doing this?
If you are not using a Forms.Timer (but Timers.Timer or Threading.Timer) for your UpdateTimer, anything called from the timer "tick" event will run on a different thread.
Thus, PopulateDevices would also be called from a non GUI thread and the BackgroundWorker will run on that thread as well.

Locking thread conditionally and processing each time only one code

Firstly, new thread born independently of my code by external factory.
I have something integer key variable - "CODE". This variable "CODE" I received as result of a lot of calculating and request to DB (and maybe I need multi-threading protection of this "CODE").
I need lock thread only for the same "CODE", if currently "CODE" is handling now, thread with other "CODE" can not locking and handling without obstacle.
Thread with "CODE" now handling and locking need to immediately finish.
Also all this function must be working asynchronously with ASYNC/AWAIT.
I am using .NET Core 6.
It looks like a very common task, but what mechanism do I need to use?
Does code template for this task exist in inet?
This is my solution, but I'm not sure how it working with high uploading. I only try to test this code with high concurrency. If anybody has advice for me, please.
I realize solution with ConcurrentDictionary and locking critical section. To unlock main locker and using secondary locker I created new task.
This is schema of my solution.
Public Sub New(ByVal logger As ILogger(...)
....
WorkingServer = New ConcurrentDictionary(Of Integer, BashJob)()
WorkingVm = New ConcurrentDictionary(Of Integer, BashJob)()
End Sub
Private WorkingServer As ConcurrentDictionary(Of Integer, BashJob)
Private RequestNextJob As New Object
Public Async Function Execute(context As IJobExecutionContext) As Task Implements IJob.Execute
_logger.LogInformation($"Time {Now}")
Interlocked.Increment(Counter)
SyncLock RequestNextJob
' calculate CODE hidden in NextJob
...
Dim NextJob As BashJob = Res1.Result.Item1(0)
Dim Val1 As BashJob
Dim ServerWorking As Boolean = WorkingServer.TryGetValue(NextJob.toServer, Val1)
If Not ServerWorking Then
Dim AddSucess1 = WorkingServer.TryAdd(NextJob.toServer, NextJob)
If AddSucess1 Then
Dim ServerThread = New Thread(Sub() ServerJob(NextJob, New ServerClosure))
ServerThread.Start()
Else
Exit Function
End If
Else
Exit Function
End If
End SyncLock
End Function
Async Sub ServerJob(ByVal Prm As BashJob, ByVal Closure As ServerClosure)
Try
Closure.Res = Sql.ExecNonQuery(...)
...
'main processor
...
WorkingServer.TryRemove(Prm.toServer, Prm)
Catch ex As Exception
_logger.LogInformation($"ServerSsh ({Counter.ToString}) {Now.ToString} Server:{Prm.toServer} {Prm.i}:[{Prm.Command}] GettingError {ex.Message}")
Finally
WorkingServer.TryRemove(Prm.toServer, Prm)
End Try
End Sub
End Class
Public Class ServerClosure
Property Res As Integer
Property Server As ServerBashAsync
Property Connect As Tuple(Of Renci.SshNet.SshClient, Exception, Exception)
Property BashRet As Task(Of String)
Property IsCompleted As Integer
Property IsCompletedWithErr As Integer
End Class

Await / async - async method runs in Main Thread

I just started to learn Async/Await, and I've got a problem. There is a WinForms App with 2 buttons and permanently running status-bar on it. When pressing first button - everithing is fine - cicle running in another thread and UI doesn't freeze. When pressing second - cicle running in Main Thread, so UI freeze. I just can't get it! Why? For me this methods look almost the same. Btw sry my bad english.
Public Class Form1
'Async - Create Task from Sync Method
Private Async Sub btn_async_from_sync_Click(sender As Object, e As EventArgs) Handles btn_async_from_sync.Click
Dim tsk As New Task(Of Integer)(AddressOf func_for_task)
tsk.Start()
Dim val As Integer = Await tsk
MsgBox(val)
End Sub
Private Function func_for_task() As Integer
Dim val As Integer
For i = 1 To 999999999
val += 1
Next
Return val
End Function
'Async - Use existing Task - Async Method
Private Async Sub btn_async_use_task_Click(sender As Object, e As EventArgs) Handles btn_async_use_task.Click
Dim tsk As Task(Of Integer) = func_for_task_async()
Dim val As Integer = Await tsk
MsgBox(val)
End Sub
Private Async Function func_for_task_async() As Task(Of Integer)
Dim val As Integer
For i = 1 To 999999999
val += 1
Next
Return val
End Function
End Class
Don't ignore compiler warnings. In this case, the compiler will warn you that your async method does not use await, and therefore will run synchronously.
In your case, you can use Task.Run to execute func_for_task on a background thread, and await it from your click event handler.
For more info, see the async/await intro on my blog.

Detect if application running in remote app

Is there a way to detect from an application written in c# if it's being launched as a remote app using RDP?
Get the parent of your application's process and check if it's raised by rdpinit.exe. If so, it's a RemoteApp.
Quick example for getting the parent-process-id (sorry, vb.net):
<Extension()>
Public Function GetParentProcessId(process As Process) As Integer
If process Is Nothing Then Throw New NullReferenceException()
Dim parentProcessId As Integer
Dim snapShot As IntPtr = IntPtr.Zero
Try
snapShot = CreateToolhelp32Snapshot(SnapshotFlags.Process, 0)
If snapShot <> IntPtr.Zero Then
Dim procEntry As New PROCESSENTRY32
procEntry.dwSize = CUInt(Marshal.SizeOf(GetType(PROCESSENTRY32)))
If Process32First(snapShot, procEntry) Then
Do
If process.Id = procEntry.th32ProcessID Then
parentProcessId = CInt(procEntry.th32ParentProcessID)
Exit Do
End If
Loop While Process32Next(snapShot, procEntry)
End If
End If
Catch ex As Exception
Throw
Finally
If snapShot <> IntPtr.Zero Then
CloseHandle(snapShot)
End If
End Try
Return parentProcessId
End Function
Now you can get the parent-process easily.
Regards,
Jan

SAP RFC call returns "Error 0" in RETURN parameter from vb

Hi everybody and thanks in advance.
I'm trying to call a SAP BAPI using RFC from vb but I'm having some problem to get the result of the call.
The BAPI "BAPI_GL_ACC_EXISTENCECHECK" (from General Ledger Account module) has two parameters,
COMPANYCODE and GLACCT, and a RETURN parameter.
I wrote this piece of code to make the call and I had no problem to establish the SAP Connection (I use the SAP Logon Control OLE/COM object to do the job), and I tried to make the RFC call.
Also in this case I make the call without problems (it seems, not sure about it ...), because the RFC call returns true and no exception.
However, looking through the objReturn object/parameter, it has a value "Error 0" in it.
I was expecting a complex structure like the BAPIRETURN object in SAP or something similar if the account doesn't exist.
Tried to search with Google and SAP forums but I haven't found a real solution to my problem, so here I am to ask you all if you have some idea to solve this problem (maybe I'm only making a wrong call!!! I'm quite a newbie on SAP integration ...).
BTW, Final Notes: after a lot of RFC_NO_AUTHORIZATION on the SAP side, they gave me a SAP_ALL / S_RFC authorization (sort of, not a SAP expert) and the error RFC_NO_AUTHORIZATION disappeared, but not the Error 0 return
Dim sapConn As Object
Dim objRfcFunc As Object
Dim SAPMandante As String
Dim SAPUtente As String
Dim SAPPassword As String
Dim SAPLingua As String
Dim SAPApplicationServer As String
Dim SAPNumeroSistema As Variant
Dim SAPIDSistema As String
Dim SAPRouter As String
Dim FlagInsertLogin As Integer
Dim FlagLogin As Variant
On Error GoTo ErrorHandler
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object
'Silent Logon
SAPMandante = "xxx"
SAPUtente = "yyyy"
SAPPassword = "zzzzzz"
SAPLingua = "IT"
SAPApplicationServer = "www.xxx.com"
SAPNumeroSistema = x
SAPIDSistema = "zzz"
SAPRouter = ""
FlagLogin = SilentLogin(sapConn, SAPMandante, SAPUtente, SAPPassword, SAPLingua, SAPApplicationServer, SAPNumeroSistema, SAPIDSistema, SAPRouter) 'IT WORKS, NO PROBLEM HERE
If FlagLogin = False Then
'Explicit Logon
If sapConn.Connection.logon(0, False) <> True Then
MsgBox "Cannot Log on to SAP", 16, "Query Interrupted"
sapConn.Connection.logoff
Set sapConn = Nothing
InsertCash = False
Exit Sub
End If
End If
'BAPI RFC Call
Set objRfcFunc = sapConn.Add("BAPI_GL_ACC_EXISTENCECHECK")
objRfcFunc.exports("COMPANYCODE") = "C100"
objRfcFunc.exports("GLACCT") = "0000000001" 'Inexistent
Rem *** BAPI CALL ***
If objRfcFunc.Call = False Then
ErrorMsg = objRfcFunc.Exception 'Message collection
MsgBox ErrorMsg, 16, "Errore"
sapConn.Connection.logoff
Exit Sub
else
Dim objReturn As Object
Set objReturn = objRfcFunc.imports("RETURN")
End If
You need to put
Dim objReturn As Object
Set objReturn = objRfcFunc.imports("RETURN")
BEFORE objRfcFunc.Call
i.e. you must state what you're importing from the function before you call it. I usually put it alongside the .exports() lines.
The problem was solved, please take a look at this thread ...
http://sap.ittoolbox.com/groups/technical-functional/sap-dev/sap-rfc-call-returns-error-in-return-parameter-from-vb-before-the-rfc-call-4894968#M4902486

Resources