Menu
Woocommerce Menu

进程间通信,静态构造函数

0 Comment
    class Program
    {
        static void Main(string[] args)
        {
            // 在bus1实例化后调用静态构造函数,只调用一次
            Bus bus1 = new Bus(71);
            Bus bus2 = new Bus(72);
            bus1.Drive();
            System.Threading.Thread.Sleep(3600);
            bus2.Drive();
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    public class Bus
    {
        protected static readonly DateTime globalStartTime;
        protected int RouteNumber { get; set; }
        static Bus ()
        {
            globalStartTime = DateTime.Now;
            Console.WriteLine("static constructor sets global start time to {0}", globalStartTime.ToLongTimeString());
        }
        public Bus (int routeNum)
        {
            RouteNumber = routeNum;
            Console.WriteLine("Bus #{0} is created", RouteNumber);
        }
        public void Drive()
        {
            TimeSpan elapsedTime = DateTime.Now - globalStartTime;
            Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}", this.RouteNumber, elapsedTime.TotalMilliseconds, globalStartTime.ToLongTimeString());
        }
    }

选择SendMessage向另黄金年代进度发送WM_COPYDATA消息

1 var remoter = new Remoter();
2 remoter.RequestMethod = "POST";
3 remoter.URL = "你要请求的URL";
4 remoter.JsonContent = "你要想URL发送的JSON数据";
5 var response = remoter.GetRemoteData();

1.
静态构造函数用于初阶化静态数据,或用来执行仅需实行二遍的特出操作。

Send端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;

namespace SentTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Title = "发送窗口";
        }

        const int WM_COPYDATA = 0x004A;

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        [DllImport("User32.dll")]
        public static extern int SendMessage(int hwnd, int msg, int wParam, ref COPYDATASTRUCT IParam);
        [DllImport("User32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            String strSent = "需要发送的信息";

            int WINDOW_HANDLE = FindWindow(null, "接收窗口");
            if (WINDOW_HANDLE != 0)
            {
                byte[] arr = System.Text.Encoding.Default.GetBytes(strSent);
                int len = arr.Length;
                COPYDATASTRUCT cdata;
                cdata.dwData = (IntPtr)100;
                cdata.lpData = strSent;
                cdata.cData = len + 1;
                SendMessage(WINDOW_HANDLE, WM_COPYDATA, 0, ref cdata);
            }
        }
    }
}
1 var remoter = new Remoter();
2 remoter.RequestMethod = "GET";
3 remoter.URL = "这里是你要请求的URL";
4 var response = remoter.GetRemoteData();

2.
静态布局函数既未有访问修饰符,也绝非参数。

Get端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace GetTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IntPtr hwnd;
        public MainWindow()
        {
            InitializeComponent();
            this.Title = "接受窗口";

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.Closed += new EventHandler(MainWindow_Closed);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //窗口加载完毕才可用,否则会报错。
            hwnd = new WindowInteropHelper(this).Handle;
            HwndSource source = HwndSource.FromHwnd(hwnd);
            if (source != null) source.AddHook(WndProc);
        }

        void MainWindow_Closed(object sender, EventArgs e)
        {
            try
            {
                HwndSource.FromHwnd(hwnd).RemoveHook(WndProc);
            }
            catch (Exception)
            {

                throw;
            }

        }

        const int WM_COPYDATA = 0x004A;//WM_COPYDATA消息的主要目的是允许在进程间传递只读数据。
        //Windows在通过WM_COPYDATA消息传递期间,不提供继承同步方式。
        //其中,WM_COPYDATA对应的十六进制数为0x004A
        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public int cData;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpData;
        }

        //wpf用此方法
        private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WM_COPYDATA)
            {
                COPYDATASTRUCT cdata = new COPYDATASTRUCT();
                Type mytype = cdata.GetType();
                cdata = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, mytype);
                this.textBox1.Text = cdata.lpData;
            }
            return IntPtr.Zero;
        }

        //WinFrom用此方法
        /* protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
            {
            case WM_COPYDATA:
                COPYDATASTRUCT cdata = new COPYDATASTRUCT();
                Type mytype = cdata.GetType();
                cdata = (COPYDATASTRUCT)m.GetLParam(mytype);
                this.textBox1.Text = cdata.lpData;
                break;
            default:
                base.DefWndProc(ref m);
                break;
            }
        } */
    }
}

 

5.
譬如静态布局函数引发那一个,运行时将不会重复调用该布局函数,况兼在程序运维所在的行使程序域的生存期内,类型将维持未发轫化。

参谋文书档案:

c# 进程间同步落成进程之间通讯的三种方法

银河国际网址手机版 1银河国际网址手机版 2

  

 

3.
在开创第二个实例或引用任宋颖态成员早先,将机关调用静态布局函数来起头化类。

银河国际网址手机版 3银河国际网址手机版 4

4.
无法直接调用静态结构函数。

银河国际网址手机版 5银河国际网址手机版 6

二、代码

  3)
此代码以【面向对象】、【C#闭包】、【异步回调】、【超时】、【等待】、【自动重试】格局完成及完结,且能够布置扩充

银河国际网址手机版 7银河国际网址手机版 8

  
 布置延续会将生产中国化学工业进出口总集团解难题的累累地点汇聚成叁个类型【Meng.Net.dll】并开源至GitHub上,接待沟通,协同提升~~

可从布局文件读取部分

异步响应成功后,在场地保持中赋值结果

银河国际网址手机版 9银河国际网址手机版 10

银河国际网址手机版,  private string
Result 回调状态保持对象,保存响应结果用

异步响应回调,闭包与自状态保持

  private bool
ResponseFlag 与央浼对应的响应是不是中标标志

Remoter.cs

 1         private void RemoteNew(Action<Remoter, string> action) 
 2         {
 3             //
 4             var reNum = 0;
 5             for (var i = 0; i < this.RetryCount; i++)
 6             {
 7                 try
 8                 {
 9                     // 此处省略
10                     //... ...
11 
12                     //
13                     break;
14                 }
15                 catch (Exception ex)
16                 {
17                     Thread.Sleep(this.TrySleep);
18                     reNum++;
19                     if (reNum == this.RetryCount)
20                     {
21                         throw new Exception(string.Format("重试失败!重试次数:{0}次,失败原因:{1}", this.RetryCount, ex.Message));
22                     }
23                     continue;
24                 }
25             }
26         }                        

                                    2016-09-24
 22:37  周六

四、安排中的开源项目…

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

相关文章

网站地图xml地图