|
You could do it using events or delegates or custom WM messages
|
|
|
|
|
Can someone please be more specific? Im new to programming.
Y*Live Long And Prosper*Y
|
|
|
|
|
Working Example:
using System;
using System.Windows.Forms;
public partial class FormButton : Form
{
public FormButton()
{
InitializeComponent();
}
private void FormButton_Load(object sender, EventArgs e)
{
FormCheckBox formCheckBox = new FormCheckBox();
formCheckBox.CheckStateChanged += formCheckBox_CheckStateChanged;
formCheckBox.Show();
}
void formCheckBox_CheckStateChanged(object sender, CheckStateEventArgs e)
{
button1.Enabled = e.IsChecked;
}
}
using System;
using System.Windows.Forms;
public partial class FormCheckBox : Form
{
public event EventHandler<CheckStateEventArgs> CheckStateChanged;
public FormCheckBox()
{
InitializeComponent();
}
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
OnCheckStateChanged(new CheckStateEventArgs(checkBox1.Checked));
}
protected virtual void OnCheckStateChanged(CheckStateEventArgs e)
{
EventHandler<CheckStateEventArgs> eh = CheckStateChanged;
if (eh != null)
eh(this, e);
}
}
public class CheckStateEventArgs : EventArgs
{
public CheckStateEventArgs(bool isChecked)
{
IsChecked = isChecked;
}
public bool IsChecked
{
get;
private set;
}
}
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) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I've already read that DaveyM69 has explained to you how to use events/delegates for solving your problem.But sometimes you need to notify other instances of your app for something important.In this case events and delegates are not useful,because you can't get valid pointers of the forms belonging to another instance of the app.In this case you have to use custom WM messages.To register custom WM message call RegisterWindowMessage native function.
And to send message call SendMessage native function passing HWND_BROADCAST value for hWnd parameter.But this function doesn't seem to work with child windows,but it works with hidden top windows for example.
But be careful with that option-it could damage OS performance.
|
|
|
|
|
I'm not sure how much code you need to see to handle this problem.
I am trying to add the ability for any logged-on client to send a message to all other logged-on clients.
My server code looks like this:
public OedipusServer
{
bool ensureSecurity = true;
RemotingConfiguration.Configure("oedipus.exe.config", ensureSecurity);
UserList insUserList = UserList.Instance;
ObjRef refUserList = RemotingServices.Marshal(insUserList, "UserList");
...
ServerClass insServerClass = ServerClass.Instance;
ObjRef refAServerClass = RemotingServices.Marshal(insServerClass, "Messaging");
}
public class ServerClass : AbstractServer
{
private static ServerClass mInstance = null;
public static ServerClass Instance
{
get
{
if (mInstance == null)
mInstance = new ServerClass();
return mInstance;
}
}
public override Object InitializeLifetimeService()
{
return null;
}
public override void myStatusHasChanged(int userID, int newStatus)
{
FireNewBroadcastedMessageEvent(userID, newStatus);
}
public event StatusChangedEventHandler myStatusChangedHandler;
public override event StatusChangedEventHandler myStatusChangedEvent
{
add
{
myStatusChangedHandler = value;
}
remove
{
}
}
protected void FireNewBroadcastedMessageEvent(int userID, int newStatus)
{
myStatusChangedHandler(userID, newStatus);
}
}
I have another project, Messaging, which looks like this:
public delegate void StatusChangedEventHandler(int userID, int newStatus);
public abstract class AbstractServer : MarshalByRefObject
{
public abstract void myStatusHasChanged(int userID, int newStatus);
public abstract event StatusChangedEventHandler myStatusChangedEvent;
}
public abstract class AbstractBroadcastedMessageEventSink : MarshalByRefObject
{
public void callbackStatusChanged(int userID, int newStatus)
{
internalCallbackStatusChanged(userID, newStatus);
}
protected abstract void internalCallbackStatusChanged(int userID, int newStatus);
}
On the client side I have:
class EventSink : AbstractBroadcastedMessageEventSink
{
protected override void internalCallbackStatusChanged(int userID, int newStatus)
{
}
}
private AbstractServer rmMessaging;
public Office()
{
rmMessaging = (AbstractServer)Activator.GetObject
(typeof(AbstractServer),
System.Configuration.ConfigurationManager.AppSettings
["AbstractServerUrl"]);
EventSink sinkStatusChanged = new EventSink();
rmMessaging.myStatusChangedEvent +=
new StatusChangedEventHandler(sinkStatusChanged.callbackStatusChanged);
}
private void button1_Click(object sender, EventArgs e)
{
rmMessaging.myStatusHasChanged(23, 14);
}
It's subscribing to the event which raises the runtime error "DelegateSerializationHolder is not permitted to be deserialized at this security level".
|
|
|
|
|
It seems your remoting configuration is not correct.This stuff might help you to solve your the problem.
|
|
|
|
|
Thanks. I found another article which discussed the problem and offered the solution of using a wrapper class. It still didn't work until I realised that I couldn't use tcp.
Adding a separate channel using http/Soap/Singleton (as your article states) solved the problem.
So I have a tcp channel for my data-access and an http channel for braodcasting messages.
|
|
|
|
|
At the risk of sparking of hilarious ridicule, can I get a ListView Control which mirrors the content of another?
I have a listview which is populated and another 'blank' (unitilialised) listview which I need to 'mirror' the first.
Any pointers welcomed.
|
|
|
|
|
Well what ever you do with the first one, just do it to the second one at the same time
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Hello. This is my problem:
try
{
Application.Run(new Form1());
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
In Debug-Mode (F5), if exception is thrown, then it goes to catch block.
In Release-Mode (Ctlr-F5), if exception is thrown, then it shows "unexpected exception" from windows. Catch block is not called.
How can I catch unexpected exception in Release-Mode ?
Thanks.
|
|
|
|
|
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
|
|
|
|
|