代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;namespace yxwinsocket
{
public partial class Form1 : Form
{
BackgroundWorker bgWorker = null;
public Form1()
{
InitializeComponent();
bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCancellation = true;
this.AddEvent();
} private void Form1_Load(object sender, EventArgs e)
{//初始化时发送按钮起用,停止按钮禁用
this.btnStartReceive.Enabled = true;
this.btnStopRecevie.Enabled = false;
} private void AddEvent()
{//添加事件
this.bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
this.btnSend.Click += new EventHandler(btnSend_Click);
this.btnStartReceive.Click += new EventHandler(btnStartReceive_Click);
this.btnStopRecevie.Click += new EventHandler(btnStopRecevie_Click);
}
private void btnSend_Click(object sender, EventArgs e)
{//发送
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sendSocket.Connect("202.38.79.111", 1099);
//sendSocket.Connect("127.0.0.1", 8099); byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text); sendSocket.Send(buffer); sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
} private void btnStartReceive_Click(object sender, EventArgs e)
{//开始接收
this.btnStartReceive.Enabled = false;
this.bgWorker.RunWorkerAsync();
//this.bgWorker.a
this.btnStopRecevie.Enabled = true;
} private void btnStopRecevie_Click(object sender, EventArgs e)
{//停止接收
if (this.bgWorker.IsBusy)
{
this.bgWorker.CancelAsync();
this.btnStartReceive.Enabled = true;
this.btnStopRecevie.Enabled = false;
}
} private IPAddress myIP = IPAddress.Parse("202.38.79.111"); void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{//
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(myIP, 1099);
receiveSocket.Bind(endpoint);
receiveSocket.Listen(10);//表示能同时接收10个监听
try
{
while (true)
{
Socket tmpSocket = receiveSocket.Accept();
byte[] buffer = new byte[tmpSocket.ReceiveBufferSize];
if (tmpSocket.Receive(buffer) > 0)
{
textBox2.Text += Encoding.UTF8.GetString(buffer)+ Environment.NewLine; //不断的接收
}
else
{
System.Threading.Thread.Sleep(1000);//休眠
} }
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}其中
private void btnStartReceive_Click(object sender, EventArgs e)
{//开始接收
this.btnStartReceive.Enabled = false;
this.bgWorker.RunWorkerAsync();
//this.bgWorker.a
this.btnStopRecevie.Enabled = true;
}
出现问题,错误提示"This BackgroundWorker is currently busy and cannot run multiple tasks concurrently."
请高手们帮帮忙,该如何解决???
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Net;namespace yxwinsocket
{
public partial class Form1 : Form
{
BackgroundWorker bgWorker = null;
public Form1()
{
InitializeComponent();
bgWorker = new BackgroundWorker();
bgWorker.WorkerSupportsCancellation = true;
this.AddEvent();
} private void Form1_Load(object sender, EventArgs e)
{//初始化时发送按钮起用,停止按钮禁用
this.btnStartReceive.Enabled = true;
this.btnStopRecevie.Enabled = false;
} private void AddEvent()
{//添加事件
this.bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
this.btnSend.Click += new EventHandler(btnSend_Click);
this.btnStartReceive.Click += new EventHandler(btnStartReceive_Click);
this.btnStopRecevie.Click += new EventHandler(btnStopRecevie_Click);
}
private void btnSend_Click(object sender, EventArgs e)
{//发送
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sendSocket.Connect("202.38.79.111", 1099);
//sendSocket.Connect("127.0.0.1", 8099); byte[] buffer = Encoding.UTF8.GetBytes(textBox1.Text); sendSocket.Send(buffer); sendSocket.Shutdown(SocketShutdown.Both);
sendSocket.Close();
} private void btnStartReceive_Click(object sender, EventArgs e)
{//开始接收
this.btnStartReceive.Enabled = false;
this.bgWorker.RunWorkerAsync();
//this.bgWorker.a
this.btnStopRecevie.Enabled = true;
} private void btnStopRecevie_Click(object sender, EventArgs e)
{//停止接收
if (this.bgWorker.IsBusy)
{
this.bgWorker.CancelAsync();
this.btnStartReceive.Enabled = true;
this.btnStopRecevie.Enabled = false;
}
} private IPAddress myIP = IPAddress.Parse("202.38.79.111"); void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{//
Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(myIP, 1099);
receiveSocket.Bind(endpoint);
receiveSocket.Listen(10);//表示能同时接收10个监听
try
{
while (true)
{
Socket tmpSocket = receiveSocket.Accept();
byte[] buffer = new byte[tmpSocket.ReceiveBufferSize];
if (tmpSocket.Receive(buffer) > 0)
{
textBox2.Text += Encoding.UTF8.GetString(buffer)+ Environment.NewLine; //不断的接收
}
else
{
System.Threading.Thread.Sleep(1000);//休眠
} }
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}
}
}
}其中
private void btnStartReceive_Click(object sender, EventArgs e)
{//开始接收
this.btnStartReceive.Enabled = false;
this.bgWorker.RunWorkerAsync();
//this.bgWorker.a
this.btnStopRecevie.Enabled = true;
}
出现问题,错误提示"This BackgroundWorker is currently busy and cannot run multiple tasks concurrently."
请高手们帮帮忙,该如何解决???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货