|
hey,
When I'm trying to delete one of my created files. My program saids it is in use (by himself) but that's because the program is to fast. When I let him wait 10sec the lock is gone... But that not zo good if you have to delete 20-50 files.
How can I unlock them automatically?
Thx!!!
|
|
|
|
|
How are the files created? Did you maybe forgot to call close on some streams?
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
There are all created with:
new DirectoryInfo(Path.GetDirectoryName(path)).Create();
StreamWriter writer = new StreamWriter(path);
writer.WriteLine(content);
writer.Close();
But than the locks are fine. But after all the files are created I zip them with "SharpZipLib" and is places locks that last realy long...
thx
|
|
|
|
|
Then there's a bug in the zip library, or in how you're using it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I don't think so everything is closed:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using log4net;
using ICSharpCode.SharpZipLib.Zip;
using System.Collections;
using System.Collections.Specialized;
namespace InterfaceCollector.domain.DAL
{
class Zip
{
private string destination;
private readonly ILog log = LogManager.GetLogger(typeof(Zip));
static Hashtable excludeDirs = new Hashtable();
static string startFolder = String.Empty;
static string outputZipPath = String.Empty;
static string password = String.Empty;
public Zip() {}
static private Zip instance = null;
static public Zip Instance
{
get
{
if (instance == null)
instance = new Zip();
return instance;
}
}
public void ZipIt(string Path,string password)
{
log4net.LogManager.Shutdown();
try
{
ArrayList ar = GenerateFileList(Path); // generate file list
// find number of chars to remove from orginal file path
int TrimLength = (Directory.GetParent(Path)).ToString().Length;
TrimLength += 1; //remove '\'
FileStream ostream = null;
byte[] obuffer;
ZipOutputStream oZipStream = new ZipOutputStream(System.IO.File.Create(Destination)); // create zip stream
if (password != String.Empty) oZipStream.Password = password;
oZipStream.SetLevel(9); // 9 = maximum compression level
ZipEntry oZipEntry;
DateTime timeStap = DateTime.Now;
string output = "";
int lenghtWrittenString = 0;
double percent = 1;
int countFiles = 0;
foreach (string Fil in ar) // for each file, generate a zipentry
{
countFiles++;
percent = countFiles * 100 / ar.Count;
//Display % on progressScreen
if (timeStap <= DateTime.Now)
{
timeStap = DateTime.Now.AddMilliseconds(1000);
ApplicationDirector.Instance.removeCharachtersFromProgressScreen(lenghtWrittenString);
if (percent < 100.0)
{
output = "\r\nCreating zip-file (" + percent + "%)";
ApplicationDirector.Instance.writeToGui(output);
}
lenghtWrittenString = output.Length;
}
oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
oZipStream.PutNextEntry(oZipEntry);
if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(Fil);
obuffer = new byte[ostream.Length]; // byte buffer
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
ApplicationDirector.Instance.removeCharachtersFromProgressScreen(lenghtWrittenString);
ApplicationDirector.Instance.writeToGui("\r\nCreating zip-file (Completed)");
oZipStream.Finish();
oZipStream.Close();
}
catch (Exception ex)
{
ApplicationDirector.Instance.writeToGui("Error during zipping.");
Logger.Instance.ExtraInfo.Add("Zip-File nog created" + ex);
}
}
private static ArrayList GenerateFileList(string Dir)
{
ArrayList mid = new ArrayList();
bool Empty = true;
foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
{
mid.Add(file);
Empty = false;
}
if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0) // if directory is completely empty, add it
{
mid.Add(Dir + @"/");
}
}
foreach (string dirs in Directory.GetDirectories(Dir)) // do this recursively
{
// set up the excludeDir test
string testDir = dirs.Substring(dirs.LastIndexOf(@"\") + 1).ToUpper();
if (excludeDirs.Contains(testDir))
continue;
foreach (object obj in GenerateFileList(dirs))
{
mid.Add(obj);
}
}
return mid; // return file list
}
public string Destination
{
get { return destination; }
set
{
string path = Path.GetDirectoryName(value);
path = path + "Agfa Zip - "+Path.GetFileNameWithoutExtension(value) + "\\" + Path.GetFileName(value);
destination = path;
Directory.CreateDirectory(Path.GetDirectoryName(destination));
}
}
}
}
|
|
|
|
|
But you have to know, he releases the locks It just takes to long
|
|
|
|
|
That means, something is NOT closed, but it is eventually garbage collected.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
that works thx!
GC.Collect();
GC.WaitForPendingFinalizers();
|
|
|
|
|
That's a hack. You should instead figure out what's going wrong with the zip library.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
If we have time at the end :->
|
|
|
|
|
Well, I'd think if it's a problem in the framework, then others will have encountered it. If you find nothing on google, it's a bug in your code. 'If we have time at the end', means, we're going to leave it broken. At least, that's what I'd expect it to mean.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
|
What do you mean with that?
|
|
|
|
|
hello again,
well I made a parent child form in C# 2.0 windows application with sql server 2000 and i'm having troble in deleting the parent record bcz it gives a constraint error that the table is also reffered to in another child table that is not being used in the current form. so can u plz tell me what I should do to delete that parent record when there are references in its child table.
thanks in advance
Rocky
|
|
|
|
|
i have also face the same problem in java.
At that time i have to delete the record from the child record and then to the parent node.
U can't simply delete parent record.
Try this out and let me know.
|
|
|
|
|
This isn't strictly true. You can do a cascading delete, but only if the database supports it and the tables have been set up to support it. I wouldn't recommend it, but you can.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
If some constraints are specified between two tables. How do to overcome the constraints.
If the above sentence is true means. How do you delete the data with deleting the data from the child record.
Regards,
A Jabeer Ali
|
|
|
|
|
It works by setting up a cascading delete on a foreign key. Basically, this means that when you delete from the master table, related entries are deleted from child tables. In SQL Server, you do this by adding ON DELETE CASCADE to the foreign key constraint.
As I noted above though, it is generally best to avoid doing this. You really need to have more control over what you are deleting because sometimes you may end up deleting things that you didn't want to.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
well yes I didnt have the cascade option there but now I think its a better idea to have more control over it bcz these tables are bill and billDetails so one shouldnt be able to delete the whole bill along with all the detail records. Right?
Rocky
|
|
|
|
|
well I have a little question: how do we chk for the existance of child records in C#. is it something like getChildRows() or something??
|
|
|
|
|
You actually need to query the tables that are linked to see if they point back to the row.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
draw an organization chart on a form but the rectangles I drew exced the size of the form what can I do to make the form sizable
merwa
|
|
|
|
|
Look at Form property -> FormBorderStyle
If you can't find time to do it right the first time how are you going to find time to do it again?
|
|
|
|
|
EDIT2: Solved, thanks to CPallini. I was stupid not to see it in the first place myself.
EDIT: The code that generates the random bools is in a different assembly from the code that calls it - caching is unlikely to work. Due to design constraints, I cannot change GetRandomBool's signature
I am trying to generate pairs of bools in a loop. Here's the relevant code with method and variable names changed:
for(int i = 0; i < rounds; i++)
{
bool firstPlayersChoice = GetRandomBool();
bool secondPlayersChoice = GetRandomBool();
}
public static bool GetRandomBool()
{
System.Random randomNumberGenerator = new System.Random((int) DateTime.Now.Ticks);
int randomNumber = randomNumberGenerator.Next();
if(randomNumber % 2 == 1)
{
return true;
}
else
{
return false;
}
}
When I step through the code, executing the program step by step, the results are fine. However, when I run the program directly, I'm seeing pairs of the same value, i.e., many instances of (true, true) or (false, false). Statistically, half my outcomes should be identical pairs, but even when rounds is 100, I barely see a non-identical pair like (true, false).
I'm guessing the program runs so fast that the two calls to GetRandomBool generate identical values for randomNumberGenerator and hence generate identical bools.
Is there some way I can get bool pairs that are statistically similar to what I'd expect?
-- modified at 9:10 Thursday 22nd March, 2007
-- modified at 9:13 Thursday 22nd March, 2007
-- modified at 10:58 Thursday 22nd March, 2007
Cheers,
Vikram.
The cold will freeze our stares
We won't care...
|
|
|
|
|
I think you get the same Ticks twice.
Have you tried re-using the random number generator?
for(int i = 0; i < rounds; i++){
System.Random randomNumberGenerator = new System.Random((int) DateTime.Now.Ticks); bool firstPlayersChoice = GetRandomBool(randomNumberGenerator);
bool secondPlayersChoice = GetRandomBool(randomNumberGenerator);
}
public static bool GetRandomBool(System.Random randomNumberGenerator){
int randomNumber = randomNumberGenerator.Next();
if(randomNumber % 2 == 1)
{
return true;
}
else
{
return false;
}
}
____________________________________
There is no proof for this sentence.
|
|
|
|