Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Questions (Windows Mobile)
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Questions (Windows Mobile) Post any question regarding Basic4ppc.

File being used by another process

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2009, 06:11 PM
Basic4ppc Veteran
 
Join Date: Sep 2009
Location: South Jordan, Utah
Posts: 237
Send a message via Skype™ to jschuchert
Default File being used by another process

I have created several points that need to be stored in my file. However I am getting an error that states "File blah blah cannot be accessed because it is being used by another process". This said to me that the file was still open when I tried to open it again. However, I have made sure the file was closed as the following code will show. Here is the code leading up to the problem:

Code:
Sub compassrule(dnorthstart,deaststart, totaldist, sfirst, spend)
erlat = main.closenorth - dblNorth        
'total error in latitude
erdep = main.closeeast - dblEast          'total error in departure
FileClose(c)
FileOpen(c,main.strfilename,cRead)
lineoftext=FileRead(c)
Do Until lineoftext=EOF
coord()=StrSplit(lineoftext,
",")
If coord(0)=main.strfirst Then
dblNorth1 = coord(
1)
dblEast1 = coord(
2)
Exit
End If
lineoftext=FileRead(c)
Loop
FileClose(c)

For i = main.strpointno-(main.intcounter) To main.strpointno-1
FileClose(c)
FileOpen(c,main.strfilename,cRead)
lineoftext=FileRead(c)
Do Until lineoftext=EOF
coord()=StrSplit(lineoftext,
",")
If coord(0)=i Then
dblNorth2 = coord(
1)
dblEast2 = coord(
2)
dblNdiff = 
Abs(dblNorth1 - dblNorth2)
dblEdiff = 
Abs(dblEast1 - dblEast2)
dist = 
Sqrt(dblNdiff ^ 2 + dblEdiff ^ 2)
lat = lat + (dist * erlat / totaldist)
dep = dep + (dist * erdep / totaldist)
dblNorth2 = dblNorth2 + lat
dblEast2 = dblEast2 + dep
coord(
1) = dblNorth2
coord(
2) = dblEast2
coord(
0) = i
dblnorth=dblnorth2
dbleast=dbleast2
main.strpointno=coord(
0)
FileClose(c)
CallSub("storecoord")  'problem after this sub is called
dblNorth1 = dblNorth2
dblEast1 = dblEast2
Exit
End If
lineoftext=FileRead(c)
Loop
FileClose(c)
Next 
FileClose(c)
End Sub
Then here is the code for the storecoord sub:
Code:
Sub storecoord()
coord(
0) = main.strpointno
coord(
1) = dblNorth
coord(
2) = dblEast
FileClose(c)
FileOpen(c,main.strfilename,cWrite,cAppend) 
'<font color="red"> Where error is triggered </font>
FileWrite(c, coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3))
FileClose(c)
End Sub
All of the numbers work leading up to the problem. I have successfully stored points using this sub with other routines. Could the error message be intimating something else? I suspect it might be bad logic within the For...Next with Do...until embedded but can't pinpoint it. I appreciate all the past support I have gotten here. Thanks.

Jim Schuchert
Reply With Quote
  #2 (permalink)  
Old 11-27-2009, 12:06 AM
Basic4ppc Veteran
 
Join Date: Sep 2009
Location: South Jordan, Utah
Posts: 237
Send a message via Skype™ to jschuchert
Default

Well guys, apparently you can't come up with a solution, either. Since my post, I have done everything I can think of, but without success. There is a work around I can use but it is really hokey and will require my explaining in the instruction manual how to do it, including exiting and returning to the program. Maybe b4ppc doesn't have the tools for this one. I have determined that a file opened for read, then closed then opened for write within the same routine can't be done without firing the error I keep getting. It doesn't make sense that it cannot be done because I have done it with other software but my skill level simply is not high enough yet with b4ppc. Hopefully one of you gurus will see the solution right away.

Jim
Reply With Quote
  #3 (permalink)  
Old 11-27-2009, 10:57 AM
Basic4ppc Expert
 
Join Date: May 2008
Location: Berkshire, UK
Posts: 810
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I must first say that I don't know what the problem is.

But it seems strange to start a code sequence with closing the file before opening it. That hints that you think that other regions of code are/may be not clsong the file when "finished with". (I don't know if the FileClose() fails if the file is not open; if so that means you are indeed leaving the file open.) Why not keep the opening and closing in the same Sub?

The Help about files mentions the "connection name" but does not say what sort of variable this is. It does not seem to need to be declared or "Dim"ed, so there is no opportunity to specify whether it is local to a module (I deduce you are using modules since you use "Main.") or public. Are you using the same connection name in other modules?

Another approach might be to use ErrorLabel(), but I'm not sure what you would do in the error handling routine!

HTH, Mike.
Reply With Quote
  #4 (permalink)  
Old 11-27-2009, 11:33 AM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 4,463
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

I had also a look at your code and don't see why you get the error.
As already suggested by mjcoon, I would also remove all unnecessary FileClose(c) lines.
Another suggestion is to change the 2nd routine that way:
Code:
Sub storecoord()
  coord(
0) = main.strpointno
  coord(
1) = dblNorth
  coord(
2) = dblEast
  FileOpen(<font color=
"red">c1</font>,main.strfilename,cWrite,cAppend)
  FileWrite(<font color=
"red">c1</font>, coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3))
  FileClose(<font color=
"red">c1</font>)
End Sub
Or could you post a sbp file so we could also test it and try to find out what's going wrong.

Best regards.
__________________
Klaus
Switzerland

Beginner's Guide / User's Guide
Reply With Quote
  #5 (permalink)  
Old 11-27-2009, 03:53 PM
Basic4ppc Veteran
 
Join Date: Sep 2009
Location: South Jordan, Utah
Posts: 237
Send a message via Skype™ to jschuchert
Default

Mike and Klaus,

Thank you for your responses. I suppose it is a VB habit of mine to always close a file prior to opening just in case I did not close it from a prior routine. Mike's suggestion that maybe B4PPC doesn't handle that well is a good one and I will check that out. Klaus, your suggestion to change the connection name in the 'Storecoord' sub doesn't work because the file name (strfilename)is still the same. If I change the file name, all is OK.

Here is what I am trying to do:

I have a text file of values, separated with commas (strfilename) that I open at the beginning of the application. Then other routines make calculations, the results of which are added to that file via the 'storecoord' sub. No problems so far. Now I have a routine (a traverse) that creates new points w/coordinates intended to close upon itself or an existing point. These new points are also stored in the file. Still no problems to here. Since the traverse does not exactly close upon the 'closing point', I need to adjust the distances/coordinates to make it precise. This is where the 'compassrule' sub comes to play. The code in this sub is supposed to adjust the coordinates and put them back into the file. The math works fine but putting the points back into the file is where the error occurs. Looking at my code, you will see where I have tried to do this by closing the file, then opening it for writing (storecoord). I realize I will have 2 sets of the same points in the file (unadjusted and adjusted) but I will address that later. My work-around is to create a second file to contain the adjusted points, have the user exit the program and copy and paste from the second file to overwrite the points in the main file (strfilename). I could probably do it with code without leaving the program but that is a lot of additional code if not necessary. Hopefully this explanation will help your understanding of the issue.

I guess what I am really asking is whether or not my logic should work and if it is feasible to do it with the method I am using. Thanks Mike and Klaus for your help.

JIm
Reply With Quote
  #6 (permalink)  
Old 11-27-2009, 08:16 PM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 4,463
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

Hi Jim,
Couldn't you store the content of your file in a matrix variable, make all your changes and addings in that matrix and when leaving the program store the matrix back to the file. I find that this would be more efficient than working in the file or even with two files.

Best regards.
__________________
Klaus
Switzerland

Beginner's Guide / User's Guide
Reply With Quote
  #7 (permalink)  
Old 11-27-2009, 08:27 PM
Basic4ppc Veteran
 
Join Date: Sep 2009
Location: South Jordan, Utah
Posts: 237
Send a message via Skype™ to jschuchert
Default

Hi Klaus,

Thanks again for your taking the time to help me. I am not clear on what you mean by storing in a matrix. Would you please give me a brief example?

Jim
Reply With Quote
  #8 (permalink)  
Old 11-28-2009, 06:50 AM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 4,463
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

You have several possibilities:

1) With a matrix variable:
Dim Pnt(1000,4)
where
Pnt(i,0)=coord(0)
Pnt(i,1)=coord(1)
Pnt(i,2)=coord(2)
Pnt(i,3)=coord(3)
i is the index of the point you are looking at

2) or with a structure variable

Dim Type (X, North, East, K) Pnt(1000)
where
Pnt(i).X=coord(0)
Pnt(i).North=coord(1)
Pnt(i).East=coord(2)
Pnt(i).K=coord(3)
You should adapt X and K to the meaning of your variables, as I don't know what coord(0) and coord(3) do mean.

3) or with single array variables:
Dim PntX(1000)
Dim PntNorth(1000)
Dim PntEast(1000)
Dim PntK(1000)
where
PntX(i)=coord(0)
PntNorth(i)=coord(1)
PntEast(i)=coord(2)
PntK(i)=coord(3)

I hope this is more understandable.

Best regards.
__________________
Klaus
Switzerland

Beginner's Guide / User's Guide
Reply With Quote
  #9 (permalink)  
Old 11-28-2009, 09:05 AM
Basic4ppc Expert
 
Join Date: May 2008
Location: Berkshire, UK
Posts: 810
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
Originally Posted by klaus View Post
You have several possibilities:

1) ...
Or perhaps even more radically

4) Use a Table. This has the advantages (should you need them) that you can extend it dynamically merely by adding new rows (just like a file) and also filter to discover rows with given content without doing a search.

A table is just like a matrix but is declared like a Control, not a variable. This is because it can be visible to the user but does not have to be. The columns in the table are equivalent to the named elements in Klaus's "2) or with a structure variable".

HTH, Mike.
Reply With Quote
  #10 (permalink)  
Old 11-28-2009, 04:11 PM
Basic4ppc Veteran
 
Join Date: Sep 2009
Location: South Jordan, Utah
Posts: 237
Send a message via Skype™ to jschuchert
Default

Thank both of you very much for the alternatives. I will explore and try to implement something like that in my code. It may prove to be a valuable tool.
Klaus, coord(0) is the point number and coord(3) is the descriptor of the point.

Jim
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inter Process Communication/Remoting library agraham Additional Libraries 16 01-05-2012 09:11 AM
how to check running process twisted Questions (Windows Mobile) 4 03-11-2009 10:14 PM
Creating process-real!!! Byak@ Code Samples & Tips 6 10-06-2008 01:59 PM
Process and Basic4ppc? Byak@ Questions (Windows Mobile) 3 07-22-2008 04:05 PM
Shell and wait while process is running g0dspeed Questions (Windows Mobile) 2 03-22-2008 09:48 AM


All times are GMT. The time now is 10:36 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0