|
Hello,
i have a problem. i have a List<string> and in another Thread a Loop which is running and checking the List (Foreach-Loop). Now, the loop is running and a new item is added to the list - > i get an Exception, and my application breaks!
So, how to solve this problem?
|
|
|
|
|
You will have to make it "thread safe" (Google will help here), but basically you need to lock your list before traversing it, and also before attempting to add to it. See here for starters[^]
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Hi,
Im making a trial of one of my applications, and i want the user to only be able to run the application 5 times..
now, my original plan had been:
int numint = 1;
TextWriter trialWrite = new StreamWriter("oote.oost");
trialWrite.WriteLine(numint+1);
trialWrite.Close();
So it would add +1 to the value of numint each time the application loads, but its not working right, and it doenst add to the number inside the text (oost) file in question.
I also tried +1 but that doesnt work either.
Its in the form_load event.
Can anyone tell me how to add +1 to the value of the number already in my text file?
thanks,
Ben.
|
|
|
|
|
You need to set the value of numint by reading the value in "oote.oost" BinaryReader.ReadInt16/32/64(). To do this you will need to use a BinaryWriter (BinaryWriter.WriteInt16/32/64() ) instead of your StreamWriter , but that is better since the file will be slightly obfuscated for casual meddlers.
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.”
|
|
|
|
|
Ok, Can you give a code example please?
Thanks,
Ben.
|
|
|
|
|
OK.
This is to go in the Form.Load, as per your original question:
private void Form1_Load(object sender, EventArgs e)
{
string fileName = "oote.oost";
int numint = 0;
if (File.Exists(fileName))
{
BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
numint = binReader.ReadInt32();
}
catch (EndOfStreamException ex)
{
MessageBox.Show("Unable to read [numint]", "File Read Error");
}
finally
{
binReader.Close();
}
}
if (numint >= 5)
{
MessageBox.Show("Sorry too many runs", "Limit Exceeded");
Application.Exit();
}
else
{
BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
try
{
binWriter.Write(numint + 1);
}
catch
{
}
finally
{
binWriter.Flush();
binWriter.Close();
}
}
}
However, I do not like doing Application.Exit() from within the Form.Load handler. I cannot give any meaningful reason, it just seems wrong to me to do all the form loading and initializing stuff, if you aren't going to let them in.
So as an alternative you can do pretty much the same in the Main() method of the Program class. Here:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
string fileName = "oote.oost";
int numint = 0;
if (File.Exists(fileName))
{
BinaryReader binReader = new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
numint = binReader.ReadInt32();
}
catch (EndOfStreamException ex)
{
MessageBox.Show("Unable to read [numint]", "File Read Error");
}
finally
{
binReader.Close();
}
}
if (numint < 5)
{
BinaryWriter binWriter = new BinaryWriter(File.Open(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
try
{
binWriter.Write(numint + 1);
}
catch
{
}
finally
{
binWriter.Flush();
binWriter.Close();
}
Application.Run(new Form1());
}
else
{
MessageBox.Show("Sorry too many runs", "Limit Exceeded");
}
}
}
Hope this helps!
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.”
|
|
|
|
|
Now all the user has to do to go beyond 5 runs is either completely remove the file, edit it with a hex editor, restore it after each run, or reinstall the program.
this answered the question, however you are well aware is not the way to implement trial limitations. real solutions would use access to a web server, or a hardware dongle.
|
|
|
|
|
I'm Aware of this fact, and I'm currently working on solutions to it
By hardware dongle do you mean memory stick?
One of the solutions im looking at is creating the file with installation, then using a If statement to see if the file exists or not, if it does, then it writes to the file, if the file doesnt exist then the application will exit.
True all the user would have to do then is make another file. But it should stop casual snoopers, All I really want it for is to make sure my friend doesnt leg it with a BETA release of some of my software, nothing too big.
thanks All,
Ben.
|
|
|
|
|
Ben Magee wrote: By hardware dongle do you mean memory stick?
not necessarily. There are products (such as WIBU Codemeter) that use a special hardware device (nowadays usually USB) to protect your app, which gets modified obviously, in a way that is supposed to be not so easy to hack; the variable cost would be around 30 euro.
for cheap software, it is possible to do it yourself e.g. based on a memory stick, as they AFAIK always contain a rather unique ID (from observation, and not guaranteed).
both methods require the hardware to be present for the app to run, which is both a hard-to-copy way of protecting, AND a burden for the user. However it is much better IMO than relying on some built-in unique ID (baseboard serial number, disk serial number, MAC address, ...) as these are not always present, some can be changed easily, and users don't want licensing trouble when changing some hardware.
|
|
|
|
|
Luc Pattyn wrote: you are well aware is not the way to implement trial limitations. real solutions would use access to a web server, or a hardware dongle
Whilst this is true in almost all situations, the OPs method might be enough, depending on the location of the "oote.oost" file, the permissions for that location and the knowledge/skill level of the users.
As I do not do commercial development I do not feel that I have the depth of knowledge to advise people on these matters. I restrict myself to trying to answer their questions. There are always people with working knowledge of such techniques that can advise the OP, if they feel it necessary.
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.”
|
|
|
|
|
Hi All,
I have custmized grid which is inherited from windows datagrid(1.0).
In this i need to implement : Shift-clicks in either list area should be supported to select all rows between the current row being selected and the last row selected.
In this grid i also have to allow user for Multiselection of rows and scroll on the data grid using Arrow Keys.
I could not implement Shift Click functionlaity.
Can any one help me how to implement "Shift-clicks in either list area should be supported to select all rows between the current row being selected and the last row selected" functionality ?
|
|
|
|
|
Hi, I was wondering if someone could give me some feedback on this method.
My goal is to join several files into one and it works but is this an effective way of doing it?
Is there something I can do to speed it up and make it more effective?
I have tried playing with the buffer-size and increasing it makes it faster up to a certain point.
private void JoinFiles(string[] files, string outputFileName)
{
int buffSize = 1024;
byte[] buffer = new byte[buffSize];
FileStream fsSave = new FileStream(outputFileName, FileMode.CreateNew);
foreach (string file in files)
{
FileStream fs = new FileStream(file, FileMode.Open);
long fileSize = fs.Length;
while (fs.Position < fileSize)
{
if (fileSize - fs.Position > buffSize)
{
if (buffer.Length < buffSize)
buffer = new byte[buffSize];
fs.Read(buffer, 0, buffSize);
}
else
{
buffer = new byte[fileSize - fs.Position];
fs.Read(buffer, 0, (int) (fileSize - fs.Position));
}
fsSave.Write(buffer, 0, buffer.Length);
}
fs.Close();
}
fsSave.Close();
}
|
|
|
|
|
Hi Johan, I think without using Win32 to read/write the files at a lower level, what you've come up with is the best that .net can offer. The method below works in exactly the same way with a couple of minor changes for memory efficiency & some personal preferences - there's no need to create a new byte[] on each loop; just use the same buffer that you define at the top. I usually use a buffer size of 4KB as a balance between memory usage & file access operations. There's no need to calculate the buffer size to ensure you don't read past the end of the file - .Read returns the number of bytes actually read, even if you asked for more than is available. Finally, a personal preference, I like to use using() blocks with the FileStream just to guarantee the file is closed (the Dispose method on Stream is actually hidden, but it is there & calls close).
Hope this helps, Rob
private void JoinFiles(string[] files, string outputFileName)
{
byte[] buffer = new byte[0x1000];
using (FileStream fsSave = new FileStream(outputFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None))
{
foreach (string file in files)
{
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
while (true)
{
int count = fs.Read(buffer, 0, buffer.Length);
if (count <= 0) break;
fsSave.Write(buffer, 0, count);
}
}
}
}
}
"An eye for an eye only ends up making the whole world blind"
|
|
|
|
|
great. less code, a single buffer, my 5.
|
|
|
|
|
Thanks a lot for you answer, it's a much better looking code and it's easier to read.
I just love getting feedback and watching how others write their code, there's a lot you can learn from that.
So thank you again...
|
|
|
|
|
I want as the installer runs, it pauses and asks for the serial key to continue installing. Just as Windows XP or Office does. I understand that this might not be related to C Sharp coding exactly but i did my software with C Sharp! I am using Visual Studio 2005. How can one configure the installer to do that?
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|
|
You can look at this [^]article at MSDN for validating the serial key.
Manas Bhardwaj
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
Hi,
I've created a Groupbox, there is a textbox in subset of it.
I've changed Groupbox's RightToLeft property to Yes, But I wanna change it for textbox to NO.
I couldn't do it , because textbox is in subset of Groupbox.
Could you please guide me , how I can change it ?
Thanks.
|
|
|
|
|
Your question is not exactly clear, but I am taking it to mean that you have created a Control which inherits from GroupBox and this control contains a TextBox .
You can set the property in design view of your control but if you want to be able to do it once your control is placed on a Form (for example), you need to surface any properties of child controls in your control. Something like this:
[Category("Appearance")]
public Color CountryTextBoxBackColor
{
get
{
return this.countryTextBox.BackColor;
}
set
{
this.countryTextBox.BackColor = value;
}
}
I have used BackColor in this example because I couldn't remember the Type for RightToLeft but the principle is exactly the same. You will need to do the same for any properties of child controls that you need to be available at design time.
The code above should be part of the code for your control, in case you have not worked that out.
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.”
|
|
|
|
|
Hi,
I will give an exmaple. We have a list box and a text box. On click of a button the text box will get enabled and the text enterd in the text box will be displayed in the label on another button click. This data is not saved. Now when i close the application and run again the labels are empty. How to store the entered data and display it in label.
Thanks,
Santa
|
|
|
|
|
There are quite a few ways you can do this. What have you learnt on your course so far, that might be applicable? What have you tried?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Please tell me how to do it. The example which i have given above in the thread is what i have tried. But when we close the applicationa dn run it again i do not get the same names in the labels.
|
|
|
|
|
I'm not going to give you the solution to your homework!
You will have to think about it a bit, but I'll give you a few clues:
System.IO.File is one way.
System.Configuration is another.
There are (probably) a dozen or so ways this could be done. Have a look at Google, and try something. When you have a specific problem (other than "I can't do my homework") post some details of where you are stuck.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Information displayed in Windows (forms) is placed there by the application that owns the forms. Information entered by the user must be saved somewhere by the application if it is needed again. You must decide how and where to save it so that it is available next time you run the appication.
|
|
|
|
|
I am working on Web application. And i do not want to store the data in DB and bring it back from ther. IS there any other possible ways to do it.
TIA.
Thanks,
Santa.
|
|
|
|
|