|
Glen Baker wrote:
Adding sleeps/waits is dangerous since you need to determine what is the maximum time that you need in order to ensure success.
True, but you could put the thread.sleep(300) inside of the Catch exc as exception . That way, it checks every 300 milliseconds if it is ready, and if not, it sleeps for another 300 milliseconds, until it eventually works.
I don't think there is any way to read/write to the file if it is returning a "File is already in use" error in previous tries.
Programmer2k4
My sig:
"The so-called 'Bugs' do not exist, they are merely features hidden by developers with message boxes that say 'An unhandled exception has occurred'." - Programmer2k4
"And it is a professional faux pas to pay someone else to destroy your computer when you are perfectly capable of destroying it yourself." - Roger Wright
I now use my CodeProject Blog!
|
|
|
|
|
Programmer2k4 wrote:
I don't think there is any way to read/write to the file if it is returning a "File is already in use" error in previous tries.
This is the crux of the whole issue. The file should not be in use! The StreamReader.Close() method has been called, the method has been exited. There should be no resources associated with the file (at least not with any reasonable operating system!). However clearly the O/S is maintaining some sort of handle to the file and something has to be kicked repeatedly in the butt to get the resources freed.
Unfortunately I can't for the life of me figure out what is holding onto the file, how to free it up, and why it eventually fixes itself.
Very frustrating.
|
|
|
|
|
StreamReader? I must be missing something here. The file is being written to disk by another process, your code tries to read the file when the FileSystemWatcher event fires (which will happen when the file is CREATED, not when it's completely written!) Your code attempts to read a file that is still being written and hasn't been flushed to disk and closed, correct?
The StreamReader/StreamWriter classes hold onto underlying Win32 file handles. Even though you called close on the Reader/Writer object, it would appear that the underlying, unmanaged, file handle hasn't been closed and released. This won't happen until the the Reader/Writer object is collect, which will call .Dispose() on the object. You can try calling .Dispose() yourself to free the underlying resources that your application holds.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
StreamReader? I must be missing something here. The file is being written to disk by another process, your code tries to read the file when the FileSystemWatcher event fires (which will happen when the file is CREATED, not when it's completely written!) Your code attempts to read a file that is still being written and hasn't been flushed to disk and closed, correct?
Ah...this is a plausible explanation! The first time that the handler gets called I notice that the system appears to load a dll (microsoft.visualbasic.dll). Subsequent iterations of the handler do not result in this dll being loaded. It may well be that the process of loading the dll is sufficient to delay things enough for the file write to complete, and therefore cause the StreamReader constructor to work.
From then on the dll is not loaded (it's already there), so the StreamReader constructor is called sooner, and as Dave points out the file may not be completely written yet, therefore causing the symptoms that I'm seeing.
Interestingly, trying to use read only handle doesn't solve this problem.
Hmm...makes me wonder how to properly use the FileWatcher at all.
Anyway, thanks Dave for pointing out the obvious. I'll now scratch my growing stubble and see if I can figure out a way to make this sucker work!
|
|
|
|
|
skifanatic wrote:
Interestingly, trying to use read only handle doesn't solve this problem.
That's because the other process has opened the file for write acces with DenyShareAll attributes. No other process on earth can open that file until the write is completed and the file is flushed and closed.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave Kreskowiak wrote:
That's because the other process has opened the file for write acces with DenyShareAll attributes.
The other process is samba (I'm dropping the file onto the Windows machine from my Linux machine...yes, I'm one of those annoying Linux people
|
|
|
|
|
Perhaps explicitly opening the files as readonly will help.
<br />
Dim sr As New IO.StreamReader(IO.File.OpenRead(e.FullPath))<br />
message = sr.ReadToEnd()<br />
sr.Close()<br />
I colleague of mine was having problems with trying to read, then upload, image files to a database. In the end, both the uploaded files and even the source files were getting hammered somehow, even though the code looked good. Opening the source files as readonly fixed the issue.
-- Dallas
|
|
|
|
|
I am trying to find a way to extract and send the contents of TEXT FILE 1, via email, to a list of email addresses in TEXT FILE 2.
So just to clarify, I would like some code which will rip the contents of Text File 1 and send the contents to everyone in Text File 2.
Any help received will be greatly appreciated.
Thanks,
|
|
|
|
|
Hello,
To read text from a file, here's some sample code: (can also be found in the VB .Net Help, under "reading text from files")
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
To change it to write to a TextBox/RichTextBox all that you need to do is:
' Create an instance of StreamReader to read from a file.
Dim sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
' Read and display the lines from the file until the end
' of the file is reached.
Do
line = sr.ReadLine()
Textbox1.text += line
Loop Until line Is Nothing
sr.Close()
For sending an email, look at: sending mail, examples [Visual Basic] under the index section of the help. The System.Web.Mail is most likely what you will be using
Programmer2k4
My sig:
"The so-called 'Bugs' do not exist, they are merely features hidden by developers with message boxes that say 'An unhandled exception has occurred'." - Programmer2k4
"And it is a professional faux pas to pay someone else to destroy your computer when you are perfectly capable of destroying it yourself." - Roger Wright
I now use my CodeProject Blog!
|
|
|
|
|
I have a table of questions and related to it are a set of choices. My online test that I am creating requires me to pick out the question from a question table and display it on the page.
I am trying to iterate through my DataTable I have created and print out the questions on screen but I get the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
My Code is as follows:
Dim AnswerArray(100) As String
Dim TesterArray(100) As String
Dim WrongArray(100) As String
Dim CorrectArray(100) As Boolean
Dim NumberOfQuestions As Integer
Dim NumberCorrect As Integer
Dim NumberWrong As Integer
Sub Page_Load(sender As Object, e As EventArgs)
if not IsPostBack
GetQuestions()
end if
End Sub
Public Sub GetQuestions()
'Create the SQL Connection
Dim connectionstring As String = "server='(local)'; trusted_connection=true; database='ELearn'"
Dim Oconnection As New SqlConnection (connectionString)
'oConnection.Open()
'Get the questions for the user and fill it into a data adapter
Dim strSQL As String
strSQL = "SELECT * FROM dbo_Private_Questions"
'Dim oCommand As New SqlCommand(strSQL, Oconnection)
Dim ds As System.Data.DataSet = New System.Data.DataSet
Dim da As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter(strSQL, connectionString)
'da.SelectCommand = oCommand
da.Fill(ds)
'Get the choices for the answer of each question and fill it into a data adapter
Dim choices As String
choices = "SELECT * FROM dbo_Choices"
da.Fill(ds)
'Create data tables for questions and choices so they can be iterated
Dim QuestionsTable As DataTable
QuestionsTable = ds.Tables("dbo_Private_Questions")
Dim ChoicesTable As DataTable
QuestionsTable = ds.Tables("dbo_Choices")
'Create a link between choices and questions
'Firstly get the datacolumn objects from the two datatables, create the relation with these
'columns
Dim parentCol As DataColumn
Dim childCol As DataColumn
parentCol = ds.Tables("dbo_Private_Questions").Columns("QuestionID")
childCol = ds.Tables("dbo_Choices").Columns("QuestionID")
Dim questionRelation As DataRelation
questionRelation = New DataRelation("QuestionLink", parentCol, childCol)
QuestionsTable.ChildRelations.Add(questionRelation)
NumberOfQuestions = 0
Dim questionRow As DataRow
for each questionRow in QuestionsTable.Rows
Dim tr1 As new TableRow
Table1.Rows.Add(tr1)
Dim aCell1 As new TableCell
aCell1.Text = questionRow("Question Text").ToString()
tr1.Cells.Add(aCell1)
AnswerArray(NumberOfQuestions) = questionRow("Answer").ToString()
dim count as integer = 0
Dim choiceRow As DataRow
for each choiceRow in questionRow.GetChildRows(questionRelation)
Dim tr2 As new TableRow
Table1.Rows.Add(tr2)
Dim aCell2 As new TableCell
aCell2.HorizontalAlign = HorizontalAlign.Left
tr2.Cells.Add(aCell2)
Dim cb as new CheckBox
cb.Text = choiceRow("Choice Text").ToString()
cb.ID = "CheckBox" + NumberOfQuestions.ToString() + Convert.ToChar(count + 65)
cb.Visible = true
aCell2.Controls.Add(cb)
next
Next
End Sub
I get the error for the for loop i.e. for each questionRow in QuestionsTable.Rows and also when creating the relation:
parentCol = ds.Tables("dbo_Private_Questions").Columns("QuestionID")
Is there anyone out there who can help sort it.
Thanks
Kav
|
|
|
|
|
ug39kxt@cs.bham.ac.uk wrote:
I get the error for the for loop i.e. for each questionRow in QuestionsTable.Rows and also when creating the relation:
parentCol = ds.Tables("dbo_Private_Questions")Columns("QuestionID")
The code doesn't lend itself to easily finding out so. One of the following is returning null:
ds
ds.Tables("dbo_Private_Questions")
ds.Tables("dbo_Private_Questions")Columns("QuestionID")
So, put some watches in (or debugging statements if you prefer) to find out what part of your statement is returning null.
Cada uno es artifice de su ventura
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
|
|
|
|
|
I have the error always occurs when trying to access the DataTable object in a DataSet:
parentCol = ds.Tables("dbo_Private_Questions").Columns("QuestionID")
childCol = ds.Tables("dbo_Choices").Columns("QuestionID")
for each questionRow in QuestionsTable.Rows
....
for each choiceRow in questionRow.GetChildRows(questionRelation)
...
The error occurs on these 4 lines. Creating the data table doesn't bring an error.
|
|
|
|
|
NB. I am using web matrix - free tool by microsoft - if that is any help
|
|
|
|
|
It's entirely possible that Tables("dbo_Private_Questions") doesn't return a DataTable object. This would mean that there is no table in the DataSet called "dbo_Private_Questions". The same could be true for the Columns collection of that table. Meaning there is no column called "QuestionID" in the Table object. The same could also be true for the DataSet not having a table in it called "dbo_Choices"...
Get the picture yet? Your code is assuming that these table and columns exist in the dataset. You need to write some code in there to check if they actually do exist before you use them.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I have a complete ASP web application. Now I want it to convert to VB.NET application. And the apllication can install in each computer. Is there any fast way to do it without hand by hand coding? PLEASE GIVE ME A HIT OF IT! THANKS A LOT!!
Lisa
|
|
|
|
|
You want to convert and ASP web app to a Windows Forms app? Start coding by hand. AFAIK , there's no tool that will do this, mainly because the execution models are so radically different.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi,
Can someone help me out with this urgent problem I am facing.
I have an Windows service whose components are controlled by a snap-in.
Now I need to create multiple instances of this service and snap-in on the same machine.
I am not able to latch the snap-in the instance I want to though both the instance use different users. The snap-in latches itself to the classed in the service via DCOM.
Excepting a quick reply.
What you cannot visualize you cannot achieve.
|
|
|
|
|
Please tell me what are the difference between RecordSet and ResultSet
|
|
|
|
|
AFAIK the two terms are used interchangably
|
|
|
|
|
hey, anyone know how to spawn the recycle bin window, by using...
diagnostics.process.start("SecretTextIdontKnow")
Im sure it can be done....
|
|
|
|
|
Private Sub MenuOpenRecycleBinOpenClick(ByVal sender As Object, ByVal e As EventArgs)
'"explorer.exe","/root,::{645FF040-5081-101B-9F08-00AA002F954E}"
System.Diagnostics.Process.Start("explorer.exe", "/root,::{645FF040-5081-101B-9F08-00AA002F954E}")
End Sub
Ok i got it nevermind
|
|
|
|
|
Sorry.. This works much better
System.Diagnostics.Process.Start("explorer.exe", "::{645FF040-5081-101B-9F08-00AA002F954E}")
(removed the "/root," from arguments.
|
|
|
|
|
I need to compress an avi file using a video codec in my application
how can I do this
|
|
|
|
|
I would like to create an application that does not appear in the Windows Task manager and system Try or any other means that might detect its existance
|
|
|
|
|
Can't do it. No matter what you do, the process will show up in the Task List.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|