本文所述为c#查找素数的程序代码,包括了可视化窗体的代码,找素数的方法可以借鉴。虽然实现的功能简单,不过为了演示一些c#技巧,本文实例中还用到了线程技术、listbox列表框的使用、设置程序挂起等操作,其中备有详尽的注释,帮助大家更好的理解。
具体实现代码如下:
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.threading; namespace suspendandresume { public class form1 : system.windows.forms.form { private system.windows.forms.label label1; private system.windows.forms.listbox listbox1; private system.windows.forms.button button1; private system.windows.forms.button button2; private system.windows.forms.button button3; private system.windows.forms.button button4; private system.windows.forms.label label2; private system.windows.forms.timer timer1; private system.componentmodel.icontainer components; //公共委托,用于输出素数 public delegate void ud(string returnval); //声明私有线程 private thread pnt; //用于标识是否挂起线程 bool suspend = false; //用于标识线程时候开始运行 bool pntstart = false; public form1() { initializecomponent(); // todo: 在 initializecomponent 调用后添加任何构造函数代码 } protected override void dispose( bool disposing ) { if( disposing ) { if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows 窗体设计器生成的代码 private void initializecomponent() { this.components = new system.componentmodel.container(); this.label1 = new system.windows.forms.label(); this.listbox1 = new system.windows.forms.listbox(); this.button1 = new system.windows.forms.button(); this.button2 = new system.windows.forms.button(); this.button3 = new system.windows.forms.button(); this.button4 = new system.windows.forms.button(); this.label2 = new system.windows.forms.label(); this.timer1 = new system.windows.forms.timer(this.components); this.suspendlayout(); // label1 this.label1.location = new system.drawing.point(8, 8); this.label1.name = "label1"; this.label1.tabindex = 0; this.label1.text = "已找到的素数:"; // listbox1 this.listbox1.itemheight = 12; this.listbox1.location = new system.drawing.point(8, 32); this.listbox1.multicolumn = true; this.listbox1.name = "listbox1"; this.listbox1.size = new system.drawing.size(272, 136); this.listbox1.tabindex = 1; // button1 this.button1.location = new system.drawing.point(19, 184); this.button1.name = "button1"; this.button1.size = new system.drawing.size(48, 23); this.button1.tabindex = 2; this.button1.text = "创建"; this.button1.click += new system.eventhandler(this.button1_click); // // button2 // this.button2.location = new system.drawing.point(88, 184); this.button2.name = "button2"; this.button2.size = new system.drawing.size(48, 23); this.button2.tabindex = 3; this.button2.text = "挂起"; this.button2.click += new system.eventhandler(this.button2_click); // // button3 // this.button3.location = new system.drawing.point(157, 184); this.button3.name = "button3"; this.button3.size = new system.drawing.size(48, 23); this.button3.tabindex = 4; this.button3.text = "恢复"; this.button3.click += new system.eventhandler(this.button3_click); // // button4 // this.button4.location = new system.drawing.point(226, 184); this.button4.name = "button4"; this.button4.size = new system.drawing.size(48, 23); this.button4.tabindex = 5; this.button4.text = "销毁"; this.button4.click += new system.eventhandler(this.button4_click); // // label2 // this.label2.location = new system.drawing.point(24, 224); this.label2.name = "label2"; this.label2.size = new system.drawing.size(200, 23); this.label2.tabindex = 6; this.label2.text = "线程未启动"; // // timer1 // this.timer1.enabled = true; this.timer1.tick += new system.eventhandler(this.timer1_tick); // // form1 // this.autoscalebasesize = new system.drawing.size(6, 14); this.clientsize = new system.drawing.size(292, 266); this.controls.add(this.label2); this.controls.add(this.button4); this.controls.add(this.button3); this.controls.add(this.button2); this.controls.add(this.button1); this.controls.add(this.listbox1); this.controls.add(this.label1); this.name = "form1"; this.text = "素数"; this.load += new system.eventhandler(this.form1_load); this.resumelayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [stathread] static void main() { application.run(new form1()); } private void button1_click(object sender, system.eventargs e) { //创建线程实例,设置属性 pnt = new thread(new threadstart(gpn)); pnt.name = "prime numbers exaple"; pnt.priority = threadpriority.belownormal; //设置按键,停用开始按键,启用挂起按键和销毁按键 button1.enabled = false; button2.enabled = true; button4.enabled = true; //线程开始,并设置标识 pnt.start(); pntstart = true; } private void button2_click(object sender, system.eventargs e) { //设置挂起bool变量为真 suspend = true; //设置按键,停用挂起按键, 启用恢复按键 button2.enabled = false; button3.enabled = true; } private void button3_click(object sender, system.eventargs e) { //设置挂起bool变量为假 suspend = false; //当线程当前状态为挂起时,恢复该线程 if(pnt.threadstate == system.threading.threadstate.suspended || pnt.threadstate == system.threading.threadstate.suspendrequested) { try { //恢复线程 pnt.resume(); //设置按键,停用恢复按键,启用挂起按键 button3.enabled = false; button2.enabled = true; } catch(threadstateexception ex) { messagebox.show(ex.tostring(), "提示"); } } } private void button4_click(object sender, system.eventargs e) { //设置按键,启用开始按键,停用其他按键 button1.enabled = true; button2.enabled = false; button3.enabled = false; button4.enabled = false; //销毁线程 pnt.abort(); } //gpn为getprimenumber的缩写,用于查找并显示素数 public void gpn() { //声明变量 long counter; //素数个数 long numbernow; //当前数 long sqrtofnow; //辅助数,做数组下标 bool isprime = false; //标识是否为素数 //数组,用于存储已找到素数 long[] primearray = new long[256]; //委托,用于显示素数,即将其添加到listbox中 string[] args = new string[] {"2"}; ud uidel = new ud(updateui); //初始化,从3开始计算,从第2个素数开始计算 numbernow = 3; counter = 2; //添加2为素数,放入素数数组并将其显示 primearray[1] = 2; this.invoke(uidel, args); //循环,用于找到并输出256个素数 while(counter <= 255) { isprime = true; //从1到当前数的平方根,穷尽计算是否为素数 for(sqrtofnow = 1; (primearray[sqrtofnow] * primearray[sqrtofnow] <= numbernow); sqrtofnow++) { //若能被已找到的素数整除,则不是素数 if(numbernow % primearray[sqrtofnow] == 0) { //若不是素数,跳出for循环 isprime = false; break; } } //若为素数,将其添加到listbox以显示 if(isprime) { //将素数存入数组中储存 primearray[counter] = numbernow; counter++; //将素数显示到listbox中 args[0] = numbernow.tostring(); this.invoke(uidel,args); //利用bool变量,控制是否挂起线程 if( suspend == true) { //调用suspend方法,并捕捉异常 try { pnt.suspend(); } catch(threadstateexception ex) { messagebox.show(ex.tostring(), "提示"); } } //使线程睡眠,使过程清楚显示,且有时间挂起线程 thread.sleep(500); } //除2外,素数必为奇数,故跳过偶数的检查,优化算法 numbernow += 2; } } //更新listbox的方法 void updateui(string result) { listbox1.items.add(result); } //利用timer控件显示线程当前状态 private void timer1_tick(object sender, system.eventargs e) { //在线程开时候再获取状态并显示 if( pntstart ) { label2.text = "线程当前状态是:" + pnt.threadstate.tostring(); } } private void form1_load(object sender, system.eventargs e) { //设置按键,启用开始按键,停用其他按键 button1.enabled = true; button2.enabled = false; button3.enabled = false; button4.enabled = false; } } }
感兴趣的读者可以动手调试一下该程序代码,相信会有一定的启发与借鉴价值。
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论