但是第33行有错误,“字符初始值无法引用非静态字段、方法或属性SThread”。
求大神指教。怎么才能实现一个按钮控制另一个按钮对应事件。
--------------------编程问答--------------------
在线程的那个类中定义一个共有bool变量,
主程序改变它,线程每次循环都判断下,如果它被设置,就退出。
--------------------编程问答--------------------
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();
SThread.SetTextAction = SetText;
SThread.form = this;
isPause = true;
isStop = true;
}
private static Boolean isPause = true;
private static Boolean isStop = false;
private SubThread SThread = new SubThread();
private Thread thread;
public void SetText(String text)
{
this.Text = text;
}
public class SubThread
{
public Form form;
public Action<String> SetTextAction;
public void SubThreadStartFunction()
{
int i=0;
do
{
if (!isPause)
{
i++;
form.Invoke(SetTextAction, new Object[1] { i.ToString() });
}
} while (!isStop);
}
}
public static bool IsRunning = true;
public class SubThread
{
public void SubThreadFunc()
{
int i = 0;
while(Form1.IsRunning)
{
i++;
Console.WriteLine("子程序正在运行……{0}",i);
}
}
SubThread SThread = new SunThread();
Thread ThreadS = new Thread(new ThreadStart(SThread.SubThreadFunc));
}
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();
SThread.SetTextAction = SetText;
SThread.form = this;
isPause = true;
isStop = true;
}
private static Boolean isPause = true;
private static Boolean isStop = false;
private SubThread SThread = new SubThread();
private Thread thread;
public void SetText(String text)
{
this.Text = text;
}
public class SubThread
{
public Form form;
public Action<String> SetTextAction;
public void SubThreadStartFunction()
{
int i=0;
do
{
if (!isPause)
{
i++;
form.Invoke(SetTextAction, new Object[1] { i.ToString() });
}
} while (!isStop);
}
}
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();
SThread.SetTextAction = SetText;
SThread.form = this;
isPause = true;
isStop = true;
}
private static Boolean isPause = true;
private static Boolean isStop = false;
private SubThread SThread = new SubThread();
private Thread thread;
public void SetText(String text)
{
this.Text = text;
}
public class SubThread
{
public Form form;
public Action<String> SetTextAction;
public void SubThreadStartFunction()
{
int i=0;
do
{
if (!isPause)
{
i++;
form.Invoke(SetTextAction, new Object[1] { i.ToString() });
}
} while (!isStop);
}
}