473,396 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

WinForms & Remoting

Hello,

Basically, I want my application to run only one instance at a time and pass
command line arguments to a running instance. I have all of this working, I
used the IPC Remoting channel and my program handles the command line
arguments well. However, after my program has been running for a little while
(say 5-10 minutes) the command line arguments no longer are passed, and
instead it causes a .NET Remoting exception claiming to have come across a
null reference.

So in my Program.cs file, I have something like this.

if (alreadyrunning)
{
IpcChannel ipcCh = new IpcChannel("MyProgram_" + username);
ChannelServices.RegisterChannel(ipcCh, false);

RemotingConfiguration.RegisterWellKnownServiceType (typeof(RemotingService),
"Remoting", WellKnownObjectMode.Singleton);
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.RunFirstInstance(arguments);
}
else
{
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.HandleCommandLineArgs(arguments);
}
Again, it all works for a short time, but stops working claiming a null
reference on the HandleCommandLineArgs method after a while. I'm guessing
that even though I specifify a Singleton instance that it somehow gets
garbage collected or a new RemotingService class generated (even though my
program is still running).

Thanks in advance,
mitch

p.s. Sorry if this is a double post, I think my last one went into a thread,
instead of starting it's own topic.
May 2 '06 #1
5 3852
As a side note, here is a simplified verson of my RemotingService.cs code:

namespace MyProject
{
public class RemotingService : MarshalByRefObject,
ISharedAssemblyInterface
{
MainForm mainform = null;
bool firstrun = true;
//--------------------------------------------------------------------------------------------
public RemotingService()
{
}
//--------------------------------------------------------------------------------------------
public void RunFirstInstance(string[] arguments)
{
mainform = new MainForm();
mainform.Show();
HandleCommandLineArgs(arguments);
Application.Run();
}
//--------------------------------------------------------------------------------------------
public void HandleCommandLineArgs(string[] arguments)
{
try
{
MethodInvoker myCallback = new MethodInvoker(
delegate()
{
Arguments args = new Arguments(arguments);
// do something with arguments
MainForm.Show();
});
MainForm.Invoke(myCallback);
}
catch (Exception e)
{
MessageBox.Show("ERROR " + e.ToString());
}
}
//--------------------------------------------------------------------------------------------
}
//--------------------------------------------------------------------------------------------
public interface ISharedAssemblyInterface
{
void HandleCommandLineArgs(string[] arguments);
void RunFirstInstance(string[] arguments);
}
//--------------------------------------------------------------------------------------------
}
May 2 '06 #2
mitch,

Are you using .NET 2.0? If so, I would recommend using the
WindowsApplicationBase class, as it does all of this (feeds command lines to
an already running instance of your application, etc, etc). There is more
information on Google groups:

http://groups.google.com/group/micro...2d86fcc6e071fe

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"mitch" <mi***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Hello,

Basically, I want my application to run only one instance at a time and
pass
command line arguments to a running instance. I have all of this working,
I
used the IPC Remoting channel and my program handles the command line
arguments well. However, after my program has been running for a little
while
(say 5-10 minutes) the command line arguments no longer are passed, and
instead it causes a .NET Remoting exception claiming to have come across a
null reference.

So in my Program.cs file, I have something like this.

if (alreadyrunning)
{
IpcChannel ipcCh = new IpcChannel("MyProgram_" + username);
ChannelServices.RegisterChannel(ipcCh, false);

RemotingConfiguration.RegisterWellKnownServiceType (typeof(RemotingService),
"Remoting", WellKnownObjectMode.Singleton);
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.RunFirstInstance(arguments);
}
else
{
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.HandleCommandLineArgs(arguments);
}
Again, it all works for a short time, but stops working claiming a null
reference on the HandleCommandLineArgs method after a while. I'm guessing
that even though I specifify a Singleton instance that it somehow gets
garbage collected or a new RemotingService class generated (even though my
program is still running).

Thanks in advance,
mitch

p.s. Sorry if this is a double post, I think my last one went into a
thread,
instead of starting it's own topic.

May 2 '06 #3
Nicholas,

Yes I am using .NET 2.0, I will look into that class, it seems a little
awkward to tap into the VisualBasic namespace when I'm coding in C#, doesn't
it? Is it possible there is a bug in remoting? I would jump on the
WindowsApplicationBase class, but my code *does* work for a while....until it
times out and stops working...

Thank you,
mitch
May 2 '06 #4
The GC has determined that ipcCh is no longer required and can be
collected - yeah a bit overzelous.

Change;
obj.RunFirstInstance(arguments);
to
obj.RunFirstInstance(arguments);
try { string keeprunning = ipcCh .ToString(); }
catch(Exception) { }

This will 'fool' the GC into keeping the remoting service active.
Keep this in mind for any planned long life objects.

- Colin

"mitch" <mi***@discussions.microsoft.com> wrote in message
news:35**********************************@microsof t.com...
Hello,

Basically, I want my application to run only one instance at a time and pass command line arguments to a running instance. I have all of this working, I used the IPC Remoting channel and my program handles the command line
arguments well. However, after my program has been running for a little while (say 5-10 minutes) the command line arguments no longer are passed, and
instead it causes a .NET Remoting exception claiming to have come across a
null reference.

So in my Program.cs file, I have something like this.

if (alreadyrunning)
{
IpcChannel ipcCh = new IpcChannel("MyProgram_" + username);
ChannelServices.RegisterChannel(ipcCh, false);

RemotingConfiguration.RegisterWellKnownServiceType (typeof(RemotingService), "Remoting", WellKnownObjectMode.Singleton);
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.RunFirstInstance(arguments);
}
else
{
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.HandleCommandLineArgs(arguments);
}
Again, it all works for a short time, but stops working claiming a null
reference on the HandleCommandLineArgs method after a while. I'm guessing
that even though I specifify a Singleton instance that it somehow gets
garbage collected or a new RemotingService class generated (even though my
program is still running).

Thanks in advance,
mitch

p.s. Sorry if this is a double post, I think my last one went into a thread, instead of starting it's own topic.

May 2 '06 #5

"mitch" wrote:
Hello,

Basically, I want my application to run only one instance at a time and pass
command line arguments to a running instance. I have all of this working, I
used the IPC Remoting channel and my program handles the command line
arguments well. However, after my program has been running for a little while
(say 5-10 minutes) the command line arguments no longer are passed, and
instead it causes a .NET Remoting exception claiming to have come across a
null reference.

So in my Program.cs file, I have something like this.

if (alreadyrunning)
{
IpcChannel ipcCh = new IpcChannel("MyProgram_" + username);
ChannelServices.RegisterChannel(ipcCh, false);

RemotingConfiguration.RegisterWellKnownServiceType (typeof(RemotingService),
"Remoting", WellKnownObjectMode.Singleton);
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.RunFirstInstance(arguments);
}
else
{
ISharedAssemblyInterface obj = (ISharedAssemblyInterface)
Activator.GetObject(typeof(ISharedAssemblyInterfac e),
"ipc://MyProgram_" + username + "/Remoting");
obj.HandleCommandLineArgs(arguments);
}
Again, it all works for a short time, but stops working claiming a null
reference on the HandleCommandLineArgs method after a while. I'm guessing
that even though I specifify a Singleton instance that it somehow gets
garbage collected or a new RemotingService class generated (even though my
program is still running).

Thanks in advance,
mitch

p.s. Sorry if this is a double post, I think my last one went into a thread,
instead of starting it's own topic.


I believe the problem is that you need to override the lifetime service of
your remoted Singleton, otherwise it gets collected by the garbage collector.
If you override InitializeLifetimeService in your remoted object, and return
null, this should ensure the object lives forever.

Remoting uses lease base lifetime services to control the lifetime of
remoted objects. As I have not used remoting for nearly a year, I cannot give
you a very good explanation, however if you Google Ingo Rammer you should
find some useful info.

May 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: David Noble | last post by:
We've been developing a web site using 3-tier architecture for 18 months now. There is a common layer that defines the classes - using XML schemas. The data layer acts as a wrapper to 3 databases...
1
by: Helen W | last post by:
The Knowledge Base article below explains how to talk to a running instance of an Office program: http://support.microsoft.com/default.aspx?scid=kb;EN-US;316126 Is there any way I can talk...
2
by: Nick | last post by:
Is there a way that if I host my remoted object in IIS (not having to mess with encryption & authentication via a custom sink) that the server can raise events and the clients can detect them? If...
20
by: Martin Rosén-Lidholm | last post by:
Although an impossible question to answer, I fell urged to raise it anyhow. Given a fairly complex ERP application scenario, what's your estimation for the X-ratio dev. time for...
11
by: kiln | last post by:
I am starting a project that may be suitable for vb.net, using windows forms. I want a rich client, thus win forms vs web forms. Most users will access the app data over a LAN, but some will be...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
1
by: Peter931 | last post by:
I have a winforms Application that should share some data via WEB-Services. Remoting is not possible for other reasons. How can this be done ? When the WEB-Service is hosted in IIS on the...
4
by: sjoshi | last post by:
Hello All I'm trying this to filter group users bu tI keep getting an unspecified error when invoking FindOne method. Any help is greatly appreciated. public static DirectoryEntry...
10
by: Jules Winfield | last post by:
Guys, I've been designing applications for the financial services industry using MSFT technologies for many years (Win32 API, MFC, and now .NET WinForms). All applications are Internet-based,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.