|
I don't see what is wrong; maybe you are nesting things (not shown) or running several file operations on different threads (not shown).
One way to get it to fail would be to have SrtFileParser.parseString call ecrireFichier.
If relevant, all this can be checked by adding some logging, as in Console.WriteLine("Now opening...") and the like, and observing the logs.
Here is a suggestion:
your getLignesFromFile() method reads the entire file at once in a complex manner; you could have used File.ReadAllText() or File.ReadAllLines() which are much harder to get wrong.
So replace all your file reads by those methods.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks for your suggestions. I removed some code lines in the listing I posted earlier, but it was only Write() calls.
There's no other thread in the app dealing with the files. The whole code for files is here.
I added some debug info with Console.WriteLine, but it confirms what I saw earlier.
Anyway, I found something new : if I try to open the second file with the second argument to StreamWriter set to true (so it appends data instead of overwriting), there's no exception thrown.
I tried the code on 2 differents PCs, both VC# 2008 and VS 2008 Pro give me the same problem !
|
|
|
|
|
Still doesn't make any sense to me.
Question: is your app writing the same file more than once?
I mean:
open file for create/overwrite
write file
close file
open file for create/overwrite
write file
close file
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks for your help !
No, it doesn't write the same file several times.
The only code dealing with files is posted above and here's what it does :
At first :
Open file 1 for reading
Read data from file 1
Close file 1
If file 2 exists :
Open file 2 for reading
Read data from file 2
Close file 2
File 1 name is stored between these, so the user can save clicking on a button.
Open file 1 for writing
Write data to file 1
Close file 1
If some data is for file 2 :
Open file 2 for writing <-- that's where it crashes
Write data to file 2
Close file 2
And if I try to delete file 2 before opening it for writing, there's no error after, and the file is actually deleted.
I know it's strange thing, I can't figure why it doesn't work !
|
|
|
|
|
With that sequence of operations, I could explain if and only if the two destination files are the same.
(you didn't show the calls to ecrireFichier and ecrireTraduction)
Suggestions:
1. check your code, make sure both sNomFichier values are different;
2. insert a Thread.Sleep(5000) before opening the second file.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
The only difference in the names is the extension. And the exception shows the name of the second file (with .rak extension).
I tried with Thread.Sleep(5000); it freezes well for 5 seconds, but the exception is still here !
The only way I found to bypass this exception is, before calling the ecrireTraduction function, to delete the file :
FileInfo fInfo = new FileInfo(sFichierTemp);
if (!fInfo.IsReadOnly)
{
fInfo.Delete();
}
The STRANGE thing is that it can't be opened for writing for overwrite, but it's ok for open for append or delete the file !
|
|
|
|
|
why the IsReadOnly check?
are you playing with the file properties? if so, you should have said so from the start!
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I found !!
After writing the data file 2 for the first time, I set its Hidden property to true; The goal was to make things transparent to the user.
If I remove this attribute to the file, and try to write it, it works ! I've never heard of such behaviour ...
Thanks a lot for your help about that !
|
|
|
|
|
good.
Are you running on a Virtual Machine? which one?
and which Operating System?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
No it's not in a virtual machine.
I tried it both on XP and Seven RC, and it's ok now !
|
|
|
|
|
Thanks for your help,
there is something even more strange : before opening the StreamWriter that throws the exception, if I try to delete the file, there's no error. It can be deleted, but can't be opened for writing.
|
|
|
|
|
Member 4052498 wrote: It can be deleted, but can't be opened for writing.
That doesn't make sense to me.
If that's correct, I would say something is very wrong.
I suggest you delete the file, reboot the system, rebuild the app, and try again.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I'm a newbie in computer programming so please have this in mind.
I have a class called Pinger which does basic pinging of hosts on a network. I'm currently implementing a network scanner so I create an array (254) of Pinger's. The problem occurs when I try to dispose the array after I've done the scan. I dispose every Pinger in the array - the Pinger class contains Dispose methods (GC.SuppressFinalize).
Still, the array of Pinger's still uses a lot of rescources on my system and I can clearly see the memory beeing freed when I close my application.
Does anyone have any idea on how to correctly get rid of the rescources being used (after I'm finished using them)?
I have the complete project, but couldn't find a way to upload it so if anyone want's to see it let me know how I can send you the project.....
private void AllPingsFinished()
{
counterForInsert = 0;
foreach (Pinger str in _pinger)
{
if (str != null)
{
str.Dispose();
}
}
}pre>
|
|
|
|
|
I don't think that you can remove object from Collection while it's looping
In Other word
<br />
<br />
foreach ( string str in MyList)<br />
{<br />
MyList.remove(str);<br />
}
List<string> ListForDetele = new List<string>();<br />
foreach (string str in MyList)<br />
{<br />
ListForDelete.Add(str);<br />
}<br />
<br />
foreach(string str in ListForDelete)<br />
{<br />
MyList.Remove(str);<br />
}
Have a good day ,
I know nothing , I know nothing ...
|
|
|
|
|
I will try this!
Thanks!!
|
|
|
|
|
Hi,
it is wise to call Dispose() on an object you no longer need, so it can release its unmanaged resources in a deterministic way. However that does not take care of the managed resources, i.e. the object is still alive (as your array is alive and holds a reference to it).
e.g. if each Pinger holds a thread, then that thread (and its stack memory) are still allocated (unless you cleaned them too in Dispose).
so it would also be good to remove the reference from the array as soon as you don't need them.
Kaare Tragethon wrote: I can clearly see
do you? how? Task Manager is not the right place to get accurate information on this subject. It shows the
working set, not how much memory the app NEEDS.
The differences are:
- the app may need to hold that amount of data;
- or the app may have asked that amount of memory because that was the fastest way to get things done, but in reality lots of memory are unused INSIDE the app (instead of inside the Window memory pool).
Test: minimize your main form. Does that dratically reduce the "memory requirement"? if yes, point proven.
More comments:
- why do you need 254 Pingers all at once? do they really all work in parallel? do you have that many threads?
AFAIK TCP/IP will choke. I tend to use say 8 threads and let each of them ping the addresses one after another (one job list, each thread picks the next job in turn).
- I tend to use a plural noun for a collection, so I would write foreach(Pinger pinger in pingers)...
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks a LOT to both of you!
I will definetively try a combination of both your answers (max 8 pingers, and add them to a list)!
Best regards
|
|
|
|
|
Hi , have a good day
I have the Class
class MyClass
{
private int _myvar;
public int MyVar
{
get
{
return _myvar;
}
set
{
_myvar = value;
}
}
public void ChangeMyVar(int NewValue)
{
_myvar = NewValue;
MyVar = NewValue;
}
}
Which method is the standard One ?
Method 1 , or Method 2 ?
thank you
I know nothing , I know nothing ...
|
|
|
|
|
Why would you need ChangeMyVar(int) if you have your public int MyVar property? That would be used to interface with the private _myvar, you woulnd't need the method ChangeMyVar(int).
|
|
|
|
|
Thank you for your replay ...
I made a simple DAL class , and it's used to Change the myvar variable inside it's Class
I know nothing , I know nothing ...
|
|
|
|
|
Stark DaFixzer wrote: Which method is the standard One ?
It depends. Like most things in programming.
Suppose that your code looked like this:
class MyClass
{
private int _myvar;
public int MyVar
{
get
{
return _myvar;
}
set
{
_myvar = value;
GoOffAndDoSomethingThatIsDoneWhenMyVarChanges(value);
}
}
public void ChangeMyVar(int NewValue)
{
_myvar = NewValue;
MyVar = NewValue;
}
GoOffAndDoSomethingThatIsDoneWhenMyVarChanges(int newValue)
{
}
}
Which method you use from within the class would depend on whether you wanted the 'side effect' to happen ('side effects' are usually, though not always, events).
From outside the class there is no choice you must use Method2.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Thank you , that is exactly what I need to understand ....
I know nothing , I know nothing ...
|
|
|
|
|
So I hope this has a Simple answer.
I have a program with 6 Options, they can be either RadioButtons or CheckBoxs, But I need to know how to limit the options to 3 or less?
Is there an easy way to do this with out checking how many are true and building a checker?
modified on Thursday, October 15, 2009 11:15 AM
|
|
|
|
|
Hi,
I have no idea what you mean by ControlButtons, did you mean CheckBoxes?
Anyway, the only way I know would be to attach this same handler to each of the relevant Controls; then either have a collection that holds the relevant controls or tag them somehow so you can easily enumerate them:
void CheckedChanged(object sender, EventArgs e) {
int count=0;
foreach(Control c in relevantControls) {
RadioButton rb=c as RadioButton;
if (rb!=null) count++;
}
if (count>MAX) {
RadioButton rb=sender as RadioButton;
if (rb!=null) rb.Checked=false;
}
}
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Yea I meant CheckBoxs or RadioButtons sorry, and yea I was looking for a way other than that.
|
|
|
|