我在这个有用的帖子中使用了一段简单的代码
它使用一个按钮和一个标签,标签应报告"10%完成"......"20%完成"......等等.
当我调试时,代码被点击,但我的标签没有在浏览器上更新.
我尝试使用和不使用更新面板,有和没有母版页.
protected void btnStartThread_Click(object sender, EventArgs e)
{
BackgroundWorker bw = new BackgroundWorker();
// this allows our worker to report progress during work
bw.WorkerReportsProgress = true;
// what to do in the background thread
bw.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs args)
{
BackgroundWorker b = o as BackgroundWorker;
// do some simple processing for 10 seconds
for (int i = 1; i <= 10; i++)
{
// report the progress in percent
b.ReportProgress(i * 10); …Run Code Online (Sandbox Code Playgroud) 我正在尝试每次Task.Factory.StartNew调用时创建一个新线程.问题是如何在不抛出异常的情况下运行代码:
static void Main(string[] args)
{
int firstThreadId = 0;
Task.Factory.StartNew(() => firstThreadId = Thread.CurrentThread.ManagedThreadId);
for (int i = 0; i < 100; i++)
{
Task.Factory.StartNew(() =>
{
while (true)
{
Thread.Sleep(1000);
if (firstThreadId == Thread.CurrentThread.ManagedThreadId)
throw new Exception("The first thread is reused.");
}
});
}
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
编辑:新代码,如果你评论第一个for语句没有问题.但是,如果你拥有它,WOW,会将"Thread reused"消息写入控制台.你能解释一下,因为我真的很困惑.
static void Main(string[] args)
{
ConcurrentDictionary<int, int> startedThreads = new ConcurrentDictionary<int, int>();
for (int i = 0; i < 10; i++)
{
Task.Factory.StartNew(() =>
{ …Run Code Online (Sandbox Code Playgroud) 我是第一次成立背景工作者.它主要是在代码运行时工作,我的停止/取消按钮正在工作.但是,我也在尝试报告更新进度条的进度,但我根本无法解决这个问题.
我从点击按钮启动代码,运行此代码:
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();//this invokes the DoWork event
Run Code Online (Sandbox Code Playgroud)
我的Do_Work方法:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
int j = 0;// Count cumulative imported files
int countDupFiles = 0;// Count number of previously imported csv files
int countImportedFiles = 0;// Count imported files
foreach (string folderPath in csvDirList)
{
string[] csvFileNames = Directory.GetFiles(@folderPath, "*.csv");
frmImportCsvData.replaceAll(csvFileNames, folderPath + "\\", "");
for (int i = 0; i < csvFileNames.Length; i++, j++)
{ …Run Code Online (Sandbox Code Playgroud) 我有一个带有文字的按钮,上面有音乐开/关.
使用以下Console.Beep()代码:
private void button1_Click(object sender, EventArgs e)
{
Console.Beep(659, 125);
Console.Beep(659, 125);
Thread.Sleep(125);
Console.Beep(659, 125);
Thread.Sleep(167);
Console.Beep(523, 125);
Console.Beep(659, 125);
Thread.Sleep(125);
Console.Beep(784, 125);
Thread.Sleep(375);
Console.Beep(392, 125);
Thread.Sleep(375);
Console.Beep(523, 125);
Thread.Sleep(250);
Console.Beep(392, 125);
Thread.Sleep(250);
Console.Beep(330, 125);
Thread.Sleep(250);
Console.Beep(440, 125);
Thread.Sleep(125);
Console.Beep(494, 125);
Thread.Sleep(125);
Console.Beep(466, 125);
Thread.Sleep(42);
Console.Beep(440, 125);
Thread.Sleep(125);
Console.Beep(392, 125);
Thread.Sleep(125);
Console.Beep(659, 125);
Thread.Sleep(125);
Console.Beep(784, 125);
Thread.Sleep(125);
Console.Beep(880, 125);
Thread.Sleep(125);
Console.Beep(698, 125);
Console.Beep(784, 125);
Thread.Sleep(125);
Console.Beep(659, 125);
Thread.Sleep(125);
Console.Beep(523, 125);
Thread.Sleep(125);
Console.Beep(587, 125);
Console.Beep(494, 125);
Thread.Sleep(125);
Console.Beep(523, …Run Code Online (Sandbox Code Playgroud) 假设我有这样的代码:
static void Main(string[] args)
{
var scheduler = NewThreadScheduler.Default;
var enumerable = Enumerable.Range(0, 100);
enumerable
.ToObservable(scheduler)
.SubscribeOn(scheduler)
.Subscribe(item =>
{
Console.WriteLine("Consuming {0} on Thread: {1}", item, Thread.CurrentThread.ManagedThreadId);
// simulate long running operation
Thread.Sleep(1000);
});
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
正如您一样,我将 IEnumerable 转换为 IObservable。然后我想在新线程上使用每个项目,所以我使用了 SubsribeOn(scheduler)。不幸的是,每次迭代都在同一个线程上工作,因此下一次迭代会阻塞。
结果是:
Consuming 0 on Thread: 4
Consuming 1 on Thread: 4
Consuming 2 on Thread: 4
Consuming 3 on Thread: 4
Consuming 4 on Thread: 4
....
Run Code Online (Sandbox Code Playgroud)
是否有可能强制这种行为?
我有网络表单应用程序.在一种形式上,我有一些功能.这被称为同步并需要一些时间.所以我需要在不同的线程中调用它们.
这是我正在做的样本:
protected void Page_Load(object sender, EventArgs e)
{
Thread t1 = new Thread(new ThreadStart(Function1));
t1.Start();
Thread t2 = new Thread(new ThreadStart(Function2));
t2.Start();
}
private void Function1()
{
Thread.Sleep(5000);
lbl1.Text = "Function1 completed";
}
private void Function2()
{
Thread.Sleep(5000);
lbl2.Text = "Function2 completed";
}
Run Code Online (Sandbox Code Playgroud)
如果我调试(设置breackpoints)lbl1.Text = "Function1 completed";并被lbl2.Text = "Function2 completed";调用,但最终的html页面上的文本没有变化.
页面加载也不需要5秒.
ps我知道asp网络有所不同,但我不知道我做错了什么.