473,326 Members | 2,061 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,326 software developers and data experts.

Single-instance app revisited

Some time ago I needed to write a single-instance app. That's an easy task
but I also needed duplicate instances to pass their command line parameters
to the first instance so that it can act upon them.

I ended up achieving this using Remoting to start my first app, and then
call into that Singleton remoted object from secondary instances. I thought
I was pretty clever until today when I tried to show an OpenFileDialog and
received an error telling me I have to use my app's main thread.

It's proving to be a real pain :-) Does anyone have a solution they have
tried?

Pete

Sep 25 '08 #1
10 1422
On Thu, 25 Sep 2008 11:58:02 -0700, Peter Morris
<mr*********@spamgmail.comwrote:
Some time ago I needed to write a single-instance app. That's an easy
task but I also needed duplicate instances to pass their command line
parameters to the first instance so that it can act upon them.

I ended up achieving this using Remoting to start my first app, and then
call into that Singleton remoted object from secondary instances. I
thought I was pretty clever until today when I tried to show an
OpenFileDialog and received an error telling me I have to use my app's
main thread. [...]
Really? That surprises me. Granted, I haven't tried it, and presumably
it'd be trivial for me to reproduce the behavior. But dialogs, having
their own message loops, usually work okay on any arbitrary thread.

That said, given that requirement, why doesn't the standard technique
apply? That is, just use Control.Invoke() or similar to execute the code
to show the dialog on the correct thread?

Pete
Sep 25 '08 #2
Peter Morris <mr*********@SPAMgmail.comwrote:
Some time ago I needed to write a single-instance app. That's an easy task
but I also needed duplicate instances to pass their command line parameters
to the first instance so that it can act upon them.

I ended up achieving this using Remoting to start my first app, and then
call into that Singleton remoted object from secondary instances. I thought
I was pretty clever until today when I tried to show an OpenFileDialog and
received an error telling me I have to use my app's main thread.

It's proving to be a real pain :-) Does anyone have a solution they have
tried?
Have you tried using the Microsoft.VisualBasic.ApplicationServices
classes?

Read http://msdn.microsoft.com/en-us/libr...wx(VS.80).aspx and
see if it helps you. Personally I think it would be nice to have this
sort of functionality in System.*...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 25 '08 #3
Yes. Unfortunately it threw a "NotImplemented" or "NotSupported" exception
(don't recall which) on a couple of machines at work, so I abandoned it.
Sep 25 '08 #4
Peter Morris <mr*********@SPAMgmail.comwrote:
Yes. Unfortunately it threw a "NotImplemented" or "NotSupported" exception
(don't recall which) on a couple of machines at work, so I abandoned it.
I suspect it's worth digging into a bit further, as VB.NET uses it
pretty easily. It may be worth coming up with a quick VB.NET app then
decompiling it to see what it's doing.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 25 '08 #5
On Thu, 25 Sep 2008 12:09:18 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
>I ended up achieving this using Remoting to start my first app, and
then call into that Singleton remoted object from secondary instances.
I thought I was pretty clever until today when I tried to show an
OpenFileDialog and received an error telling me I have to use my app's
main thread. [...]

Really? That surprises me. Granted, I haven't tried it, and presumably
it'd be trivial for me to reproduce the behavior. But dialogs, having
their own message loops, usually work okay on any arbitrary thread.
By the way, I took another look at this, and I can confirm that it's not
actually a problem for the OpenFileDialog to be shown on other than the
main GUI thread per se.

However, the OpenFileDialog does require that the thread be initialized as
a COM single-threaded-apartment. Since you're using remoting, you
probably don't have control over the nature of the thread itself, and so
it might be difficult for you to ensure that.

Given that, I still think that my follow-up question still applies:
That said, given that requirement, why doesn't the standard technique
apply? That is, just use Control.Invoke() or similar to execute the
code to show the dialog on the correct thread?
Pete
Sep 25 '08 #6
Hi Pete

Using Invoke doesn't work either.....
Thanks for your time!
Pete

private void ButtonSelectSpeechAudioFileName_Click(object sender,
EventArgs e)
{
Invoke(new System.Threading.ThreadStart(
delegate()
{
OpenFileDialog ofd = GetOpenWavDialog(Composition.SpeechAudioFile);
if (ofd.ShowDialog() == DialogResult.OK)
Composition.SpeechAudioFile = ofd.FileName;
}));
}

System.Threading.ThreadStateException was unhandled by user code
Message="Current thread must be set to single thread apartment (STA) mode
before OLE calls can be made. Ensure that your Main function has
STAThreadAttribute marked on it. This exception is only raised if a debugger
is attached to the process."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.MarshaledInvoke(Contr ol caller,
Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[]
args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at
AlterEgo.WizardStepControls.AudioAndScriptStepCont rol.ButtonSelectSpeechAudioFileName_Click(Object
sender, EventArgs e) in
C:\Data\CustomerProjects\Inteevo\Projects\AlterEgo \AlterEgo\WizardStepControls\AudioAndScriptStepCon trol.cs:line
77
at System.Windows.Forms.Control.OnClick(EventArgs e)
at DevExpress.XtraEditors.BaseButton.OnClick(EventArg s e)
at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseE ventArgs e)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at DevExpress.Utils.Controls.ControlBase.WndProc(Mess age& m)
at
System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message&
m)
at System.Windows.Forms.NativeWindow.DebuggableCallba ck(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchM essageW(MSG&
msg)
at
System.Windows.Forms.Application.ComponentManager. System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32
reason, ApplicationContext context)
at
System.Windows.Forms.Application.ThreadContext.Run MessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Inteevo.SingleInstanceApp.SingleInstance.Execute(S tring[] args) in
C:\Data\CustomerProjects\Inteevo\Libraries\Inteevo .SingleInstanceApp\SingleInstance.cs:line
26
at
System.Runtime.Remoting.Messaging.StackBuilderSink ._PrivateProcessMessage(IntPtr
md, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInContext, Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .PrivateProcessMessage(RuntimeMethodHandle
md, Object[] args, Object server, Int32 methodPtr, Boolean
fExecuteInContext, Object[]& outArgs)
at
System.Runtime.Remoting.Messaging.StackBuilderSink .SyncProcessMessage(IMessage
msg, Int32 methodPtr, Boolean fExecuteInContext)
InnerException:

Sep 26 '08 #7
I did decompile it in the past, pretty deep stuff with use of lots of
classes/methods marked "internal".

Sep 26 '08 #8
PS: I am going to try named pipes instead of remoting, see if that helps.
Pete
Sep 26 '08 #9
I adapted this....
http://www.flawlesscode.com/post/200...t-passing.aspx

So now I just write

new SingleInstanceApplication<MainForm>("MyCompany.MyA pp");

instead of

Application.Run(new MainForm());
Works a treat :-)
Thanks for your input guys, I appreciate it as always!


Pete

Sep 26 '08 #10
On Fri, 26 Sep 2008 00:17:48 -0700, Peter Morris
<mr*********@spamgmail.comwrote:
Hi Pete

Using Invoke doesn't work either.....
Your stack trace looks odd. In particular, you seem to be executing your
Application.Run() call on a thread without [STAThread]. This will, as the
exception implies, cause all sorts of problems. If this is in fact the
difficulty you're running into, then I'd say it's a good thing you're
running into this problem, because it's one you need to address regardless.

Without a concise-but-complete code sample, it's hard to comment on the
specific technique you're using to ensure a single-instance application
here, but I really don't see any reason that you should have a call to
Application.Run() in the same call stack with any of the remoting stuff.
By definition, the stuff coming in from remoting should never wind up
calling Application.Run()...that's the point of making your application
single-instance.

Pete
Sep 26 '08 #11

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

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
12
by: Dennis Plöger | last post by:
Hi all! I'm currently having some problems parsing a char array in c++. (And yes, I'm a half-newbie ;-)) Perhaps you can help me with this: #include <iostream> using std::cout; void...
11
by: Thom Little | last post by:
I would like three states on an icon ... Left Click Right Click Double Click Left Click is fired at least once on a Double Click Is there a good example that shows how to determine if the...
3
by: R Millman | last post by:
under ASP.NET, single stepping in debug mode appears not to stop within event procedures. i.e. 1) Create web page with submit button and event procedure for the click event in the code behind...
2
by: John Dann | last post by:
I'm retrieving some columns from a database with numeric values that fall comfortably within the range of the Single type and I'm tempted to use Single for the relevant column type in the retrieved...
13
by: Kevin Walzer | last post by:
Which of the Windows/Unix package builders for Python applications is capable of creating single-file executables? I'm thinking of: 1. py2exe 2. Mcmillan Installer/PyInstaller 3. cxfreeze ...
7
by: ashley.ward | last post by:
We have been using VB6 to develop small custom apps that access an Oracle database, in order to extend a larger product that is developed by our colleagues in Germany (who use C++ and Java). As...
3
by: Eric Layman | last post by:
Hi, I've saved data into the db by doing a replace() on single quote. Right now on data display on a datagrid, it shows double single quote. How do I make changes during run time of datagrid...
2
by: Reporter | last post by:
I got the following example from http://www.evolt.org/article/User_Friendly_Forms_in_PHP/20/60144/index.html : echo '<tr><td>First name:</td><td><input type="text" name="first_name"...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.