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));
}
你不要在Invoke方法调用的委托所指代的函数中执行 Thread.Sleep(5000)。这相当于把窗口线程卡死了。(form.Invoke方法所在的 form的线程(主线程) 被委托SetTextAction 所指代的 SetText函数中的sleep语句给挂起了)
你可能需要这么改
if (!isPause)
{
i++;
form.Invoke(SetTextAction, new Object[1] { i.ToString() });
Thread.Sleep(5000);
}
这才是在新的线程中休眠,并定期通过Invoke方法访问窗口的标题属性报告进度。
#8
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);
}
}
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));
}
变量、字段和方法在初始化的时候不能用动态的变量初始化。你可以改为静态变量,或者改在类的构造函数中去初始化。
具体原理我也不是很清楚,大致是用变量初始化变量,很难检查异常,并可能产生相互依赖。
例如:
int a=b;
int b=a;
这种初始化没有任何意义,但从语法角度,如果允许使用变量初始化变量,则无法避免这种情况发生。
事实上,某些语言甚至不能完全地支持变量的初始化。比如VB6。
我上面的例子是在构造函数中完成初始化的。
参考:http://blog.csdn.net/yl_99/article/details/8472789。
#6
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);
}
}
“form.Invoke(SetTextAction, new Object[1] { i.ToString() });”这一句不知道是做什么用的呀?如果我的SetTextAction有两个参数,应该怎么写呢?
#7
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);
}
}
你不要在Invoke方法调用的委托所指代的函数中执行 Thread.Sleep(5000)。这相当于把窗口线程卡死了。(form.Invoke方法所在的 form的线程(主线程) 被委托SetTextAction 所指代的 SetText函数中的sleep语句给挂起了)
你可能需要这么改
if (!isPause)
{
i++;
form.Invoke(SetTextAction, new Object[1] { i.ToString() });
Thread.Sleep(5000);
}
这才是在新的线程中休眠,并定期通过Invoke方法访问窗口的标题属性报告进度。
#8
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);
}
}