writing to file in multithreaded app fails vb.net - vb.net

I have a multi threaded app that writes log to file. occasionaly, saving fails using this code
Friend Sub SaveToText(ByVal FileName As String, ByVal Text As String)
'// create a writer and open the file
Dim objLock As New Object
SyncLock objLock
Dim tw As TextWriter = Nothing
Try
'// write a line of text to the file
tw = New StreamWriter(FileName, True)
tw.Write(Text)
Catch ex As Exception
MessageBox.Show(ex.Message, "Error saving", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally
'// close the stream
If tw IsNot Nothing Then
tw.Close()
tw.Dispose()
End If
End Try
End SyncLock
End Sub
The error message i get
The process cannot access the file 'error.log' because it is being
used by another process.
Which other way can i make this safe?

Dim objLock As New Object
Your SyncLock statement doesn't lock anything. Every thread will gets its own instance of objLock since it is a local variable and gets allocated every time the method is entered. Move the declaration outside of the method so it becomes a member of your class. And make sure there is only one instance of that class. Or make it Shared ReadOnly.

Related

How to properly set up BackgroundWorker in WPF VB

I am working in VB and when the end user clicks on a button, I want to show a “Please wait” window (no progress update necessary) and trigger the process in the background on another thread as it might take a while to complete. I understand BackgroundWorker is the recommended approach for this, and although I have previously set up a BGW on a windows forms application, I am new to WPF and having a hard time understanding the differences.
In order to reuse some of my code, I have written a function (“DbOperation”) that executes a SQL stored procedure (from parameter) and returns a data table (or NULL if no output), and it seems to work well everywhere except in concert with my BGW.
I understand UI should only be affected by the main thread, and background threads should not touch the UI, and the BGW thread should call the long-running process.
Private Sub btnProcessReports_Click(sender As Object, e As RoutedEventArgs) Handles btnProcessReports.Click
'Set up background worker
bgw.WorkerReportsProgress = False
bgw.WorkerSupportsCancellation = False
AddHandler bgw.DoWork, AddressOf bgw_DoWork
AddHandler bgw.RunWorkerCompleted, AddressOf bgw_RunWorkerCompleted
'Show please wait window
f.Label1.Text = "Importing web reports. Please wait."
f.Show()
'Start the work!
bgw.RunWorkerAsync()
End Sub
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs)
'Set current thread as STA
Thread.CurrentThread.SetApartmentState(ApartmentState.STA)
'Trigger job
Dim Qry As String = "EXEC [msdb].[dbo].sp_start_job N'Import Online Payment Portal Reports';"
DbOperation(Qry)
End Sub
Public Function DbOperation(ByVal qry As String, Optional ByVal type As DatabaseQueryReturnType = DatabaseQueryReturnType.NonQuery) As Object
Dim objDB As New Object
Dim dt As DataTable
Dim da As SqlDataAdapter
OpenConnection()
Dim cmd As New SqlCommand(qry)
cmd.Connection = con
If type = DatabaseQueryReturnType.DataTable Then
dt = New DataTable
da = New SqlDataAdapter(cmd)
Try
da.Fill(dt)
Catch ex As Exception
MessageBox.Show("Error retrieving data from database: " & ex.Message.ToString)
End Try
objDB = dt
dt.Dispose()
CloseConnection()
Return objDB
ElseIf type = DatabaseQueryReturnType.NonQuery Then
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("Error executing nonquery: " & ex.Message.ToString)
End Try
objDB = Nothing
CloseConnection()
Return objDB
End If
Return objDB
End Function
When I try running the program, it gives me “The calling thread must be STA, because many UI components require this.” on the line that calls my function. After a bit of research, I learned that you have to call SetApartmentState() before the thread is started, but this doesn’t make sense since the recommended code seems to be:
Thread.CurrentThread.SetApartmentState(ApartmentState.STA)
After adding this line, I then get “Failed to set the specified COM apartment state” on that line of code.
I have also tried commenting out the messagebox lines in the function in case it was trying to open the messageboxes on the UI thread, but it had no impact and I still got the same errors.
What am I missing in my code to get the function to run on the BGW thread?

Getting error in Access database connection

I'm trying to connect to my database and I'm getting this error message (ex):
I've another function which opens DB and load some data, it works fine without any error... But when I try to use this, it returns me this error, the line 175 is "cn.Open()".
My connection string and Local_DB is both same as other functions which are working without errors.
Private Sub AtualizaClientes(ID As Integer)
' Local da DataBase
Dim Local_DB As String
Local_DB = "C:\Users\Heitor BASAM\Desktop\Sistema\DataBase_Sistema.accdb"
Try
Dim cn As New OleDb.OleDbConnection
cn.ConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={Local_DB}"
cn.Open()
cn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Glad you fixes your problem! Just a few bits to tidy up the code.
Always use Using...End Using with database objects that expose a Dispose method. They may be using unmanaged objects and their dispose methods must run to release these resources. The End Using takes care of this for you even if there is an error.
You can let the error bubble up to the calling code, user interface code. Wrap the call to AtualizaClientes in a Try...Catch...End Try
Private Sub AtualizaClientes(ID As Integer)
Dim Local_DB = "C:\Users\Heitor BASAM\Desktop\Sistema\DataBase_Sistema.accdb"
Using cn As New OleDb.OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={Local_DB}")
cn.Open()
End Using
End Sub
EDIT
You would add the Try...End Try to the UI code. Example...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim id As Integer = 7
Try
AtualizaClientes(id)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
I figured out the problem! I've a function when a especify textbox is changed, it calls this "AtualizaClientes" function. The problem is, when you load the form, it runs a "TextChanged" event in every textbox and trys to run the database before loading it. This was causing the error message.
To solve this problem, I made a boolean variable which returns true after loading the database. So, I changed my textchanged event to run only if this variable is true.

Throwing exception from sub to async call

This is a windows forms application in which I have a particular form. On this form I display the progress of some processing that is supposed to happen in the background asynchronously. All of it works great, except for when I try to handle exceptions that are caught within the background processing....
This is the sub in my form's code that calls the Async function, which is in a module containing all the background processing code:
Public Async Sub BasicProcessing()
Try
Dim processTarget As Action(Of Integer)
processTarget = AddressOf UpdatePulseProcessing
myProgress = New Progress(Of Integer)(processTarget)
myCount.Vehicles = Await ProcessmyCountFile(myCount, myProgress)
If OperationCanceledByUser = True Then
Exit Sub
End If
Catch ex As Exception
MessageBox.Show(Me, "Unable to update count." _
& Environment.NewLine & ex.Message, _
"Error updating count", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
End Sub
This is the async function that it calls, which is in a separate module:
Public Function ProcessmyCountFile(CountToProcess As Count, ByVal ProgressObject As IProgress(Of Integer)) As Task(Of List(Of Vehicle))
myProgressObject = ProgressObject
basicToken = New CancellationTokenSource
Try
Return CType(Task(Of List(Of Vehicle)).Run(Function()
If basicToken.IsCancellationRequested Then
Return Nothing
Exit Function
End If
myCountFile = CountToProcess
MyVehicles = New List(Of Vehicle)
'All that is important in here to note is a call to a regular sub within this module
CreateVehicles()
Return MyVehicles
End Function, basicToken.Token), Global.System.Threading.Tasks.Task(Of List(Of Global.STARneXt.Vehicle)))
Catch ex As Exception
Throw New Exception(ex.Message)
Return Nothing
End Try
End Function
Public Sub StopProcess()
If Not basicToken Is Nothing Then
basicToken.Cancel() ' We tell our token to cancel
End If
End Sub
This is the regular sub called by the Async function:
Private Sub CreateVehicles()
Try
'In here are calls to other regular subs within the same module, let's just call them A and B
Catch ex As Exception
StopProcess()
Throw New Exception("Error creating vehicles at pulse " & pulsePointer & ". " & ex.Message)
End Try
End Sub
When I run this code with data that I know ends up generating an error in sub B, the error does propagate up, as far as up to the method that is directly called by the async function....
So when running in VS, it will stop at "Throw New Exception("Error creating vehicles at pulse " & pulsePointer & ". " & ex.Message)", with the Message containing the message thrown by sub B.
This is what the debugger says on that line:
An exception of type 'System.Exception' occurred in MyProject.exe but
was not handled in user code. Additional information: Error creating
vehicles at pulse....[error message propagated up as thrown by called
subs]. Arithmetic operation resulted in an overflow.
Then strangely enough, within the debugger if I hit "Step Into", it does then return to the sub in my form that called the Async function, which shows the message box on the GUI.
So how do I get this to automatically return back up to the original form code to show the message box? Why does it stop where it stops without continuing to propagate?
FYI, I did find this question, but it ultimately did not help.
#StephenCleary was right - I created a new install for my project as it is, and in the installed version I do get the message box with the expected error message.
Strange behavior on the part of the debugger, and a bit discouraging, but none-the-less I am glad that my code as laid out in my question does actually work.

Location to save custom settings text file?

I have a VB.Net program that has a few custom settings that are saved in a text file. Right now the file is saved in "settings.txt" which saves in the bin folder. The problem is that this program gets published to my network so my coworkers can use it, and every time I roll out a new update their settings they've saved get deleted; it overwrites the file with a blank version of it. Is there another location that would be better for me to save the file? or is there a way (maybe through code?) to prevent the contents for each of my coworkers from getting deleted every time I publish an update?
Public Class Program
'global variables
Dim fileName As String = "settings.txt"
Private Sub Program_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'code
Try
If File.Exists(fileName) = False Then
File.Create(fileName)
End If
' Open the file using a stream reader.
Using sr As New StreamReader(fileName)
Dim line As String
While (sr.EndOfStream = False)
line = sr.ReadLine()
End While
End Using
Catch ex As Exception
MsgBox(ex.ToString())
End Try
'rest of program
End Sub
End Class
If this is a ClickOnce app, try this:
Friend fileName As String = String.Empty
With My.Application.Info
fileName = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), .CompanyName, .ProductName, .Version.ToString, "Settings.txt")
End With

server busy error message vb.net

I get this error. I can not catch it where it comes from. All methods made with try catch but still I get this error from time to time. I used code:
Private Sub add2ComboBox_called(ByVal text As String)
Try
If Me.InvokeRequired Then
Dim args() As String = {text}
Me.Invoke(New Action(Of String)(AddressOf add2ComboBox_called), args)
Return
End If
ComboBox_called.Items.Add(text)
Catch ex As Exception
log_it(ex.StackTrace)
End Try
End Sub
and also this one:
Public Sub clear_do_not_open()
Dim FILENAME As String = path & "\DO_NOT.OPEN"
Dim index As Integer
Try
Using fs As New IO.FileStream(FILENAME, IO.FileMode.Truncate, IO.FileAccess.Write, IO.FileShare.ReadWrite), _
tl As New TextWriterTraceListener(fs)
index = Trace.Listeners.Add(tl)
Trace.Write("")
Trace.Listeners(index).Flush()
Trace.Flush()
End Using
Trace.Listeners.RemoveAt(index)
Catch ex As Exception
log_it(ex.StackTrace)
End Try
End Sub
How do I make this to press Retry by default if it appears? Wait for a second and press Retry again? If not like this then how do I know that this message pops up? If catch the moment it shows up I could then just restart the program or send an email to myselt so I could come to PC where program runs and I could handle it. Now message pops up and program stops while its on.