打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用MSMQ传送文字、类、图片
学习MSMQ期间做的测试程序。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

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

        
int iCount = 0;
        
/// <summary>
        
/// 发送文字。
        
/// </summary>

        private void btnSend1_Click(object sender, EventArgs e)
        
{
            iCount
++;
            
try
            
{
                msgq1.Send(
"Message Contents: "+iCount);
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }

        
/// <summary>
        
/// 接收文字。
        
/// </summary>

        private void btnReceive1_Click(object sender, EventArgs e)
        
{
            System.Messaging.Message m;
            
string str;
            
try
            
{
                m 
= msgq1.Receive(new TimeSpan(003));
                str
=(string)m.Body;
            }

            
catch
            
{
                str 
= "没有返回消息。";
            }

            MessageBox.Show(str);
        }

        
/// <summary>
        
/// 发送一个类。
        
/// </summary>

        private void btnSendClass_Click(object sender, EventArgs e)
        
{
            Student no1 
= new Student();
            no1.name 
= "my";
            no1.age 
= 20;
            no1.sex 
= "";

            Student no2 
= new Student();
            no2.name 
= "朋友1";
            no2.age 
= 21;
            no2.sex 
= "";

            Student no3 
= new Student();
            no3.name 
= "朋友2";
            no3.age 
= 22;
            no3.sex 
= "";

            no1.friends.Add(no2);
            no1.friends.Add(no3);

            System.Messaging.Message m 
= new System.Messaging.Message();
            m.Body 
= no1;
            
try
            
{
                msgq1.Send(m);
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message 
+ " " + ex.InnerException.Message);
            }

        }

        
/// <summary>
        
/// 接收一个类。
        
/// </summary>

        private void btnReceiveClass_Click(object sender, EventArgs e)
        
{
            System.Messaging.Message m;
            Student st;
            
try
            
{
                m
=msgq1.Receive(new TimeSpan(0,0,3));
                m.Formatter 
= new System.Messaging.XmlMessageFormatter(new Type[] typeof(Student) });
                st 
= (Student)m.Body;
                
string str = " name:"+st.name;
                str 
+= "\r\n age:"+st.age;
                str 
+= "\r\n sex:" + st.sex;
                str 
+= "\r\n friends:\r\n";
                
foreach (Student friend in st.friends)
                
{
                    str 
+= "[name:" +friend.name;
                    str 
+= " age:" + friend.age;
                    str 
+= " sex:" + friend.sex+"]\r\n";
                }

                MessageBox.Show(str);
            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }

        
/// <summary>
        
/// 发送图片
        
/// </summary>

        private void btnSendImage_Click(object sender, EventArgs e)
        
{
            Stream imgStream;
            System.Messaging.Message mImg 
= new System.Messaging.Message();
            openFileDialog1.Filter 
= "ImageFile(*.bmp,*.jpg,*.gif)|*.bmp;*.jpg;*.gif";
            openFileDialog1.FilterIndex 
= 1;
            
if (openFileDialog1.ShowDialog() == DialogResult.OK)
            
{
                
if ((imgStream = openFileDialog1.OpenFile()) != null)
                
{
                    mImg.BodyStream 
= imgStream;
                    
try
                    
{
                        msgq1.Send(mImg);
                    }

                    
catch (Exception ex)
                    
{
                        MessageBox.Show(ex.Message);
                    }

                    
finally
                    
{
                        imgStream.Close();
                    }

                }

            }

        }

        
/// <summary>
        
/// 接收图片
        
/// </summary>

        private void btnReceiveImage_Click(object sender, EventArgs e)
        
{
            Bitmap bmp;
            Stream imgStream;
            System.Messaging.Message mImg 
= new System.Messaging.Message();
            
bool bReceive = false;
            
try
            
{
                mImg 
= msgq1.Receive(new TimeSpan(003));
                bReceive 
= true;
            }

            
catch
            
{
                MessageBox.Show(
"没有接收到消息。");
            }

            
try
            
{
                
if (bReceive)
                
{
                    imgStream 
= mImg.BodyStream;
                    bmp 
= new Bitmap(imgStream);
                    pictureBox1.Image 
= bmp;
                }

            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }


        
#region Student类

        
public class Student
        
{
            
public string name;
            
public System.Collections.ArrayList friends;
            
public int age;
            
public string sex;
            
public Student()
            
{
                friends 
= new System.Collections.ArrayList();
            }

        }

        
#endregion

    }

}

要使用MSMQ必须安装win消息队列组件。
---------------------------------------------------------------------
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
学生信息管理系统
带有消息机制的线程
VB.NET语法规则三大要素
WebService开发笔记 2 -- VS 2005 访问WebServcie更简单
详解VB.net文件传输.(可传输任意文件)
MSMQ(Microsoft Message Queue,微软消息队列)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服