我有以下代码将数据写入XML文件.
private void WriteResidentData()
{
int count = 1;
status = "Writing XML files";
foreach (Site site in sites)
{
try
{
//Create the XML file
StreamWriter writer = new StreamWriter(path + "\\sites\\" + site.title + ".xml");
writer.WriteLine("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>");
writer.WriteLine("<customer_list>");
foreach (Resident res in site.GetCustomers())
{
bw.ReportProgress((count / customers) * 100);
writer.WriteLine("\t<customer>");
writer.WriteLine("\t\t<customer_reference>" + res.reference + "</customer_reference>");
writer.WriteLine("\t\t<customer_name>" + res.name + "</customer_name>");
writer.WriteLine("\t\t<customer_address>" + res.address + "</customer_address>");
writer.WriteLine("\t\t<payment_method>" + res.method + "</payment_method>");
writer.WriteLine("\t\t<payment_cycle>" + res.cycle + "</payment_cycle>");
writer.WriteLine("\t\t<registered>" …Run Code Online (Sandbox Code Playgroud) 我正在WPF中创建一个备份实用程序,并且有一个关于线程的一般问题:
在backgroundWorker.DoWork()方法中,语句Message2.Text ="..."给出错误" 调用线程无法访问此对象,因为另一个线程拥有它. ".
我无法直接访问backgroundWorker.DoWork()中的UI线程,即在那时更改XAML TextBox中的文本?或者我是否需要将所有显示信息存储在内部变量中,然后将其显示在backgroundWorker.ProgressChanged()中,因为我必须使用例如percentageFinished?
XAML:
<Window x:Class="TestCopyFiles111.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="350" Width="525">
<DockPanel LastChildFill="True" HorizontalAlignment="Left" VerticalAlignment="Top"
Margin="10">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top">
<Button x:Name="Button_Start"
HorizontalAlignment="Left"
DockPanel.Dock="Top"
Content="Start Copying"
Click="Button_Start_Click"
Height="25"
Margin="0 0 5 0"
Width="200"/>
<Button x:Name="Button_Cancel"
HorizontalAlignment="Left"
DockPanel.Dock="Top"
Content="Cancel"
Click="Button_Cancel_Click"
Height="25"
Width="200"/>
</StackPanel>
<ProgressBar x:Name="ProgressBar"
DockPanel.Dock="Top"
HorizontalAlignment="Left"
Margin="0 10 0 0"
Height="23"
Width="405"
Minimum="0"
Maximum="100"
/>
<TextBlock DockPanel.Dock="Top" x:Name="Message" Margin="0 10 0 0"/>
<TextBlock DockPanel.Dock="Top" x:Name="CurrentFileCopying" Margin="0 10 …Run Code Online (Sandbox Code Playgroud) 我的问题如下:
我有一个窗体,我在其中放置了一个LayoutPanel,当窗体加载时,多个控件如:文本框和标签被添加到LayoutPanel.
然后在按钮上单击,我需要处理用户在这些动态创建的控件上输入的数据.对于那个purpouse我使用Backgroundworker,它应该采取这些控件并读取他们的数据.
我的问题是,Backgroundworker不允许我从DoWork方法访问控件,但我需要这样做,因为我将报告操作的进度.
以下是我的代码的一部分,以澄清这个概念:
private void frmMyForm_Load(object sender, EventArgs e)
{
//I add multiple controls, this one is just for example
LayoutPanel1.add(TextBox1);
....
}
private void bgwBackground_DoWork(object sender, DoWorkEventArgs e)
{
foreach (Control controlOut in LayoutPanel1.Controls)
{
//do some stuff, this one is just for example
string myString = controlOut.Name; //-> Here is the error, cant access controls from different Thread.
}
}
Run Code Online (Sandbox Code Playgroud)
使用委托来设置文本很简单,但如何让整个父控件来操作子控件(仅用于获取信息,我不想设置任何数据,只需要获取名称,文本,类似的东西) .
希望我明确表示,谢谢大家.
我的应用程序用于BackgroundWorker将文件上传到FTP服务器.一切正常,但似乎OnProgressChanged事件不应该像它应该的那样工作.
事件发生OnProgressChanged后我会完全完成RunWorkerCompleted,但事实并非如此.
在我的情况下,OnProgressChanged虽然RunWorkerComplete被触发,事件仍在触发.显然,我的进度条仍在继续,而我的文件已经完全发送到ftp服务器.
我在我的调试模式下进行了测试,我看到在RunWorkerCompletedFired 之后,OnPorgressChanged仍在工作.
我的代码在这里.
void FTP_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
try
{
string filename = e.Argument.ToString();
if (filename != string.Empty)
{
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
if (!IsFolderExist(_defaultDir))
{
MakeDefaultDir(_defaultDir);
}
reqFTP = GetRequest(this._host, this._port, GetDirName(_defaultDir) + "/" + fileInf.Name, this._user, this._pass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length; …Run Code Online (Sandbox Code Playgroud) 我有一个WCF服务,所有客户端都连接到该服务,以获取通知\提醒(使用他们实现的CALLBACK接口).目前,WCF服务是自托管的,但计划是将其托管在Windows服务中.
WCF服务具有"发布","订阅"和"取消订阅"操作.
我需要有一个后台工作线程,不断[每隔XXX分钟]轮询一个SQL服务器数据库表,并寻找某些'提醒'行.一旦找到它们 - 它应该通知所有连接的客户端.
我想到了实现这一目标的两种方法.
.
方法A:
有一个单独的EXE项目(不希望它是一个控制台,那应该是什么 - 一个Windows服务?),它将启动并运行一个后台线程.后台线程将作为其客户端之一连接到"Reminder"服务.后台线程将轮询数据库,一旦找到某些内容 - 它将向WCF服务发送'发布'消息,这将使WCF服务向所有订阅的客户端发送提醒.
.
方法B:
以某种方式使后台线程在 WCF服务项目中运行,并且当它在数据库中检测到新的提醒行时,以某种方式使其"发信号通知"具有该信息的WCF服务,然后WCF服务将该信息发送给所有订阅的客户端. .
.
哪种方法更好?还有其他建议吗?
我正在使用BackgroundWorker定期检查硬件开关.由于它通过慢速RS485网络连接,我必须延迟下一次状态更新.在开关状态更改时,我想更新OK/nOK图片框.这是在nOK pictureBox上实现的绿色OK图片框.这里没有真正的工作.
为了扩展性,我决定使用Backgroundworker.最后我想要一个隐藏的工人,这个
问题描述 启动BackgroundWorker后,它会按预期工作.然而,GUI冻结了.
我尝试了什么? 在MSDN的BackgroundWorker类注1 表示,该GUI应通过ProgressChanged更新.我尝试通过Worker_Switch.ReportProgress(fakeProgress ++)引发此事件并失败.PictureBox不再更新.
来自设计师的片段
this.Worker_Switch = new System.ComponentModel.BackgroundWorker();
//
// Worker_Switch
//
this.Worker_Switch.WorkerSupportsCancellation = true;
this.Worker_Switch.DoWork += new System.ComponentModel.DoWorkEventHandler(this.Worker_Switch_DoWork);
Run Code Online (Sandbox Code Playgroud)
主表格的片段
delegate void SetEventCallback(object sender, DoWorkEventArgs e); // Threadsafe calls for DoWork
private void btnBackgroundworker_Click(object sender, EventArgs e)
{
if (!Worker_Switch.IsBusy)
{
Worker_Switch.RunWorkerAsync();
}
}
private void Worker_Switch_DoWork(object sender, DoWorkEventArgs e)
{
// Worker Thread has no permission to change PictureBox "pictureBoxSwitchrightOK"
// Therefore this method calls itsself in the MainThread, …Run Code Online (Sandbox Code Playgroud) 我在C#(4.0)中有一个常规的Queue对象,我正在使用访问此Queue的BackgroundWorkers.
我使用的代码如下:
do
{
while (dataQueue.Peek() == null // nothing waiting yet
&& isBeingLoaded == true // and worker 1 still actively adding stuff
)
System.Threading.Thread.Sleep(100);
// otherwise ready to do something:
if (dataQueue.Peek() != null) // because maybe the queue is complete and also empty
{
string companyId = dataQueue.Dequeue();
processLists(companyId);
// use up the stuff here //
} // otherwise nothing was there yet, it will resolve on the next loop.
} while (isBeingLoaded == true // still …Run Code Online (Sandbox Code Playgroud) 我的应用程序使用后台工作程序在循环内做一些工作.我有它,所以在每次循环迭代时,它检查取消挂起是否为真,如果是,则打破循环.好的,我的应用程序一旦完成循环的当前迭代就会停止处理.问题是我认为后台工作程序仍然在运行 - 如果我单击按钮再次开始处理,我会收到一个错误,说后台工作人员正忙.
我打算处理这个工作人员,但是当表单运行时就会创建它,所以如果我处理它,那就不能再开始工作了.我真正想做的是告诉后台工作人员它是完整的,如果我点击"停止处理"按钮,那么当我点击开始按钮时它就可以再次开始处理了!
我打算尝试这个:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
while (!backgroundWorker1.CancellationPending)
{
// Start processing in a background thread so the GUI remains responsive,
// pass in the name of the text file produced by
// PFDR FilenameLogic(txtLetterType.Text);
}
}
Run Code Online (Sandbox Code Playgroud) 我对此有疑问BackgroundWorker.我可以在ProgressChanged没有启动线程的情况下调用该事件RunWorkerAsync.
我不明白为什么会这样.如果新线程还没有启动,它如何通知原始线程?
这似乎无论如何都有效,因为它更新GUI没有问题,在我实现之前不是这样的BackgroundWorker.
我有一个asp.net MVC站点,它有许多使用InstancePerHttpRequest范围注册的组件,但是我还有一个"后台任务",每隔几个小时运行一次,没有httpcontext.
我想得到一个我的IRepository的实例,它已经像这样注册了
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>))
.InstancePerHttpRequest();
Run Code Online (Sandbox Code Playgroud)
如何使用Autofac从非http上下文中执行此操作?我认为IRepository应该使用InstancePerLifetimeScope
backgroundworker ×10
c# ×7
.net ×3
progress-bar ×2
winforms ×2
asp.net ×1
autofac ×1
picturebox ×1
queue ×1
wcf ×1
wpf ×1
xaml ×1