c#暂停循环 for(i=0;i<10000;i++){ lable1.text=i.toString();}如果做到点击暂停, 再点击从暂停处开始? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主看看这篇文章:http://www.sunnychen.org/chinese/article.asp?id=7文章里面是点击一个button开始,另一个button停止。跟你的需求差不太多。你稍作修改就可以用。 就像这样,初始时timer1的Enabled=为falseif(this.timer1.Enabled==false){ this.button1.Text="暂停"; this.timer1.Enabled=true;}else{ this.button1.Text="开始"; this.timer1.Enabled=false;}将它写到按钮中 Object thisLock = new Object();lock (thisLock){ // Critical code section.} private int i = 0; private Boolean flag= true; private void updatelable() { while (flag) { i++; Thread.Sleep(1000); label1.Text = i.ToString(); } } private void button1_Click(object sender, EventArgs e) { flag = true; Thread th = new Thread(new ThreadStart(updatelable)); th.Start(); } private void button2_Click(object sender, EventArgs e) { flag = false; }兄弟我不才,乱搞了一个.你试下 如果永远睡了就醒不来了 难道只有睡一定时间吗 可以尝试用AutoEventhanlder 只是建议...现在没空搞 应该是放在线程里操作 Thread th; delegate void LblMessage(string str); private void Message(string str) { richTextBox1.Text = str.ToString(); } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { th = new Thread(new ThreadStart(Test)); th.Start(); } private void Test() { for (int i = 0; i < 1000000; i++) { LblMessage lMessage = new LblMessage(Message); this.Invoke(lMessage, i.ToString()); } } private void button2_Click(object sender, EventArgs e) { if (th.ThreadState == ThreadState.Running) { th.Suspend(); } else { th.Resume(); } } private int m_nBeginIndex = 0;private bool m_bPause = false;for (int i = this.m_nBeginIndex; i < 1000000; i++) { this.m_nBeginIndex = i; if(m_bPause ) break;} 然后你在某处设置变量m_bPause 就OK了嘛 private int m_nBeginIndex = 0; private bool m_bPause = false; int i = 0;while(true) { if(m_bPause ) continue; if(i>=1000000) break; 你的代码.... this.m_nBeginIndex = i++; } 这样就好了 AutoResetEvent autoEvent = new AutoResetEvent(false);bool threadSwitch = false;button_onclick(){ if(button.Text.Equal("暂停")){ threadSwitch = true;button.Text = "开始";}else{threadSwitch = false;autoEvent.Set();}}for(i=0;i <10000;i++) { lable1.text=i.toString(); if(threadSwitch){ autoEvent.WaitOne();}} AutoResetEvent autoEvent = new AutoResetEvent(false); bool threadSwitch = false; button_onclick() { if(button.Text.Equal("暂停")) { threadSwitch = true; button.Text = "开始"; } else { button.Text = "暂停";threadSwitch = false; autoEvent.Set(); } } for(i=0;i <10000;i++) { lable1.text=i.toString(); if(threadSwitch) { autoEvent.WaitOne(); } } 红色的忘记加了。 是这样的 这个循环只执行一个远程webservice的方法 我想按下去暂停这个方法 再点一下接着继续 我的方法可以满足你的需求,你把循环放到一个后台线程里,然后通过AutoResetEvent去通知线程是否继续还是暂停,这个做法的好处在于对于webservice的处理与主线程脱离,也就是不会干扰你的界面操作。然后当被界面通知时能阻塞住线程,不占用cpu资源,你的界面可以继续做其他操作。点击开始又可以运行大致做法见15楼 tmxk2002 现在暂停是可以停下来了 不过点完后界面死了 没法开始了 1)肯定异步执行这个循环,否着点了button之后界面就挂了2)单独用ResetEvent之类的互斥量似乎不可用,至少要个标志量在循环里检测和控制3) 线程暂停的方案不推荐,.Net2.0开始就不推荐使用线程的Resume,Suspend等操作 按tmxk2002 的方法做的 点了暂停后 界面无反应 直接死掉 这种需求做c\s就够LZ喝一壶的了,再作成webservice的,访问量大了不挂服务器才是奇迹呢,这个需求是不是应该变通下,用js来实现还是比较现实的,因为做为webservice它会为你的每次请求都重新load自己一次,这就是为什么你的页面会僵死,所以你希望服务器始终为你保留这个线程或是逻辑过程,本来就不合理,如果这样处理的话,服务器又能为几个人服务呢,并发访问不就完蛋了吗,即便你想一切方法实现,结果也不会好的,从新想个方法吧 是一个短信群发的软件 服务器上做成了webservice 我可以给你举个例子,就是服务器上的IIS里有10个线程代表着有10个人在request,他们10个人都调用这个webservice,就意味着有10个webservcie的副本,当IIS为其中的一个reques做出了response时,意味着这次请求响应结束,这个对应的webservice的副本也就消失了,那么你再访问就是一个新的request,也就是一个新的webservcie副本产生,我当前就是这么认为的,如果哪位仁兄能改变或是能有别的方式,就赶紧贡献出来,我这里抛砖引玉了,包括SOA架构的服务和客户端模式,我做过简单的实验,也是这样的。 vc++的for循环或while循环暂停很容易做到,但是c#的我就不会了 这个解决的方式我觉得,就是让webservice这几个副本之间能互跨,使用像session这样的变量(webservice中是可以的)或是Server这样的变量,只要之间能跨域就有办法可想,毕竟你可以恢复你上次会话完结后的变量 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;namespace WindowsFormsApplication1{ public sealed class SMSSender { bool _threadSwitch = false; bool _pauseSwitch = false; AutoResetEvent _resetEvent = new AutoResetEvent(false); Thread _worker = null; object _locker = new object(); public delegate void DoSomething(object sender, EventArgs e); public event DoSomething DoSomethingEvent; private void OnDoSomethingEvent() { try { if (DoSomethingEvent != null) { DoSomethingEvent(this, new EventArgs()); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } public void Start() { lock(_locker) { if (_worker == null) { _threadSwitch = true; _worker = new Thread(Run); _worker.IsBackground = true; _worker.Start(); } } } void Run() { while (_threadSwitch) { try { OnDoSomethingEvent(); if (_pauseSwitch) { _resetEvent.WaitOne(); } } catch (System.Exception ex) { MessageBox.Show(ex.Message); } Thread.Sleep(0); } } public void Continue() { lock (_locker) { if (!_pauseSwitch) { return; } _pauseSwitch = false; _resetEvent.Set(); } } public void Pause() { lock (_locker) { if (_pauseSwitch) { return; } _pauseSwitch = true; } } public void Stop() { lock (_locker) { if (_worker == null) { return; } _threadSwitch = false; _pauseSwitch = false; _resetEvent.Set(); if (_worker.IsAlive) { try { _worker.Abort(); } catch { } } _worker = null; } } }} using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Threading;namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private SMSSender smsSender = new SMSSender(); private void button1_Click(object sender, EventArgs e) { if (button1.Text.Equals("Pause")) { smsSender.Pause(); button1.Text = "Continue"; } else { smsSender.Continue(); button1.Text = "Pause"; } } private void Form1_Load(object sender, EventArgs e) { smsSender.DoSomethingEvent += new SMSSender.DoSomething(smsSender_DoSomethingEvent); } void smsSender_DoSomethingEvent(object sender, EventArgs e) { MessageBox.Show("working"); Thread.Sleep(1000); } private void button2_Click(object sender, EventArgs e) { button1.Text = "Pause"; smsSender.Start(); } private void button3_Click(object sender, EventArgs e) { smsSender.Stop(); } }} namespace WindowsFormsApplication1{ partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(107, 154); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Pause"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(109, 125); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "Start"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(107, 183); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 2; this.button3.Text = "Stop"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; }} namespace WindowsFormsApplication1{ partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(107, 154); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Pause"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(109, 125); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 1; this.button2.Text = "Start"; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(107, 183); this.button3.Name = "button3"; this.button3.Size = new System.Drawing.Size(75, 23); this.button3.TabIndex = 2; this.button3.Text = "Stop"; this.button3.UseVisualStyleBackColor = true; this.button3.Click += new System.EventHandler(this.button3_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; }} using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace WindowsFormsApplication1{ static class Program { /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } }}这是我刚刚花20分钟写的,包括测试程序,我想大家不用在这里磨嘴皮子了,coding-testing-validating,这是程序员最应该做的事情了。已经通过测试,lz可以直接将代码copy过去用了,在dosomethingevent事件里写上你访问webservice的代码就可以了,如果你觉得dosomethingevent的名字不好听,可以改成Sending? 很精彩,不过我想看看调webservice的情况,SMSSender的事件句柄调用服务webservice的方法函数,意味着LZ说的for循环是要放到webservice的方法里了,对吗? 佩服,不过是不是成功与否并且做到逻辑不混乱还需要在看看,这是我个人认为的,况且LZ的for循环是在服务器段保持暂停状态,在客户端做成这样有什么作用呢?不明白,可能理解有误吧。 其实在webservcie进行暂停也可以做到的但可能需要用到WCF,设置wcf的访问模式为PerSession,WCF可以维持Session状态,把SMSSender放到ServiceHost端,作为ServiceHost的私有属性,就像Form里一样,只是把button的事件改成Service的方法,然后在客户端调用Start,Pause,Continue,Stop就可以了 WS*是可以使用维持服务器端状态的变量的,这个没有什么疑意的,客户端的暂停不是问题所在,如果当前客户端执行1ms来调用webservice,结果webservice的方法没运行完,就把客户端的调用事件给睡了,那服务器回答谁啊,半链接请求攻击,服务器能受得了吗,http的缺点就是这样折磨服务端开发的 第一,客户端是否执行1ms来调用服务端,这个是由业务需求来决定的,不是你在这里假设的,如果业务需要这样,那1ms调用一次也没有什么;第二,如果是在服务器端做暂停控制,怎么可能给客户端睡了?是在服务器端阻塞,不是在客户端。并且对于SMSSender来说,别人操作的是它的变量,与它在Run里做什么事,做多久是隔离开来的,这就是多线程的好处,在它执行Run的时候,不管界面调用多少次continue、pause,它始终只认最后的状态。第三,webservice应该是同步服务,在这里不应是异步服务,一个请求过来,完成请求,返回结果,同步的调用怎么可能它不知道返回结果给谁呢?第四,至于服务器是不是被请求攻击,那是系统安全的问题。如果是正常的调用,怎么可能会是攻击呢?如果是非法调用,系统在安全机制上已经排除掉了。第五,如果系统真的由于安全不到位被人攻击,即使不是http,就算是tcp,哪怕更底层的通讯,都会折磨死服务器端的。 我仔细看了你的这五点,我在第三点上有疑问,同步服务,我想让webservice上的那个方法暂停,你让服务器端阻塞,也就是客户端一直保持与服务器的链接了,这样子是否欠妥,如果不保持链接,我下次再请求这个webservice的方法时,怎么恢复到我的上一次状态,还是要使用像session这样的变量吧,如果不使用,你还有什么方法呢,请教一下,共享一下你的知识了 你看第二点,不会阻塞客户端的,客户端也只是去调用了continue或者pause,也就是只是更改一个bool变量,设置了一下信号量而已。可能前面没有说的明白。如果要在服务器端做发送sms的事情,那么SMSSender就移到服务器端上去了。它的Continue和Pause等方法,就要暴露成webservice了,客户端在开始,继续和暂停按钮时就是直接调用这些webservice方法而已。当然这种方法没必要采用,因为websrvice就是服务,它只要提供一个发送SMS的服务即可,至于发还是不发,则应该由客户端来控制的。就像我代码写的一样,在dosomething里就是调用发送sms的webservice,而且放在服务端这样做也不行,因为必须为每一个客户端位置一个后台线程,如果有几千个客户,那服务器开销就太大了。完全可以把这部分损耗放到客户端,减轻服务器端资源损耗,让它去做更应该做的事情。 学了,这就看明白了,“它的Continue和Pause等方法,就要暴露成webservice了,客户端在开始,继续和暂停按钮时就是直接调用这些webservice方法而已” 你的意思是把你写的客户端调用放到服务器端运行,在服务器端开很多线程,会耗资源,“完全可以把这部分损耗放到客户端,减轻服务器端资源损耗,让它去做更应该做的事情。”就是你刚才一直提倡的客户端调用webservice,不过这个调用过程会让客户端和服务器端保持较长的链接状态,也就是你说的阻塞服务器,这种会不会影响客户端向服务器端的链接数量呢?不过确实把这个问题搞明白了,谢谢了呵呵,是给LZ解决问题的,现在到是我学了一把 看是看明白了 不过我这个webservice是在服务器端发短信的,客户端只是循环调用而已 难道要那暂停 开始的事件也要写成webservice? 还是把直接把循环放到服务器端 但这样webservice一下根本就接收不了这么多数据 短信群发一般都是一下10多万的 另外开个线成来出你的东西。你在界面线成上处理,一但操作的话,截面就回假死掉了。上边说的timer其实就是另开了一个现成的意思!!! 你可以把循环放在服务器端,但对于每个客户端的请求只在内存里保存状态,就是说发sms的线程你可以控制在一定数量,但客户端请求信息可以有无限个。不过我觉得你的客户端应该是一个监控形式的程序。可能就是一个?去控制和监控一下服务器发送sms的情况。那么这样你可能需要在服务器端保存当前发送的一个状态,让客户端能查到。 TreeNode如何存储键值 DataGridView中的Combobox列的问题 多级网络计划怎么实现呀?急~ VS2005+Access+VSS,在查询数据时候出现错 string str = null 与 string str = “” 一个下午都没弄出来,郁闷 帮看看一点代码 各位高手请教一个关于MDI的问题.. 未能找到类型或空间名称Label .aspx文件与对应的.aspx.cs文件是什么关系? winform窗中如何根据分辨率、窗体大小自动缩放窗体控件大小 C# 鼠标形状的问题
文章里面是点击一个button开始,另一个button停止。跟你的需求差不太多。你稍作修改就可以用。
if(this.timer1.Enabled==false)
{
this.button1.Text="暂停";
this.timer1.Enabled=true;
}
else
{
this.button1.Text="开始";
this.timer1.Enabled=false;
}
将它写到按钮中
Object thisLock = new Object();
lock (thisLock)
{
// Critical code section.
}
private Boolean flag= true;
private void updatelable()
{
while (flag)
{
i++;
Thread.Sleep(1000);
label1.Text = i.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
flag = true;
Thread th = new Thread(new ThreadStart(updatelable));
th.Start();
} private void button2_Click(object sender, EventArgs e)
{
flag = false;
}
兄弟我不才,乱搞了一个.你试下
可以尝试用AutoEventhanlder 只是建议...现在没空搞
delegate void LblMessage(string str);
private void Message(string str)
{
richTextBox1.Text = str.ToString();
}
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(Test));
th.Start();
}
private void Test()
{
for (int i = 0; i < 1000000; i++)
{
LblMessage lMessage = new LblMessage(Message);
this.Invoke(lMessage, i.ToString());
}
} private void button2_Click(object sender, EventArgs e)
{
if (th.ThreadState == ThreadState.Running)
{
th.Suspend();
}
else
{
th.Resume();
}
}
private bool m_bPause = false;for (int i = this.m_nBeginIndex; i < 1000000; i++)
{
this.m_nBeginIndex = i;
if(m_bPause )
break;
} 然后你在某处设置变量m_bPause 就OK了嘛
private bool m_bPause = false; int i = 0;
while(true)
{
if(m_bPause )
continue;
if(i>=1000000)
break; 你的代码.... this.m_nBeginIndex = i++;
} 这样就好了
bool threadSwitch = false;button_onclick()
{
if(button.Text.Equal("暂停"))
{
threadSwitch = true;
button.Text = "开始";
}
else
{
threadSwitch = false;
autoEvent.Set();
}
}for(i=0;i <10000;i++)
{
lable1.text=i.toString();
if(threadSwitch)
{
autoEvent.WaitOne();
}
}
bool threadSwitch = false; button_onclick()
{
if(button.Text.Equal("暂停"))
{
threadSwitch = true;
button.Text = "开始";
}
else
{
button.Text = "暂停";
threadSwitch = false;
autoEvent.Set();
}
}
for(i=0;i <10000;i++)
{
lable1.text=i.toString();
if(threadSwitch)
{
autoEvent.WaitOne();
}
}
红色的忘记加了。
2)单独用ResetEvent之类的互斥量似乎不可用,至少要个标志量在循环里检测和控制
3) 线程暂停的方案不推荐,.Net2.0开始就不推荐使用线程的Resume,Suspend等操作
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public sealed class SMSSender
{
bool _threadSwitch = false;
bool _pauseSwitch = false;
AutoResetEvent _resetEvent = new AutoResetEvent(false);
Thread _worker = null;
object _locker = new object(); public delegate void DoSomething(object sender, EventArgs e); public event DoSomething DoSomethingEvent; private void OnDoSomethingEvent()
{
try
{
if (DoSomethingEvent != null)
{
DoSomethingEvent(this, new EventArgs());
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void Start()
{
lock(_locker)
{
if (_worker == null)
{
_threadSwitch = true;
_worker = new Thread(Run);
_worker.IsBackground = true;
_worker.Start();
}
} } void Run()
{
while (_threadSwitch)
{
try
{
OnDoSomethingEvent();
if (_pauseSwitch)
{
_resetEvent.WaitOne();
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
Thread.Sleep(0);
}
} public void Continue()
{
lock (_locker)
{
if (!_pauseSwitch)
{
return;
}
_pauseSwitch = false;
_resetEvent.Set();
}
} public void Pause()
{
lock (_locker)
{
if (_pauseSwitch)
{
return;
}
_pauseSwitch = true;
}
} public void Stop()
{
lock (_locker)
{
if (_worker == null)
{
return;
}
_threadSwitch = false;
_pauseSwitch = false;
_resetEvent.Set();
if (_worker.IsAlive)
{
try
{
_worker.Abort();
}
catch
{
}
}
_worker = null;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private SMSSender smsSender = new SMSSender(); private void button1_Click(object sender, EventArgs e)
{
if (button1.Text.Equals("Pause"))
{
smsSender.Pause();
button1.Text = "Continue";
}
else
{
smsSender.Continue();
button1.Text = "Pause";
}
} private void Form1_Load(object sender, EventArgs e)
{
smsSender.DoSomethingEvent += new SMSSender.DoSomething(smsSender_DoSomethingEvent);
} void smsSender_DoSomethingEvent(object sender, EventArgs e)
{
MessageBox.Show("working");
Thread.Sleep(1000);
} private void button2_Click(object sender, EventArgs e)
{
button1.Text = "Pause";
smsSender.Start();
} private void button3_Click(object sender, EventArgs e)
{
smsSender.Stop();
}
}
}
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(107, 154);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Pause";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(109, 125);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Start";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(107, 183);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "Stop";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(107, 154);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Pause";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(109, 125);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Start";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(107, 183);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 2;
this.button3.Text = "Stop";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}这是我刚刚花20分钟写的,包括测试程序,我想大家不用在这里磨嘴皮子了,coding-testing-validating,这是程序员最应该做的事情了。已经通过测试,lz可以直接将代码copy过去用了,在dosomethingevent事件里写上你访问webservice的代码就可以了,如果你觉得dosomethingevent的名字不好听,可以改成Sending?
但可能需要用到WCF,设置wcf的访问模式为PerSession,
WCF可以维持Session状态,把SMSSender放到ServiceHost端,作为ServiceHost的私有属性,就像Form里一样,只是把button的事件改成Service的方法,然后在客户端调用Start,Pause,Continue,Stop就可以了
第二,如果是在服务器端做暂停控制,怎么可能给客户端睡了?是在服务器端阻塞,不是在客户端。并且对于SMSSender来说,别人操作的是它的变量,与它在Run里做什么事,做多久是隔离开来的,这就是多线程的好处,在它执行Run的时候,不管界面调用多少次continue、pause,它始终只认最后的状态。
第三,webservice应该是同步服务,在这里不应是异步服务,一个请求过来,完成请求,返回结果,同步的调用怎么可能它不知道返回结果给谁呢?
第四,至于服务器是不是被请求攻击,那是系统安全的问题。如果是正常的调用,怎么可能会是攻击呢?如果是非法调用,系统在安全机制上已经排除掉了。
第五,如果系统真的由于安全不到位被人攻击,即使不是http,就算是tcp,哪怕更底层的通讯,都会折磨死服务器端的。
“它的Continue和Pause等方法,就要暴露成webservice了,客户端在开始,继续和暂停按钮时就是直接调用这些webservice方法而已”
你的意思是把你写的客户端调用放到服务器端运行,在服务器端开很多线程,会耗资源,
“完全可以把这部分损耗放到客户端,减轻服务器端资源损耗,让它去做更应该做的事情。”
就是你刚才一直提倡的客户端调用webservice,不过这个调用过程会让客户端和服务器端保持较长的链接状态,也就是你说的阻塞服务器,这种会不会影响客户端向服务器端的链接数量呢?不过确实把这个问题搞明白了,谢谢了
呵呵,是给LZ解决问题的,现在到是我学了一把
不过我觉得你的客户端应该是一个监控形式的程序。可能就是一个?去控制和监控一下服务器发送sms的情况。
那么这样你可能需要在服务器端保存当前发送的一个状态,让客户端能查到。