|
|
I've inherited a wrapper for a gps interpretter. The guy who wrote it is dispose happy, I'm guess from the old days with .net. I once had to go back and change all the close calls in managed database connections to .dispose(). This was code we were maintaining, but not code that was touched in my update. So I had to go through the whole project and change them to prevent memory leaks that wasn't there?
Anyway, I found the msdn page, after, that said that wasn't necessary. That still wasn't proof enough?
Ok, so this wrapper is a static class. It has a member NmeaInterpreter, and a method dispose. Dispose says :
if( GpsInterpreter.Device != null)
{
GpsInterpreter.Device.Dispose();
}
GpsInterpreter.Dispose();
I don't even think that is necessary. Is it?
If it moves, compile it
|
|
|
|
|
loctrice wrote: I don't even think that is necessary. Is it?
Depends. Not disposing means that it'll be disposed of when the GC runs, which usually happens when the system is low on memory. That might also cause other apps to run into problems when trying to allocate memory.
There's another problem with not disposing objects, if they reference unmanaged resources; a stream or a SqlConnection needs be disposed of. Given a file-stream, the file can be "locked" until the reader is disposed of.
The advantage of disposing aggressively is performance. Still, I don't implement dispose unless I have to.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I will leave the dispose in there, just to be on the safe side. It's a mobile device that we are using, so resources are resources.
I always thought, and programmed, that an Sql connection and/or file stream can be .close() 'ed without issue. They don't need both close and dispose called, it's one or the other and close is "good enough"
If it moves, compile it
|
|
|
|
|
loctrice wrote: They don't need both close and dispose called, it's one or the other and close is "good enough"
Dispose would also close it. Closing it will not dispose it, but will return the connection to the pool. This will not work for all classes in the same way though.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
am created sdk for rfid card reader it reads the rfid value and display the output in that sdk screen i want to use the rfid values in my form to use the rfid value
|
|
|
|
|
And? What's your question?
|
|
|
|
|
You don't really know what an SDK is, do you?
|
|
|
|
|
I have a project that i have to create and write to a text file using the StreamWritter class. i create the file just fine and after playing with it a little got the text to enter into the file correctly but i cant add more text to the file after the inital line or it just truncates whats already inside of it, i want the text inside to add each time the button is clicked even after the program closes and i reopen it. here is my code so far
using System.IO;
namespace SWardVideoSalesApplication
{
public partial class MainSalesForm : Form
{
public MainSalesForm()
{
InitializeComponent();
}
#region Variables
StreamWriter testFile = File.CreateText("TestFile2.txt");
#endregion
private void summaryButton_Click(object sender, EventArgs e)
{
bool Flag = true;
testFile = File.
if (Flag)
{
testFile.WriteLine("This is a test {0}", todayDate.ToString());
testFile.Close();
}
else
{
MessageBox.Show("ERROR");
}
}
|
|
|
|
|
Please ignore the testFile = File. statement i was playing around with an idea and forgot to delete it before i posted my code
|
|
|
|
|
how about :-
using (StreamWriter testfile = new StreamWriter("TestFile2.txt", true))
{
testfile.WriteLine("This will be appended");
}
'g'
|
|
|
|
|
Hi,
The answer to your question is to set the stream writer to append mode.
Try this:
string _fileName = "TestFile2.txt";
private void summaryButton_Click(object sender, EventArgs e)
{
bool Flag = true;
if (Flag)
{
string content = string.Format("This is a test {0}", todayDate.ToString());
writeToFile(_fileName, content);
}
else
{
MessageBox.Show("ERROR");
}
}
private void writeToFile(string fileName, string content)
{
FileInfo fi = new FileInfo(fileName);
if (!fi.Exists)
{
MessageBox.Show("file doesn't exists!");
return;
}
else
{
using (StreamWriter sw = new StreamWriter(fileName, true, System.Text.Encoding.Default))
{
sw.Write(content);
}
}
}
Hope this helps.
Regards
Jegan
Think! Don't write a line of code unless you absolutely need to.
|
|
|
|
|
The reason the code is failing is because the following is happening:
- You create the TestFile2.txt the first MainSalesForm is instantiated. Subsequent instantitations of MainSalesForm will overwrite the file effectively blanking it. (try prepenging the DateTime.Now.Ticks to the filename to see when the file is actually being created/overwritten).
- On button click, you write to the file and close the stream so the file can no longer be written to. If the CreateFile is executed later (say the form is closed and reopened) it will overwrite what has been written.
I suggest the following code:
private void summaryButton_Click(object sender, EventArgs e)
{
WriteToFile(string.Format("This is a test {0}", todayDate.ToString());
}
private void WriteToFile(string fileName, string content)
{
FileInfo fi = new FileInfo(fileName);
if (!fi.Exists)
{
using(StreamWriter testFile = File.CreateText(fileName)
{
sw.Write(content);
}
}
else
{
using (StreamWriter sw = new StreamWriter(fileName, true))
{
sw.Write(content);
}
}
}
Once the button is pressed the file will be created (if it does not exist) or will be appeneded to with the string passed to the method. Note that the file is not locked between clicks (so you can open it for reading and writing). This may not be the desired behaviour, if not the code will be unsuitable. The code I have provided may not compile (I haven't tested it) and could be tidied a little but it should get you on track.
|
|
|
|
|
I did it like this once:
StreamWriter writer;
if (!File.Exists("filepath")
{
writer = new StreamWriter("");
}
else
{
writer = File.AppendText("filepath");
}
writer.WriteLine("Write stuff in here");
|
|
|
|
|
I am working on a thing that how to copy username from a text file already created in C drive and paste that username into the USERNAME text field of the website for login....
I will be very thankful for your help...
Thanks in advance
|
|
|
|
|
Are you trying to automatically fill and submit a login form?
If so, you can use a WebBrowser control to do this:
new WebBrowser().Navigate(
url,
"_blank",
Encoding.Default.GetBytes(
"username=" + username +
"&userpass=" + userpass),
"Content-Type: application/x-www-form-urlencoded");
where url , username and userpass are string variables containing the data. Obviously change the names of the fields to what the names are of the input fields of the original HTML form, and add other fields as required.
|
|
|
|
|
Great tip, I've been trying to figure out an easier way to do this than the one I worked out.
Use the best guess
|
|
|
|
|
Cheers Richard - though I must admit it was 'stolen' from somewhere on the interwebs some time ago, I just happened to have reused this code myself yesterday so had it right to hand I never made a note of where I got it so can't give proper credit...
|
|
|
|
|
I am facing this error
"Invalid URI: The format of the URI could not be determined."
webBrowser1.Navigate("www.facebook.com", "_blank", Encoding.Default.GetBytes("username=123@abc.com" + "&userpass=xyz"),
"Content-Type: application/x-www-form-urlencoded");
What am i doing wrong here?
|
|
|
|
|
First of all, you need the correct URL which is https://www.facebook.com/login.php
Then you need the correct names for the fields which are email and pass
I got all this information by looking at the HTML source for the Facebook login page.
So the code is:
string facebookUrl = "https://www.facebook.com/login.php";
string facebookEmail = "user@domain.com";
string facebookPassword = "userpassword";
new WebBrowser().Navigate(facebookUrl, "_blank", Encoding.Default.GetBytes(
"email=" + facebookEmail +
"&pass=" + facebookPassword), "Content-Type: application/x-www-form-urlencoded");
Change the value of facebookEmail and facebookPassword to your login details and it should work.
|
|
|
|
|
Did my FB code work for you?
Feedback on whether a suggestion worked or not is always helpful for others that may come across this thread in future.
|
|
|
|
|
Hi,
Please see this image:
http://www.saveyourteeth.com/images/tooth%20chart%202.gif[^]
I want to know how to do the following:
when the user clicks on the tooth it will change the tooth color to gray and it ill display a MessageBox showing the tooth number?
could you please guide...
Technology News @ www.JassimRahma.com
|
|
|
|
|
Create a polygon that represents each tooth. Do a test to see if the click point is inside the polygon. If true, colour the polygon.
|
|
|
|
|
Another method is to allow the user to point to and click at the centre of each tooth.
Save the co-ordinates in a List of Tooth class(you will need to define the Tooth class).
On clicking a dialogue box the application can then ask which tooth number has been selected.
Then when it comes to identifying teeth:
As teeth as fairly round(well more oval really) in shape you can detect if the user clicks with the orbit of the declared centre of each tooth and respond accordingly.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
I've asked maybe to broad of a question before. So I have created a very simple application. My Word document has 1 chart in it.
In a nutshell, where is what happens:
1. Word Document is opened.
2. Find the number of Charts in the document.
3. Loop through the inline shapes
4. Check to see if they are Charts.
So as I said, nothing fancy.
private void Form1_Load(object sender, EventArgs e)
{
Document oReportDoc = new Document();
Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
oReportDoc = oWord.Documents.Open(@"C:\ChartTest.docx", ReadOnly: false);
label2.Text = oReportDoc.InlineShapes.Count.ToString();
int j = 1;
foreach (Microsoft.Office.Interop.Word.InlineShape wshape in oReportDoc.InlineShapes)
{
try
{
if (wshape.OLEFormat.ProgID.Equals("Excel.Sheet.8"))
{
if (wshape.HasChart == Microsoft.Office.Core.MsoTriState.msoTrue)
{
wshape.OLEFormat.Open();
Microsoft.Office.Interop.Excel.Workbook xlWorkBook = wshape.OLEFormat.Object;
if (xlWorkBook != null)
{
xlWorkBook.ActiveChart.HasTitle = true;
xlWorkBook.ActiveChart.ChartTitle.Text = "test" + j.ToString() + "!";
j++;
}
}
}
}
catch (Exception ex)
{
richTextBox1.Text = ex.Message;
}
}
}
So it appears that, when debugging the code, that the OLEFormat property is Null, which I assume is causing the exception to fire off.
Can anyone tell me what is happening here and why the OLEFormat is null? And how to get around this issue?
Very frustrating.
Thanks,
Jeff
|
|
|
|