|
hi everyone!
i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows.
is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access.
i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work.
do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file?
thanks.
stephan.
|
|
|
|
|
|
so as far as i already read it is not too easy to just realize this within some minutes. it might be a trial and error until you find an acceptable solution for your own problem, right?
i will try myself by tomorrow.
but if someone already has a short solution "on the fly" please add it to my posting. i guess this might help everyone a lot. (some solutions might also be found in the links in the last posting by Giorgi Dalakishvili).
thanks.
stephan.
|
|
|
|
|
i just found a simple solution but i am not very happy with it. i coded a function private bool IsAccessible(string Path) which checks, if the file is accessible or not. if it is the access trying to open the file is closed again, it returns true and the other operations continue. if it is not accessible, then the io-exception is caught and it returns false.
when this function is called it is combined with a sleep(1000), meaning it tries to access the file, if it is accessible then it continues without waiting, if it is not, it waits for 1 sec. well you could reduce the amount of time for waiting to a fair value depending of the estimated time until the file should be ready. but after 1 sec most files are copied and ready for access 
|
|
|
|
|
Hi Stephan,
Before accessing a file for your processing, try opening a file with a WRITE access on it. If the file open fails with denied WRITE access, then another thread (Windows) might be using it.
U can use this logic for any case (Files being copied, New file being created by another thread, etc.)
Hope this helps...
Regards,
Krista
|
|
|
|
|
Not sure why i don't seem to get this but i would like to write some records to a .txt file. At the end of all records, would like to keep a number which signifies the count. So if there are 5 records, the last line in the file will have "5".
If i have to add 10 more. I read the last line, add 10 to the number to total 15. etc.
I have been using StreamReader and Writer to do this, but i find that there is a limitation
I really want to be able to:
Open the file
Check the last line to see a number count
Append more data to file, update the number count.
Close the file.
if (!FileExists(ReportFile))
{
File.Create(ReportFile);
}
StreamReader stream = new StreamReader(ReportFile);
//How would i get to the end of the file and only read a line that says:
Count:100
where the statement "Count:" is constant, but the number will vary as we add
records?
Then i write the records:
StreamWriter filewrite = new StreamWriter(ReportFile, true);
filewrite.WriteLine(Rec);
filewrite.Close;
File
|
|
|
|
|
Wouldn't it be easier to write the record count in a header row at the start of the file? Then you wouldn't have to read to the end of the file to retrieve this information.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
I actually do that:
StreamReader stream = new StreamReader(QueryReportFile);
buf = stream.ReadLine();//Read in th efirst line which is the count
int count = (int)buf;
count++;
stream.Close
My question here is that after i read the count and add to it, i have to update the first line with the new count. Do i have to create another StreamWriter just for that line, then execute below. How can i do this with one StreamWriter??
StreamWriter filewrite = new StreamWriter(ReportFile,true);
filewrite.WriteLine(RQ);
filewrite.Close();
|
|
|
|
|
i have a login page wid login check up loop as
if (userType.Equals("Administrator"))
{
Response.Redirect("successPage.aspx");
}
else if (!userType.Equals("Administrator"))
{
errMessage.Text = " Login Incorrect!! " ;
}
login works well with correct details as needed ..but if i enter wrong details.. it displays nothing.. i want to display text above... how to do this ??
|
|
|
|
|
I'm not sure why the message is not displayed. But why are you using an else if ?
If the user is an admin it will pass through the first if . If it gets to the else part then we already know they're not an admin, there should be no need to check again.
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|
ya its fine !!! but just like i want to display a text message .. when u enter wrong details !!!
|
|
|
|
|
Hello everyone,
I am working on a BHO and created a Class Library. I am using the Attach Process (Tools -> Attach Process...) to attach my Class Library Assembly(MyProject.dll and Interop.SHDocVw.dll) to the running process.
My question is, using Windows Module (Debug -> Windows -> Modules) should I be able to see the two mentioned dll files also loaded? I think I should but would like to know your opinion on this. If this is not the right way to check what file has been attached then how can I check to see what has been attached to the running process? I ask since when I use the mentioned approach I don't see any of the mentioned DLL file loaded which leaves me with the question: "What has been attached/loaded then?!!!"
I don't have to register the BHO if I am using Attach Process approach to attach the Assembly. Do I have to? I don't think I should since I am attaching it manually but I was told by someone that I should but I am not 100% sure on that. What do you think?
Thank you very much for all your help.
Khoramdin
-- modified at 12:41 Monday 19th November, 2007
|
|
|
|
|
Hello,
Well, i'm messing with C# for one month now (sort of new to me), and i've studied a bit how thread works in .NET 2.0, but i'm facing an annoying problem i can't fix ! Here it is :
Let's assume i've got the main thread in my windows Form, and two others i'm trying to manage within this Form. A big point of interest to me is to know for sure a thread has ended, so it seems kinda easy at first. Thing is, there are some stuff that i can't make it work, so i'm here to ask for your help !
The main thread is rather big with some processing, and handles the two others.
One of the others is a small one, looping to wait for data input. It looks like this :
public void dataReadThread(){
try{
while(true){
if(eventAbortRead.WaitOne(5,false))break;
dataReceived = receiveData();
if(dataReceived)eventProceedNext.Set();
}
}
catch (...) {}
eventReadThreadHasExited.Set();
}
So it's supposed to allways try to read data, and send an event when it get a response. If the thread is asked to abort, the break statement leaves the while and then fires the event eventReadThreadHasExited.
Well, thing is that works without any issues because the function receiveData is quite simple.
Now the processing thread is rather similar in it's architecture, but a bit more complex !
private void processInstructionThread(){
while(!stop){
if(eventAbortProcess.WaitOne(5,false))break;
stop = processLine();
if(FLAG_WaitAnswer){
WaitHandle[] wh = new WaitHandle[2] {eventProceedNext, eventAbortProcess};
if(WaitHandle.WaitAny(wh) == 1)
break;
}
}
eventProcessThreadHasExited();
}
So there's no huge difference between these two threads, except the processLine function is really complex and does a lot of processing.
Now on the main thread, my thread are started and processing, and everything goes fine ... until i try to abort the threads ...
To do so, it seems quite easy, but to me it's not (maybe my code is totally wrong, but looked rather suitable to me). Here is what i'm trying to do and where i get the issues on the main thread :
protected override void OnClose(EventArgs e){
this.eventAbortRead.Set();
if( ! eventReadThreadHasFinished.WaitOne(5000, false) ){
}
this.eventAbortProcess.Set();
if( ! eventProcessThreadHasFinished.WaitOne(5000, false) ){
}
}
The thing is that for some unknow reason, it sometimes works and sometimes doesn't. Most of the time the read abortion is doing fine, but the Process abortion is impossible ! It looks like doing a WaitOne() in the main thread pauses or interrupts the other threads at some time !
I've also tried to code the waiting with FLAGs (with a while(!FLAG) Thread.Sleep(0); ) and it seems to do the same error .
I've also tried to code these thread using backgroundWorker but no success
Well, seems a bit weird to me, and i think i miss something enomous, but can't figure out what !
So if any of you has any idea, i'd be glad to test it !
Best Regards,
Mamat,
|
|
|
|
|
I'd say your approach is wrong. Constantly polling threads to see if they're done, aborting threads, using lots of synchronization events; these kind of things raise flags to me.
Let's step back for a moment. Ignoring possible implementation details, what are trying to accomplish?
|
|
|
|
|
Hello, and thank you for your reply !
Well, this is what i thought, but i don't know how to do that another way
I'm writing an interactive application, with a RichTextBox containing a script to process and get the answer to these lines. It has to handle debug features such as run, step by step, breakpoints, halt, and so on.
The thing is, i've got a main thread (my windows form), and i have to do two things :
1/ Have a background thread to listen to data input, that may come at anytime (i.e. even when the script is not processing, but have the possibility to disable it in order to process some specific commands)
2/ Have another thread to process the script lines, one by one or the whole script depending on the debug button pressed.
To me, i don't see another way to do that. Considering the reading thread for example, it has to loop for data input, unless i tell it to stop/pause. So i thought it was the right way to implement this feature.
Now after reading from you, i guess it's not right to start/stop constantly the threads, because for the step by step for example, i run the process thread just for one line and then i cut it off before the user asks for another interaction. I've also thought at using the events to pause the threads but it wasn't doing what i expected (maybe i've done some mistake on that implementation, i tried it in a hurry). But the thing is that i need to know when a thread is running, then have the ability to tell it to stop/pause and be sure it has done processing before performing the remaining operations.
As i have two loops (one for each thread), i can't find out another way that polling at each loop if they're asked to stop/pause.
I hope it's more clear for you now, and i also hope you can give me hand to find out how to implement that correctly !
Best Regards,
Mamat,
|
|
|
|
|
I've got a databound datagrid from SQL Server Express '05 and one of the columns is the foreign key to another table. Right now this column just displays a number (the ID) but I want it to display the name associated with that ID.
I'm sure that this is possible as I've seen it done with databound comboboxes.
|
|
|
|
|
Change the query for your datagrid to JOIN to the table that contains the names for IDs. Include the 'name' field in your query output and bind your grid column to this field instead of the ID field itself.
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Paul you beauty! Thank you so much, you just made my week year!
|
|
|
|
|
No problem. Happy to help
Paul Marfleet
"No, his mind is not for rent
To any God or government"
Tom Sawyer - Rush
|
|
|
|
|
Does anyone know how to best lock access to the object during enumeration? Eg, is it a bad/good idea to use a Monitor.Enter in the constructor and a Monitor.Exit in the dispose function? I know, I could put a lock around, but I really need this to happen automatically.
Cheers.
|
|
|
|
|
Check out this [e-book]. It has some really nice examples on (automatic) locking and when (not) to use it in chapter 2. You might want to look at the Mutex class.
Standards are great! Everybody should have one!
|
|
|
|
|
|
When you compile this code:
IEnumerable<string> Test() {
lock (someObj) {
yield return "a";
yield return "b";
}
}
Then the C# compiler creates an enumerator class that calls Monitor.Enter on the first MoveNext call; and calls Monitor.Exit in the third MoveNext call, or in the Dispose method if Dispose is called between after the first MoveNext call and before the third.
foreach will automatically dispose the enumerator, but I've seen people write code like
IEnumerable<MyType> myCollection = ...;
if (e.GetEnumerator.MoveNext()) {
So yes, using Monitor.Exit in the Dispose function is the way locking is meant to happen in enumerators, but make sure that your team is aware of the fact that enumerators must be disposed!
|
|
|
|
|
I am trying to find every control on my page with "txt" in the id, they are all my textboxes. The following bit of code is working fine and duly goes into the inner condition for some of the textboxes on the screen. But not all.....will this only work on textboxes that are visible at runtime as I have some sent to txtBox.visible = false at startup.
foreach (Control c in Page.FindControl("MyPage").Controls)
{
if (c.ID != "" && c.ID != null && c.ID.ToString().IndexOf("txt") > -1)
{
TextBox txtClearBox =new TextBox();
txtClearBox.ID = c.ID;
txtClearBox.Text = "";
}
}
Any advice on clearing all my textboxes would be great.
Thanks in Advance
|
|
|
|
|
Instead of checking that the name isn't null, and that it contains "txt"
Can you not use Control.GetType() and see if it is a textbox?
And... are you making a new textbox everytime you want to clear it? Why not just set the text to ""?
My current favourite word is: PIE!
Good ol' pie, it's been a while.
|
|
|
|
|