FileClose doesn't seem to close switch handle value to 0

 

Hi all,

I have a working function that identifies whether the graph has a new bar or not in some time frame. After the new bar is made I want to write to the same file from several programs (currencies) with all using similar script (program). The problem is that when two different currency pairs try to write something to the file only one of them will add its info and the other one (probably because the file was opened by another currency) doesn't add its information. So i tried to make a workaround test where I would check whether the handle is 0 or 1 and based on that either wait or write the info to the file. Here's the script, I'm trying to make work. Thanks for all your ideas/input in advance!

int loopcount=1

if(isNewBar())
   {

         int signals=FileOpen("signals.bin",FILE_BIN|FILE_READ);
         FileClose(signals); // here I create the handle
         Alert(signals); // the value is always 1 indicating the file is open

         while(loopcount<10)
         {
            if(signals>0)
            {
            Alert(signals,GetLastError()); // signal value always 1
            Sleep(1000);
            loopcount++;
            }
            else
            {
            Alert("signals=0"); // never get this
            signals=FileOpen("signals.bin",FILE_BIN|FILE_READ|FILE_WRITE);
            FileSeek(signals,0,SEEK_END);
            FileWriteDouble(signals,some info,DOUBLE_VALUE);
            FileSeek(signals,0,SEEK_END);
            FileWriteDouble(signals,some info,DOUBLE_VALUE);
            FileClose(signals);
            }
         } 
   }
 
Try opening the file in Notepad while the loop is running . . .
 
  1. FileClose doesn't seem to close switch handle value to 0

             Alert(signals); // the value is always 1 indicating the file is WAS open
    
             while(loopcount<10)
             {
                if(signals>0)
                {
                Alert(signals,GetLastError()); // signal value always 1
    Why would you expect your int to suddenly change?
  2.             signals=FileOpen("signals.bin",FILE_BIN|FILE_READ|FILE_WRITE);
    

    What are Function return values ? How do I use them ? - MQL4 forum Did the file open? You don't check. It failed if another thread has the file open.

    Check, wait, and retry. Or use a mutex.

 

Thanks for your input! The file can be opened and as I was playing around I added the cycle break to the code. Now it seems it will write into the file first currency information but not the second one which gets an error of 4002 from somewhere (Array index is out of range.). Here's the code I tested.

 

if(isNewBar())
   {

         int loopcount=1;
         int positsioonid=FileOpen("positsioonid.bin",FILE_BIN|FILE_READ);
         FileClose(positsioonid);
         //Alert(positsioonid);  

         
         while(loopcount<10)
         {
            if(positsioonid>0)
            {
            Alert(positsioonid,GetLastError());
            Sleep(1000);
            loopcount++;
            }
            else
            {
            Alert("positsioonid=0");
            positsioonid=FileOpen("positsioonid.bin",FILE_BIN|FILE_READ|FILE_WRITE);
            FileSeek(positsioonid,0,SEEK_END);
            FileWriteDouble(positsioonid,sorting1,DOUBLE_VALUE);
            FileSeek(positsioonid,0,SEEK_END);
            FileWriteDouble(positsioonid,sorting2,DOUBLE_VALUE);
            FileClose(positsioonid);
            break;
            }
            break;
         }
}
 
In addition while the code is set up to be renewed every minute it doesn't get to the second part of the loop which should say "positsioonid=0" and instead of that it gives me 4002 error every minute.
Reason: