Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

winforms - Passing Information Between Applications in C#

All. Firstly I am aware of the question Send data back to .exe from dll, however the answers there leave too many open ends for me and as I have no experience of what I am attempting I feel a new question is warranted.

I have an existing C# [All WinForms here] app that heavily relies on SQL. We have been asked (by clients) to provide an SQL Editor and library that can be used to develop and test SQL, but that can also be used to paste directly back in to the main application. The new SQLEditor is a multi-threaded application that parses and executes TSQL. I now have some things to consider; what is the best way to launch this second application from the main application::

  1. Make the second app into a DLL and load into the main project, call the second app as a new form (SqlEditor sqlEd = new SqlEditor() etc.)? What are the implication in terms of thread bombardment, would I need [STAThread] - as I want both multithreaded apps to be available and active at the same time.

  2. To launch as a separate .exe from the main application?

Depending on you advice; in either of the above cases - what is the best way I can pass information back to the main application from a click event in the second application whilst they are still both running and active [WCF, ApplicationDomains etc.]? Would the Observer design pattern come in to play here?

To make this question a little more appealing, here is the SQL Editor:

Editor

I plan to have a button which pastes the selected SQL back into the main application.

I am also aware that there are multiple questions here - which I apologise for. Thanks very much for your time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you need a simple way to do an outproc communication of two WinForms applications, then why don't you use the Clipboard with a custom format?

In the source application:

    // copy the data to the clipboard in a custom format
    Clipboard.SetData( "custom", "foo" );

In the destination application, create a timer to peek the clipboard:

    private void timer1_Tick( object sender, EventArgs e )
    {
        // peek the data of a custom type
        object o = Clipboard.GetData( "custom" );
        if ( o != null )
        {
            // do whatever you want with the data
            textBox1.Text = o.ToString();
            // clear the clipboard
            Clipboard.Clear();
        }
    }

This should fit your needs and it's still really simple as it doesn't require any heavyweight outproc communication mechanisms.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...