|
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.
|
|
|
|
|
Try Googling on linq to ef crud. I got loads of hits and the top few looked as if they concerned the type of functionality you are looking for.
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.”
|
|
|
|
|
Thanks. Looks like that was the approach im looking for.
|
|
|
|
|
Ok so im making a program thats made up of lots of custom user controls. Basically like plugins. Now it works great with all my "plugins" by dragging them onto the form, but what I want is that the user can add the dll's of the controls he wants and my program dynamically loads them in(and I will size and place them etc in the code).
Im sure this is possible as tons of apps do it. So what is the best way?
Shot
|
|
|
|
|
Is this a winform or an Web page
|
|
|
|
|
Sorry, its a winform. Would be useful if it would work when I ran it on linux with mono(just to be future proof).
|
|
|
|
|
You first need a method of loading an assembly from a path. You can use Assembly.LoadFrom for that. Then you can call GetTypes on the result from that; you'll get a collection of Types from that. Iterate through each of those, and check to see if it inherits from Control. If it does, call Activator.CreateInstance and cast it to a Control instance. Then you just have to add it to a form and do your sizing
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Sounds good, thanks alot
|
|
|
|
|
Cool got it working. Thanks alot!
By the way for anyone reading this heres the code I used excluding the obvious positioning and fluff:
<pre>
Assembly asm = Assembly.LoadFrom(dlldirectory);
Type typ = asm.GetType("My_Control.UserControl1");
UserControl uc = (UserControl)Activator.CreateInstance(typ);
this.Controls.Add(uc);
</pre>
Strive to be humble enough to take advice, and confident enough to do something about it.
|
|
|
|
|
Hi,
I have a method which is in another project (but in the same solution), I create a delegate to that method and then use delegate.BeginInvoke to Asynchronously call that method and the callback method is in the calling project. Now the problem is when the invoked method completes, I need to somehow signal the callback method if the invoked method has succedded True or False, and because the invoked method is in another project and the calling method is also in another project and this project has a reference to the invoked method project if I try to set some variable in the calling project from invoked method project I can't because i don't have a reference to this project and if I try to reference the project i get a circular dependency error.. which is resonable...
This is the code:
private delegate void edi2xml_delegate(object ar);
private edi2xml_delegate edi2xml_del;
public void convert_file(object async)
{
DemoApplication.edi_test edi2xml_converter = new DemoApplication.edi_test();
edi2xml_converter.file_path = pub.def.edi_file_directory + "\\" + file_list.file_name;
edi2xml_del = new edi2xml_delegate(edi2xml_converter.convert);
edi2xml_del.BeginInvoke(null, import_xml2db, (string)(pub.def.edi_file_directory + "\\" + file_list.file_name + ".xml"));
}
private void import_xml2db(IAsyncResult ar)
{
string full_path = (string)ar.AsyncState;
edi2xml_del.EndInvoke(ar);
}
public void edi2xml()
{
}
Any help or suggestions greatly appreciated.
|
|
|
|
|
Hi,
I suggest you have a look at BeginInvoke, EndInvoke and IAsyncResult. This trio normally manages to synchronize with and obtain return values from asynchronous operations.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|