Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I have a question, I would like to like the WPF application saves time similar to the one in the rankings. Time and number of attempts is not a problem to list but the problem occurs when I want to sort it in a text file.
EDIT:I'll start over again
EDIT2: advance

What I have tried:

C#
private void WriteText(string strPath, TimeSpan tsDuration)
{
    
    var list = new List<TimeSpan>();

    
    string line;
    using (var sr = new StreamReader(strPath))
    {
        while ((line = sr.ReadLine()) != null)
        {
            list.Add(TimeSpan.Parse(line));
        }
    }

    
    list.Add(tsDuration);

    
    list.OrderByDescending(i => i);

    
    using (StreamWriter str = new StreamWriter(strPath, false))
    {
        str.WriteLine(tsDuration / suma);
        
    }
}

readonly string path = @"C:\Users\info\Desktop\žebříček.txt";
TimeSpan duration = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);//this is from other methods

suma=number of attempts
duration=duration in milliseconds

With this code now it only writes the last value and I don't know why...

If you have any advice or an idea how to do it differently and I will be very happy. Thank you.
Posted
Updated 7-Jun-21 23:20pm
v4

The error message says it all: the file you are trying to read is in use.

When you open a file for writing, the stream acquires an "exclusive lock" on the file to prevent the contents changing while it is being read and potentially invalidating the data.
When another stream tries to open the same file, it gets rejected until the original stream has been closed.

The most likely reason you are getting this is that somewhere else in your app you are opening the file and not closing it properly, even if the stream itself is no longer accessible.

The best way to avoid this is to use a using block around all stream creation, so that the system will close and dispose the stream when you are finished with it and it goes out of scope:
C#
private void writeText(string strPath, TimeSpan tsDuration)
        {                   
        using (StreamWriter str = new StreamWriter(strPath, true))
            {
            str.WriteLine(tsDuration/suma);
            sort(strPath);
            }
        }
But in your case, because you are trying to sort the file you have open that won't help you fix this.

I'd strongly suggest that you start thinking about using a database instead of text files - they provide much better facilities for this kind of work than flat text files do.

Oh, and by the way - your sort code does nothing useful at all: sorting lines you read into memory doesn't affect the file content in any way, and the sorted data is discarded as soon as your sort method exits ...
 
Share this answer
 
Comments
Member 15170612 27-May-21 5:16am    
I added my code, but I don't know if it will help
OriginalGriff 27-May-21 6:00am    
Look at your code, look at what I said.
You are causing that error in the code you show!
BillWoodruff 28-May-21 4:40am    
+5 with extra gold star for merciful patience
You have raised this question a number of times and you still appear to be struggling.

Think carefully about what you are trying to do, and the order in which you do it.
1. Call the Read method to read the records from the input file.
2. Any other processing that needs to append any new records to the record set.
3. Call the sort method to sort the records, remembering to return the sorted data.
4. Call the write method to write the sorted data to the output file.


Keep the methods separate so they do not interfere with each other. Keep input and output files separate to avoid the conflicts that you are experiencing.
 
Share this answer
 
Comments
Member 15170612 27-May-21 6:28am    
You're right i'm pretty lost
Richard MacCutchan 27-May-21 6:57am    
you need to learn to stop and think before you start coding. Write things out on paper, one step at a time. And keep your methods simple, so that they only do one thing. The problem you got into above is because you are trying to do the reading, sorting and writing, inside the same method. This is especially important when you are fairly new to the subject.

Once you are happy that the overall logic meets your requirements then you can start coding.
BillWoodruff 28-May-21 4:40am    
+5 with extra gold star for merciful patience
Richard MacCutchan 28-May-21 4:52am    
Thanks. I do wonder what, and how well, some of these new people are being taught.
BillWoodruff 28-May-21 7:44am    
A test of our psychic powers: to discern from posts like these those who are not learning because:

1. they are not being taught, or being taught poorly
2. they have not made an effort, not done homework, etc.
3. they have made an effort, but are not making progress because:
a. they do not have aptitude
b. they have aptitude but have taken on a challenge they are not prepared for
c. they do not know how to use the resources of the net and/or documentation.

Kong Fu-zi (Confucius) on education:

http://www.san.beck.org/CONFUCIUS3-How.html

I do not enlighten those who are not eager to learn,
nor arouse those who are not anxious
to give an explanation themselves.
If I have presented one corner of the square
and they cannot come back to me with the other three,
I should not go over the points again.6

The case is like that of someone raising a mound.
If he stops working,
the fact that it perhaps needed only one more basketful
makes no difference; I stay where I am.
Whereas even if he has not got beyond leveling the ground,
but is still at work,
the fact that he has only tilted one basketful of earth
makes no difference. I go to help him

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900