⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 清华大学出版社出版的 移动应用开发宝典 张大威(2008)的附书源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Messaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace MessagingSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //helper function to launch a process and wait for the result code
        private static int ShellWait(string app, string args)
        {
            if (!File.Exists(app))
            {
                return -1;
            }
            Process p = Process.Start(app, args);
            p.WaitForExit();
            return p.ExitCode;
        }

        private static void InitMsmq()
        {
            string msmqadmPath = "\\Windows\\msmqadm.exe";

            if (ShellWait(msmqadmPath, "status") < 0)
            {
                //failed, msmq isn't present

                string apppath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                
                
                if (System.Environment.OSVersion.Version.Major < 5)
                {
                    //copy files (PPC2003)
                    File.Copy(Path.Combine(apppath, "msmqadm.exe"), "\\Windows\\msmqadm.exe");
                    File.Copy(Path.Combine(apppath, "msmqadmext.dll"), "\\Windows\\msmqadmext.dll");
                    File.Copy(Path.Combine(apppath, "msmqd.dll"), "\\Windows\\msmqd.dll");
                    File.Copy(Path.Combine(apppath, "msmqrt.dll"), "\\Windows\\msmqrt.dll");
                }
                else
                {
                    //Install CAB (WM5.0)
                    string cabname = Path.Combine(apppath, "msmq.arm.cab");
                    ShellWait("\\Windows\\wceload.exe", "\"" + cabname + "\"");
                }
                //initialize
                ShellWait(msmqadmPath, "register cleanup");
                ShellWait(msmqadmPath, "register install");
                ShellWait(msmqadmPath, "register");
                ShellWait(msmqadmPath, "enable binary");
                
                //register the service
                IntPtr handle = ActivateDevice("Drivers\\BuiltIn\\MSMQD", 0);
                CloseHandle(handle);

                //final check on the status
                if (ShellWait(msmqadmPath, "status") < 0)
                {
                    throw new ApplicationException("Failed to register MSMQ");
                }
                
            }

           
        }
        
        [DllImport("coredll.dll", SetLastError=true)]
        private extern static int CloseHandle(IntPtr handle);

        [DllImport("coredll.dll", SetLastError = true)]
        private extern static IntPtr ActivateDevice(string lpszDevKey, int dwClientInfo);

        private void Form1_Load(object sender, EventArgs e)
        {
            //call our initialization method
            InitMsmq();

            //create our local queue if not present
            if (!MessageQueue.Exists(mqLocal.Path))
            {
                MessageQueue.Create(mqLocal.Path);
            }

            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ReceiveThread));
            t.IsBackground = true;
            t.Start();
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            //send the current details to the message queue
            //for our imaginary CRM server application

            try
            {
                Prospect p = new Prospect();
                p.Name = txtName.Text;
                p.Company = txtCompany.Text;
                p.Number = txtNumber.Text;

                mqRemote.Send(p);
                //send a local copy
                mqLocal.Send(p);

                //clear the form
                txtName.Text = "";
                txtCompany.Text = "";
                txtNumber.Text = "";
            }
            catch(MessageQueueException mqe)
            {
                MessageBox.Show(mqe.ToString());
            } 

        }

        private void AppendToListBox(string newItem)
        {
            listBox1.Items.Add(newItem);
        }
        private delegate void AppendToListBoxDelegate(string newItem);

        private bool listening = true;

        private void ReceiveThread()
        {
            XmlMessageFormatter formatter = new XmlMessageFormatter(new Type[] { typeof(Prospect) });
            mqLocal.Formatter = formatter;

            while (listening)
            {
                Message m = mqLocal.Receive();
                Prospect p = (Prospect)m.Body;
                this.Invoke(new AppendToListBoxDelegate(AppendToListBox), new object[] { p.ToString() });
            }
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            listening = false;
        }
    }

    [Serializable()]
    public class Prospect
    {
        private string name;
        private string company;
        private string number;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Company
        {
            get { return company; }
            set { company = value; }
        }

        public string Number
        {
            get { return number; }
            set { number = value; }
        }

        public override string ToString()
        {
            return name + "," + company + "," + number;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -