backgroundworker

InvalidAsynchronousStateException in function that checks if invoke is required for control

家住魔仙堡 提交于 2019-12-07 12:43:36
问题 I use this function courtesy of a Stack Overflow user to update controls from a BackgroundWorker . static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action) { // If the invoke is not required, then invoke here and get out. if (!sync.InvokeRequired) { // Execute action. action(); // Get out. return; } // Marshal to the required thread. sync.Invoke(action, new object[] { }); } This function has worked perfectly until now. I just got this exception: What does this mean and how do I

BackgroundWorker Return A Value?

不想你离开。 提交于 2019-12-07 12:39:01
问题 The following code adds the numbers from 1 to 100 and returns the sum. What I'm trying to do is run the calculations in a backgroundworker and return a value. The problem with this is that returnValue is returned before DoWork completes. How can I have it wait for my background worker to complete before returning a value? (I can't seem to put the return in my DoWork...) double returnValue = 0; var b = new BackgroundWorker(); b.DoWork += new DoWorkEventHandler( delegate(object sender,

In C#, Loading large file into winform richtextbox

放肆的年华 提交于 2019-12-07 11:40:53
问题 I need to load a - 10MB range text file into a Winform RichTextBox, but my current code is freezing up the UI. I tried making a background worker do the loading, but that doesnt seem to work too well either. Here's my several loading code which I was tried. Is there any way to improve its performance? Thanks. private BackgroundWorker bw1; private string[] lines; Action showMethod; private void button1_Click(object sender, EventArgs e) { bw1 = new BackgroundWorker(); bw1.DoWork += new

Is it safe to use C# global variables in a background worker thread

感情迁移 提交于 2019-12-07 09:42:44
问题 Hi I am working on a simple desktop application, it needs to handle some operations like loading a webpage which may block the main thread, so i moved the code to a background worker. My problem is there is a heavy class named UCSProject, which contains many string and List fields, i need to pass an instance of this class to the background worker, since the class is a bit heavy, i would like to reduce the number of duplicate instances by using the global variable directly, instead of passing

Run RIA service on non UI thread

白昼怎懂夜的黑 提交于 2019-12-07 09:29:46
问题 I am trying to make RIA service calls from non UI thread. I made calls with opening new thread and with background workers, but for both cases callback is running on UI thread. Is it possible to execute callback on callers thread, not UI? Thanks 回答1: tl;dr Use WCF Marshal your results to the UI thread yourself WCF RIA is built to do work on the UI thread WCF RIA was designed to work mostly on the UI thread. That obviously has many pros and cons; mostly cons in your case. I'm having trouble

Marquee ProgressBar unresponsive with BackgroundWorker

為{幸葍}努か 提交于 2019-12-07 09:13:52
问题 In my code, when a button is clicked the progress bar is set to marquee and then my BackgroundWorker is called but when the BackgroundWorker is called the progress bar freezes or disappears. I use the BackgroundWorker to seperate the RefreshReport method of the ReportViewer from the UI thread. Any help is appreciated. Thanks! Private Sub btnOtherReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOtherReport.Click rvReport.ProcessingMode = ProcessingMode.Remote

c# - Pass information to BackgroundWorker From UI during execution

和自甴很熟 提交于 2019-12-07 07:32:58
问题 I have a c# application that uses a background worker thread, and quite successfully updates the UI from the running thread. The application involves shortest path routing on a network, and I display the network and the shortest path, on the UI, as the background worker proceeds. I would like to allow the user to slow down the display through use of a slider, while the application is running. I found this as a suggestion, but it is in vb.net, I am not clear on how to get it to work in c#. How

How to immediately cancel a working BackgroundWorker?

核能气质少年 提交于 2019-12-07 04:01:08
问题 I have a time taking process in DoWork of my BackgroundWorker . Whenever I try to Cancel the job by backgroundWorker1.CancelAsync() , the backgroundWorker1.CancellationPending becomes Pending and I should wait for the next iteration in my DoWork to Cancel the job and stepping out of it myself. Is there any way to Cancel the job immediately after calling it? 回答1: No. In general you can't safely terminate a thread "immediately", because it may be holding resources that would leak, and more

I am trying to read the output of a process in c# but I get this message “Cannot mix synchronous and asynchronous operation on process stream.”

佐手、 提交于 2019-12-06 21:30:13
问题 I am writing a backup program using xcopy and because there are a lot of large files it takes a while so I want to show the progress. When I try to use StreamReader to get the standard output, it has this error message when I debug. "Cannot mix synchronous and asynchronous operation on process stream." public void backup_worker_DoWork(object sender, DoWorkEventArgs e) { int loop = 1; backup_worker.WorkerReportsProgress = true; Process xcopy = new Process(); ProcessStartInfo startinfo = new