|
This particular problem may not be handleable (is that a word?).
As you know all USB devices should be remved 'safely'. When a device removal is done properly a DBT_DEVICEQUERYREMOVE[^] message is broadcast which you can catch and close the port first.
If a user decides to just unplug a USB device - they shouldn't expect programs that use them not to explode!
This article[^] may be of interest.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
DaveyM69 wrote: they shouldn't expect programs that use them not to explode
I disagree. Should a program explode when a disk stops working, when a network goes down, when the serial cable gets unplugged?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
DaveyM69 wrote: If a user decides to just unplug a USB device - they shouldn't expect programs that use them not to explode!
I agree with Luc. If someone yanks the cable then the program should always try to gracefully handle the exception.
|
|
|
|
|
I did watch for WM_DEVICEREMOVE and at the point in time where I handle DBT_DEVICEREMOVECOMPLETE, SerialPort.IsOpen already == false, and closing it doesn't make a difference.
I checked, there is no DBT_DEVICEQUERYREMOVE or DBT_DEVICEREMOVEPENDING event, just DBT_DEVICEREMOVECOMPLETE when the port is already closed...
|
|
|
|
|
Just in case you are not watching your thread, DaveyM69 has made a reply, but attached it to my first answer by mistake.
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.”
|
|
|
|
|
Oops!
Thanks Henry
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Okay, I've been trying to figure this out for the last 24 hours to no avail.
I'm writing a datebook style program just as a personal test while I learn, and I've run into a snag. The idea is that the user enters a date and event, then presses a 'save event' button which is supposed to both write the event to the event list window, and append it to a file called datebook.txt. My problem is that each time the 'save event' button is pressed, it rewrites the entire datebook.txt file. How do I fix this? I'm including the code below, minus irrelevant things (such as empty textChanged sections, etc).
Thanks.
namespace WpfApplication2
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DateTime current = DateTime.Now;
Entrance();
}
private void Entrance()
{
selectedDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
}
private void displayEvents_Click(object sender, RoutedEventArgs e)
{
try
{
DateTime selected = DateTime.Parse(enteredDate.Text);
selectedDate.Text = selected.ToString("MM/dd/yyyy");
}
catch (FormatException)
{
MessageBox.Show("The date you enterd was invalid.\n\nPlease reenter the date in
the format MM/dd/yyyy.");
}
}
private void saveNewEvent_Click(object sender, RoutedEventArgs e)
{
try
{
WriteToFile();
WriteToEventList();
}
catch (FormatException)
{
MessageBox.Show("The date you enterd was invalid.\n\nPlease reenter the date in the format MM/dd/yyyy.");
}
}
private void WriteToFile()
{
DateTime newEntry = DateTime.Parse(newDate.Text);
string addEvent = "";
addEvent = newEvent.Text;
using (TextWriter writeEvent = new StreamWriter("datebook.txt"))
{
writeEvent.WriteLine(newEntry.ToString());
writeEvent.WriteLine(addEvent);
}
}
private void WriteToEventList()
{
DateTime newEntry = DateTime.Parse(newDate.Text);
string addEvent = "";
addEvent = newEvent.Text;
eventsList.AppendText(String.Format("{0}\t{1}\n", newEntry, addEvent));
}
}
}
|
|
|
|
|
Try to use the method Flush() - streamWriter's name.Flush();
|
|
|
|
|
It figures, I happened to be looking for something else and found at least one solution that works. Switching the entire TextWriter writevent line for StreamWriter writeEvent = File.AppendText("datebook.txt") . Now i just need to figure out how to format it with a tab between the date and event instead of a line return...
Previous reply:I appreciate the quick response, but I don't fully understand...
Having no idea what the Flush() method does exactly, I googled it, and from what I've found this is merely an alternate way of closing the stream to ensure the data is purged. How would that help in this case, as the new event is being written, it's just being written over the previous event, so only the most recent saved event is in the text file when I open it.
Thanks again
modified on Wednesday, March 25, 2009 4:30 PM
|
|
|
|
|
Perhaps changing your WriteToFile method to the following will help:
private void WriteToFile()
{
DateTime newEntry = DateTime.Parse(newDate.Text);
string addEvent = "";
addEvent = newEvent.Text;
using (TextWriter writeEvent = System.IO.File.AppendText("datebook.txt"))
{
writeEvent.WriteLine(newEntry.ToString());
writeEvent.WriteLine(addEvent);
}
}
This should append text to the file. I'm not sure how it will handle the file not being present, so you may want to test for that.
|
|
|
|
|
using (TextWriter writeEvent = new StreamWriter("datebook.txt", true))
The second argument in the constructor tells it whether to overwrite (false) or append (true).
|
|
|
|
|
Hello...
I have a question. Is C#.NET able to be used for application for data acquisition with sample rate 10ms ?
Are there probably any problems with GarbageCollector ?
If I read the data each 10ms, the garbage collector probably doesn't have time to run.
Must I use C or C++ for that ?
Thanks...
|
|
|
|
|
stancrm wrote: If I read the data each 10ms,
You're probably not "just" going to read it, obviously, and I guess you'd like to store it. Probably in memory, until you got enough to write to..
Isn't there some kind of device-driver for the gizmo that you want to read from?
BTW, Windows' does not guarantee a minimal amount of CPU-time and it might decide to put you on hold in favor of the other processes.
I are troll
|
|
|
|
|
When it comes to time critical stuff, Windows is pretty rubbish. Low(er) level C stuff talking directly to the hardware is pretty much the only way to go. Once that's done, it's possible to create a C# wrapper around the unmanaged stuff.
Disclaimer
This is just from my experience of messing with ASIO (audio stuff), and may not be accurate for your situation!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
DaveyM69 wrote: When it comes to time critical stuff, Windows is pretty rubbish.
With the price of hardware one might decide on using a complete computer with QNX[^] as a peripheral device to the Windows host
I are troll
|
|
|
|
|
Cool... very cool
Link saved for further investigation!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Hi,
Windows is not a real-time system, it can't and doesn't guarantee a maximum latency time.
If you need to get something done every 10 msec no matter what, then you need something else.
OTOH if there is sufficient buffering, and all you need is accepting the data, but not immediately act on it, you might well be able to solve it with a Windows app. A simple example is data coming in over a serial line, say 1 character every 10 msec; if your buffer is 10KB it can hold 100 seconds' worth of data, so the app should be capable of processing that under all but the most extreme load conditions.
BTW: if the sample timing is important, you better have an external means as a sample clock; Windows sucks at creating small and precise time intervals.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I launch application B from application A. When i close application A i need to close application B.
I RDC to a remote server and launch application A and launch B from A. When i close A it looks for B and kills it. However it goes and kills application B in another RDC session.
How do i differentiate the process launched in the session i logged into?
Any help would be greatly appreciated.
|
|
|
|
|
Well, there are quite a few ways to launch a process... In this case, why not start it using the System.Diagnostics.Process class, and just keep a reference to that object?
private Process appB;
public void LaunchAppB()
{
appB = new Process();
appB.StartInfo.FileName = "...";
appB.Start();
}
public void KillAppB()
{
if (!appB.CloseMainWindow()) appB.Kill();
}
(Note: You generally want to try CloseMainWindow first, which will try to close the app in the usual manner, like clicking the X. If that doesn't work, you kill the process. Depending on exactly what you're doing here, you might need to make it more sophisticated, perhaps giving it a few seconds to close properly and killing after that, or something of the sort)
|
|
|
|
|
The context in which i close the application i cannot maintain the instance. Thanks for the response.
I found a solution. Check below:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr FindWindow(string sClassName, string sWindowTitle);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
uint processId;
IntPtr hProgram = WebApp.FindWindow(null, ".....");
GetWindowThreadProcessId(hProgram , out processId);
System.Diagnostics.Process.GetProcessById((int)processId).Kill();
|
|
|
|
|
I might suggest there is a problem with GetWindowThreadProcessId, I do UI automation testing, i was looking a using GetWindowThreadProcessId a few weeks ago. Bottom line from what i found if you are on Win95, or NT 3.51, GetWindowThreadProcessId should work fine, on Vista I thought i remember seeing warning about it not working correctly.
Alternatively, i might suggest 1.) Make sure you app is modal, that only one instance of it can be running. 2.) Get the process by GetProcessesByName() for your process, and then issue a kill on the process you find, well if it is still running.
HTH,
dan d;)
|
|
|
|
|
hi there, i need some kind of references to, "how i can find how many bits the computer sent and recieve over the lan?"
i need it in my C# application, is there any Library or any link that i can study ?
|
|
|
|
|
|
Can somebody tells me how long the connection to the database stays open if the connetion is not closed in the function.
If connetion stays open, meanwhile can we do the database restore or backup. Will that cause a problem.
Thanks
|
|
|
|
|
i am not sure but as the expierience says open connections will be closed automatically after sometime that is being idle,
for backing up and restore u should close all open connections(u can simply drop the database) ,otherwise u'll recieve error
|
|
|
|