zoukankan
html css js c++ java
使用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(
0
,
0
,
3
));
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(
0
,
0
,
3
));
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);
}
}
Student类
#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消息队列组件。
-----------------------------------------------------------------
我做的小程序们
【推荐】Web版短信管理平台源码
WinForm版短信管理平台源码
移动短信程序源码Win服务版(CMPP3.0/CMPP2.0协议)
移动物联网卡短信源码(CMPP3.0协议,支持MsSql/MySql数据库)
C#实现联通短信Sgip协议程序源码
C#实现电信短信SMGP协议程序源码
C#实现移动短信CMPP服务端程序源码
小y的QQ:
28657321
(欢迎交流)
查看全文
相关阅读:
user.table.column, table.column 或列说明无效
spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务
easyui-treegrid移除树节点出错
jquery easyui easyui-treegrid 使用异步加载数据
Java动态调用webService,axis2动态调用webService
让IE支持Css3属性(圆角、阴影、渐变)
float浮动之后高度自适应失效解决方案
td中使用overflow:hidden; 无效解决方案
jquery插件select2事件不起作用(select2-3.5.4)
wsdl自动生成Java代码,根据wsdl生成Java代码
原文地址:https://www.cnblogs.com/tuyile006/p/1100275.html
最新文章
【bzoj4719】[Noip2016]天天爱跑步 权值线段树合并
【bzoj2396】神奇的矩阵 随机化
【bzoj2694】Lcm 莫比乌斯反演+线性筛
【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛
【bzoj4001】[TJOI2015]概率论 生成函数+导数
如此大量的代码,但每个类里面的代码却不显得特别多,原因。。。。。。。。。。。。
如何应对杂乱无章的代码
把你的黄金时间投入到可迁移的相关技能中
停止学习框架(Stop Learning Frameworks)
国外(英文)——WPF较好的奇葩问题解决网站
热门文章
C#定义一个方法的3种形式
使用框架的——好处
WPF经典编程模式-MVVM示例讲解
错误不能中断(不许因错误或异常而产生阻断性Bug)
框架设计——View
权限管理库
Goland 激活码
Go 微服务架构Micro相关概念理解
Consul 随记
protobuf / Consul / 边缘计算 / MEC / CDN / Serverless / GraphQL / 微服务 / 网关 / 云原生 / Serverless (真能造概念啊!!!)
Copyright © 2011-2022 走看看