|
There's a special event that gets triggered whenever there's an unhandled exception. Your application could subscribe to the Application.ThreadException [^]-event.
Something like this (adapted from the MSDN-example, not tested);
{
Application.ThreadException += new ThreadExceptionEventHandler(MyErrorHandler);
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.Run(new Form1());
}
private static void MyErrorHandler(object sender, ThreadExceptionEventArgs t)
{
ShowMessage(t.ToString());
}
I are Troll
|
|
|
|
|
|
Hey!
How can i close Firefox?
|
|
|
|
|
Enumerate all the processes using the GetProcesses method, and if you find FF, terminate it. Take a look at the Process [^]-class
I are Troll
|
|
|
|
|
May I politely ask why do you want to close FF from your program? What kind of program are you writing?
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
i want to make a Firefox Pw Viewer.
And for that i will close the FF before i open the Firefox Pw Viewer.
|
|
|
|
|
Firefox keeps a lock on the bookmarks database, and needs to be closed for some bookmark readers to work. I'm not certain, but I think that something similar would be in effect for the passwords database
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
I look for a code!
And then i will close the FF prozess
|
|
|
|
|
PC17 wrote: I look for a code!
Sorry for posting an answer instead of real code
I are Troll
|
|
|
|
|
What do you say to this Code?
//using System.Diagnostics;
Process[] pp = Process.GetProcessesByName("ttermpro");
foreach (Process p in pp)
{
p.CloseMainWindow();// Normales ende
//p.Kill(); sofort beenden
}
instead of "ttermpro" "firefox.exe"
|
|
|
|
|
Sieht gut aus!
Well done
|
|
|
|
|
Unfortunately, the solution you've got won't work in all cases; notably in applications that are running as multiple top level single instance applications. If you open up multiple instances of firefox and look in Task Manager, you'll see only one instance of FireFox there (i.e. one process). This is because FireFox is actually a single instance application with multiple top level windows (these are intended to save memory).
In order to kill all the instances, you'll need to resort to a bit of p/invoke trickery where you retrieve the window title of all the running windows, and then look for those windows that contain the string "Mozilla Firefox". When you find them, you close them via the API. The following code sample demonstrates this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace KillFirefox
{
class Program
{
static void Main(string[] args)
{
KillWindowProcesses.KillProcess("Mozilla Firefox");
}
public class KillWindowProcesses
{
const int MAXTITLE = 255;
const int WM_CLOSE = 0x0010;
private static string name = string.Empty;
private delegate bool EnumDelegate(IntPtr hWnd, int lParam);
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool _EnumDesktopWindows(
IntPtr hDesktop,
EnumDelegate lpEnumCallbackFunction,
IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "GetWindowText",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
private static extern int _GetWindowText(
IntPtr hWnd,
StringBuilder lpWindowText,
int nMaxCount);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(
IntPtr hWnd,
uint Msg, IntPtr
wParam, IntPtr
lParam);
public static void KillProcess(string processName)
{
name = processName;
GetDesktopWindowsCaptions();
}
private static void PostMessageSafe(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
{
bool returnValue = PostMessage(hWnd, msg, wParam, lParam);
if (!returnValue)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
}
private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
{
string title = GetWindowText(hWnd);
if (!string.IsNullOrEmpty(title) && title.Contains(name))
{
PostMessageSafe(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}
return true;
}
private static string GetWindowText(IntPtr hWnd)
{
StringBuilder title = new StringBuilder(MAXTITLE);
int titleLength = _GetWindowText(hWnd, title, title.Capacity + 1);
title.Length = titleLength;
return title.ToString();
}
private static void GetDesktopWindowsCaptions()
{
EnumDelegate enumfunc = new EnumDelegate(EnumWindowsProc);
IntPtr hDesktop = IntPtr.Zero;
bool success = _EnumDesktopWindows(hDesktop, enumfunc, IntPtr.Zero);
if (!success)
{
int errorCode = Marshal.GetLastWin32Error();
string errorMessage = String.Format(
"EnumDesktopWindows failed with code {0}.", errorCode);
throw new Exception(errorMessage);
}
}
}
}
}
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
That seems a little odd. What if I've got a window of my own which has got the title "Mozilla Firefox Bookmark Importer"? It might be more effective to alter EnumWindowsProc so that it gets the process id of the given handle, and closes the window if the process id is correct.
That would change the function of the class, so that instead of closing windows based on a title, (which might not be unique) it closes windows based on a process id (which is guaranteed to be unique)
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Computafreak wrote: That seems a little odd. What if I've got a window of my own which has got the title "Mozilla Firefox Bookmark Importer"? It might be more effective to alter EnumWindowsProc so that it gets the process id of the given handle, and closes the window if the process id is correct.
It's a good point, but this still comes back to you not knowing what the process is because FireFox runs multiple instances under one process id (which is why Process.GetProcess(id).Kill() doesn't work).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I've just hacked together this sample application, and GetProcessById(i).Kill() worked. Both instances that I had open were closed, and one process was detected:
int i = 0;
foreach (Process p in Process.GetProcessesByName("firefox"))
Console.WriteLine(i = p.Id);
if (i != 0)
Process.GetProcessById(i).Kill();
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Good sample - and it seems to have been a bug in the version I put together at first, using CloseMainWindow() at that point rather than Kill. Rather stupid of me.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks for this. It works fantastic!
|
|
|
|
|
Can you not do what most apps do - detect if the FF process is running, and display a "Please close Firefox, and press OK to continue" dialog. Then you've just got to detect if its running, and not worry about trying to make it close. (Have you considered that killing the process might corrupt the very database you're trying to read?)
|
|
|
|
|
okay
Thank you at everbody who helped me!!!
|
|
|
|
|
Hi
I would like to know how do I incorporate AForge.NET to my C# program for motion detection. I have installed AForge.NET but I get an error " The type or namespace name 'AForge' could not be found (are you missing a using directive or an assembly reference?)"
Thank you in advance.
Vallie
|
|
|
|
|
This may seem like a stupid question, but have you actually included a reference to AForge in your project? Just because it's been installed on your machine doesn't mean that your project will automatically pick it up - you still need to add a reference to it.
Right click on your project and click "Add reference..." to bring up the references dialog. At this point, if AForge has been installed in the GAC, you can add a reference from the .NET assemblies tab. If it hasn't, you'll need to browse to the folders containing the DLLs and add them from there (making sure you pick up any items that AForge is dependent on).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
i'm try to execute a function on button click and hold event , have anyone has got the experience on doing this,
i need to execute the function while the user is pressing and holding the button.
|
|
|
|
|
Try using both the mouse down and mouse up events. The first starts the function, the second stops the function (so to speak)
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I'm not certain what you are trying to achieve, so is this what you want:
1) The user presses the mouse down, nothing happens immediately.
2) When the user has held the mouse button down for some time, something happens
3) The user then releases the mouse button.
If so, then the way to do it is:
In the MouseDown event, set a bool to indicate the mouse is down, and start a timer.
If you get a MouseUp event, and the bool is true, kill the timer, clear the bool.
If you get a Timer.Tick event, and the bool is true, kill the timer, and perform the action.
Note that this may not work with some controls, as they handle mouse events differently (Button, CheckBox, ComboBox, and RadioButton) - see here[^]for sequence details.
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
|
|
|
|
|
Im new to the entity framwork approach and im planning on using it for a small application here at the office.
However , i can see EF to become a very good approach in some other applications we are planning on writing in the near future.
A bought a book called "Programming entity framework" from Oreilly , but it has so many errors in it so i cant really do the examples and get a clear understanding on how to accomplish the following.
I need to write a class library to handle all the database calls and then return the results. I just want to send the LINQ statement to the class , and then get the result back.
I have tried to find examples on the net , but i dont find anything that i could look at as an example.
Can anyone please send me links or give me an example on how to handle this for Inserts , updates and deletes.
|
|
|
|
|