backgroundworker

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

拜拜、爱过 提交于 2019-12-05 16:40:47
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 it as an argument to the background worker. To make it short, I just want to know is it safe to access

Using Console.WriteLine in a BackgroundWorker doWork event

旧街凉风 提交于 2019-12-05 13:20:39
I have a console application that utilizes the BackgroundWorker object and wanted to test my output with a Console.WriteLine(fooBar). However, that application exits as the application executes the Console.WriteLine command. Here's a watered down version that illustrates what I wanted to do: protected static void bWorker_DoWork(object sender, DoWorkEventArgs e) { for (int i = startId; i <= endId; i++) { Console.WriteLine(i.ToString()); Thread.Sleep(1000); } } Any ideas why the application would appear to exit like that? For your backgroundworker set WorkerReportsProgress to true . Subscribe to

c# - Pass information to BackgroundWorker From UI during execution

孤人 提交于 2019-12-05 12:45:36
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 can the BackgroundWorker get values from the UI thread while it is running? I can pass the value of

Fill dataGridView thank's to backGroundWorker

≯℡__Kan透↙ 提交于 2019-12-05 10:23:16
I have this code snippet: private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { remplirDataGrid(); } private void frmChercherActesLoad(object sender, EventArgs e) { backgroundWorker1.RunWorkerAsync(); } private void remplirDataGrid() { dataGridView1.DataSource = ActeServices.getAllActes(0, 40); dataGridView1.Columns[0].Visible = false; dataGridView1.Columns[1].HeaderText = "Code acte"; dataGridView1.Columns[2].HeaderText = "Désignation"; dataGridView1.Columns[3].HeaderText = "Pris en charge"; dataGridView1.Columns[4].HeaderText = "Id article";

Convert Ping application to multithreaded version to increase speed - C#

南笙酒味 提交于 2019-12-05 08:23:31
I have an application that pings every possible IP on your local subnet in order to compile a list of responsive IP addresses. Currently it pings all 255 one at a time. Is it possible to convert this app to use multiple threads to increase the speed by pinging more than one at a time? I am new to the concept of multiple threads and figure this would be a good way to learn(as long as it's possible of course). also, any stylistic improvements you can educate me on would also be helpful. thanks ahead of time Here is the current pinging method in a backgroundWorker1_DoWork event. private void

Background Worker loading screen in Winforms

蹲街弑〆低调 提交于 2019-12-05 08:00:55
问题 I have a Form where one can Sign In and it could take a while till the data gets loaded into the Form. So I wanted to create a seperate Form (loadScreen.cs) with a Progress Bar when the Form is loading. I tried this in the loadScreen.cs Form: private void Form1_Load(object sender, EventArgs e) { worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.ProgressChanged += new

How to immediately cancel a working BackgroundWorker?

走远了吗. 提交于 2019-12-05 07:12:34
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? No. In general you can't safely terminate a thread "immediately", because it may be holding resources that would leak, and more importantly it may be holding locks. You need to structure your worker to respect the cancellation flag and and

C# background worker not triggering dowork event on any pc besides my own

微笑、不失礼 提交于 2019-12-05 06:18:37
I'm having a really weird issue with a background worker in one of my applications. I recently overhauled it slightly with some GUI fixes but now when someone on another PC runs the .exe or installs my OneClick deployment the background worker doesn't enter the dowork event. The logic has not changed between the 2 releases. I've done a compare and besides adding more error logging nothing has changed. I've gone as far as to include message boxes and breakpoints in the dowork event but it never enters it anywhere besides on my own PC. Even when i remote debug it doesnt enter the DoWork event

Updating the GUI from background worker

こ雲淡風輕ζ 提交于 2019-12-05 06:08:18
The name of the question is: " Updating the GUI from background worker ", but the correct name world be: " Updating the GUI from background worker OR reporting multiple-variables (other than an integer) from background worker " Please let me explain my situation. In a program I have a background worker which analyses the information.As the result of this analysis - form GUI elements should be populated with necessary data. In GUI I would like to update 2 datagridviews 1 listbox 5 labels As I understand - I can only natively report 1 int value via ReportProgress() method of background worker.

Is BackgroundWorker's IsBusy same as “IsAlive”?

谁都会走 提交于 2019-12-05 04:51:55
I am trying to figure out a way to verify that a BackgroundWorker thread is alive (i.e. still running. The thread is essentially implemented as a simple infinite loop: while (AllConditionsMet()) { DoSomeMagic(); Thread.Sleep(10000); } The closest thing to IsAlive() I found so far is the IsBusy property, but given that my thread Sleep()s most of the time, I am not sure whether this will do the job. Can I count on IsBusy to do: if (!myWorker.IsBusy) RestartWorker(); or am I calling for trouble? Hans Passant BackgroundWorker.IsBusy is true as long as the DoWork event handler is busy and the