我有一个带状态栏的WPF应用程序.
<StatusBar Grid.Row="1"
Height="23"
Name="StatusBar1"
VerticalAlignment="Bottom">
<TextBlock Name="TextBlockStatus" />
</StatusBar>
Run Code Online (Sandbox Code Playgroud)
我想在那里显示文本并在我做少量工作时切换到沙漏等待光标.
此代码将更新光标,但StatusBar 文本不会更新 ...
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow
Run Code Online (Sandbox Code Playgroud)
如果我这样做的话,它会起作用,但我对这个解决方案并不满意.有更简单的方法吗?
Delegate Sub Load1()
Sub Load2()
System.Threading.Thread.Sleep(New TimeSpan(0, 0, 3))
End Sub
Dim Load3 As Load1 = AddressOf Load2
Sub Load()
Cursor = Cursors.Wait
TextBlockStatus.Text = "Loading..."
Dispatcher.Invoke(DispatcherPriority.Background, Load3)
TextBlockStatus.Text = String.Empty
Cursor = Cursors.Arrow
End Sub
Run Code Online (Sandbox Code Playgroud)
我宁愿它看起来像这样......
Sub Load()
Cursor = …Run Code Online (Sandbox Code Playgroud) 假设我正在向用户显示一个表单,并使用BackgroundWorker在幕后进行一些工作.
当用户单击"确定"时,在BackgroundWorker完成之前,我无法继续.如果用户单击"确定"时尚未完成,我想显示WaitCursor,直到它有,然后继续.
实现这个的最佳方法是什么?
我知道我可以使用一个普通的老线程,然后做Thread.Join,但我喜欢BackgroundWorker.
可以做得很好吗?
有没有人尝试使用TwainDotNet扫描来自.NET的TWAIN API调用?虽然它运行良好,但是当与使用MVVM的WPF应用程序一起使用时,我遇到了一些问题.基本上我是从服务调用Twain扫描功能,而Service又使用BackgroundWorker.
List<BitmapSource> bitmapSources = new List<BitmapSource>();
Twain twain = new Twain(new WpfWindowMessageHook(_window));
ScanSettings settings = new ScanSettings() { ShowTwainUI = false };
using (BackgroundWorker worker = new BackgroundWorker())
{
worker.DoWork += (sndr, evnt) =>
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
EventHandler scanCompleteHandler = (se, ev) => { waitHandle.Set(); };
twain.ScanningComplete += scanCompleteHandler;
twain.StartScanning(settings);
waitHandle.WaitOne();
if (twain.Images.Count > 0)
{
foreach (var image in twain.Images)
{
BitmapSource bitmapSource = Imaging.CreateBitmapSourceFromHBitmap(new Bitmap(image).GetHbitmap(),
IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
bitmapSources.Add(bitmapSource);
}
}
}; …Run Code Online (Sandbox Code Playgroud) 我对混合事件和线程知之甚少.场景是在PC上运行C#程序,在PLC上运行Twincat.我们需要在C#程序中访问PLC变量(已经完成了没有后台工作线程并且工作正常.)现在我们需要将这些处理移动到一个线程(最好是后台工作者).这是不起作用的代码.(表单包含一个START按钮,它将启动BGworker,一个STOP按钮将取消BGWorker,以及一个UPDATE按钮,它将值从PLC更新到文本框.),但现在tcClient_OnNotification没有被召唤!请指出我错过的地方,任何帮助将非常感谢.
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; // not added by default
using System.IO; // not added by default
using TwinCAT.Ads; // not added by default
namespace BGworker
{
public partial class Form1 : Form
{
private BackgroundWorker bw = new BackgroundWorker();
private TcAdsClient tcClient; // C# program is the client.
private AdsStream dataStream; // Data transfered through System IOStream
private BinaryReader binReader; // We are now reading …Run Code Online (Sandbox Code Playgroud) 我需要在readstream函数中使用comboBox1.text和comboBox2.Text等表单控件,这会传递一条错误消息(翻译自德语):
控制元素comboBox1/comboBox2的访问来自另一个线程,而不是创建它的线程!
我需要的是将这些控件传递给readstream函数,但我不知道究竟是怎么回事.
private void button1_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker;
worker.WorkerReportsProgress = true;
worker.ProgressChanged += ProgressChanged;
worker.DoWork += ReadStream;
//Need to pass the comoBox Texts from here!!!
string start = comboBox1.Text;
string end = comboBox2.Text;
worker.RunWorkerAsync();
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
UpdateProgressBar(e.ProgressPercentage);
comboBox1.Text = e.UserState.ToString();
}
private void ReadStream(object sender, DoWorkEventArgs doWorkEventArgs)
{
BackgroundWorker worker = sender as BackgroundWorker;
string line;
//And use the values here !!!!
using (StreamReader sr = …Run Code Online (Sandbox Code Playgroud) 我有Mono的困境.我试图了解BackgroundWorker实例不会同时运行的方式或原因,或者至少不能非常有效或快速地运行实例.这几乎就像线程的数量受到限制一样,但我不是Mono开发人员或.NET开发人员,所以我把自己置于我的兄弟们的怜悯之下.
请考虑以下示例:
using System;
using System.ComponentModel;
using System.Threading;
namespace backgroundworkertest
{
class MainClass
{
public static void foo(object sender, DoWorkEventArgs e) {
Console.WriteLine("Start");
Thread.Sleep(500);
Console.WriteLine("Done");
}
public static void Main (string[] args)
{
for(int i = 0; i < 6; i++) {
BackgroundWorker b = new BackgroundWorker();
b.WorkerReportsProgress = false;
b.DoWork += new DoWorkEventHandler(foo);
b.RunWorkerAsync();
}
Thread.Sleep(2000);
Console.WriteLine("Really done");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的内容时,我得到以下输出.
Start Start Done Start Start Done Start Done Really Done
首先,并非所有的BackgroundWorkers都在两秒内完成.从循环中可以清楚地看出我正在实例化6名工人,但只有3名人员打印"完成".如果所有6个人同时运行,我觉得两秒钟应该足够让这些工人完成.
真正的内幕故事是我有一个运行良好的应用程序(使用大量的BackgroundWorker …
由于ProgressChanged事件处理程序是从事件处理程序中的某个地方引发的DoWork,因此它们不应该在异步操作线程DoWork上调用,而线程也在运行,而不是UI线程,因此需要调用或BeginInvoke操作控件?
我的猜测是在ReportProgress方法中发生了一些魔法,但它怎么知道,哪一个是调用ProgressChanged事件处理程序的正确线程?
在我的Windows窗体应用程序中,我有一个textbox和backgroundworker组件.在dowork的情况下,backgroundworker我试图访问文本框的值.我怎样才能做到这一点?当我尝试访问文本框的值时,我在dowork事件处理程序代码中遇到异常:
Cross-thread operation not valid: Control 'txtFolderName' accessed from a thread other than the thread it was created on`
Run Code Online (Sandbox Code Playgroud) 目前正在使用sidekiq运行我的后台工作.同时我一次运行10个工作岗位.这些工作不是一个小工作.完成工作需要6-8个小时.
与此同时,我需要运行一些较小的工作(需要2-3分钟).目前,在完成上述10个工作中的任何一个工作之前,我无法运行这些工作.所以我需要等待6-8个小时来完成这个小工作.
但是这些作业应该在添加到队列后立即运行.是否有任何选项可以阻止任何一个进程运行这种小型作业.我尝试了队列选项但它在我的场景中也不起作用.
这是我的sidekiq配置
:concurrency: 10
:queues:
- [web, 7]
- [default, 3]
Run Code Online (Sandbox Code Playgroud)
任何人都可以给我这个问题的解决方案吗?
想象一下,我有这样的代码:
private bool flag = false;
public SomeClass()
{
public void setup()
{
worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
if(!worker.IsBusy)
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
if(flag == true)
return;
//whatever You want the background thread to do...
SomeClass.SomeStaticMethodWhichHasLoopInside();
}
}
Run Code Online (Sandbox Code Playgroud)
当用户单击我的应用程序中的"退出"按钮时,我可以设置flag=true内部 - 这是停止线程启动的正确方法BackgroundWorker吗?我想只在应用程序想要退出时才这样做 - 这样我就停止了线程.或者会被自动杀死?