Search Results

Search found 190 results on 8 pages for 'backgroundworker'.

Page 3/8 | < Previous Page | 1 2 3 4 5 6 7 8  | Next Page >

  • Throwing an exception from a BackgroundWorker which calls an async. method (Webrequest)

    - by mrbamboo
    Hi, My main application creates a new BackgroundWorker X the DoWork event handler of X calls a method Y of my controller. This method creates the WebRequest (async.) instance and the callback using AsyncCallback. When the response arrives the callback method Z gets called and the content will be analyzed. It can happen that the response has an unwanted content. At that moment callback Z will throw an exception. I want to catch this exception in my main application. I tried it in DoWork and RunWorkerCompleted but nothing can be caught from there. Error in RunWorkerCompletedEventArgs is always null.

    Read the article

  • How to implement progress bar and backgroundworker for database calls C#?

    - by go-goo-go
    How to implement progress bar and backgroundworker for database calls C#? I do have some methods that deal with large amounts of data. They do last a lot, so in my windows application, i wanna do something to tell users that the data are being processed. So i thought of using progress bar or status strip label, but since there is a single ui thread, the thread where the database-dealing methods are executed, ui controls are not updated, so progress bar or status strip label are useless to me. I've already seen some examples, but they deal with for-loops, ex: for(int i = 0; i < count; i++){ System.Threading.Thread.Sleep(70); // ... do analysis ... bgWorker.ReportProgress((100 * i) / count);} private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e){ progressBar.Value = Math.Min(e.ProgressPercentage, 100);} Can anybody give an example where I can use a method call, not a for-loop, and let the progress bar run while this method is executing??? thnx in advance, any help and hint is highly appreciated.

    Read the article

  • When Something Occurs in a BackgroundWorker, Trigger Code on a Different Thread?

    - by Soo
    I have a background worker that runs and looks for stuff, and when it finds stuff, I want to update my main WinForm. The issue that I'm having is that when I try to update my WinForm from my background worker, I get errors that tell me I can't modify things that were made outside of my background worker (in other words, everything in my form). Can someone provide a simple code example of how I can get my code to work the way I want it to? Thanks!

    Read the article

  • Updatating DataGrid From a BackGroundWorker

    - by subbu
    I hava a Background Worker and a DataGrid in my c# Application. In do work of my Backgroundworker which will call an Api in my dlls which will enter some Data in a SQLite Database. After the Completion of my Api call I report a progress and In progress event of my Backgroundworker I get the contents from Db and assign it as a DataSource to my grid. I call same API in same backgroundworker. In the middle of processing my application crashes. But If I dont assign the dataSource in ProgressChanged my application doesnt crashes.

    Read the article

  • BackgroundWorker and instance variables

    - by Alastair Pitts
    One thing that's always confused me is how a BackgroundWorker seems to have thread-safe access to the instance variables of the surrounding class. Given a basic class: public class BackgroundProcessor { public List<int> Items { get; private set; } public BackgroundProcessor(IEnumerable<int> items) { Items = new List<int>(items); } public void DoWork() { BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerAsync(); } void worker_DoWork(object sender, DoWorkEventArgs e) { var processor = new ProcessingClass(); processor.Process(this.Points); //Accessing the instance variable } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { //Stuff goes here } } Am I erroneous in my assumption the the call to processor.Process(this.Points); is a thread-safe call? How don't I get a cross-thread access violation? I'm sure it's obvious, but it always has confused me.

    Read the article

  • Spawning BackgroundWorkers

    - by washtik
    We have a business case that would be perfect for multiple BackgroundWorkers. As an example, we have a form with a "Save" button on it. Normally we would run all the save commands (Save is an example) synchronously and then close the form. We would like to now split the work onto separate threads using backgroundworker. We will loop through each "Save" required (could be many and/or different number of commands that need executing) creating a BackgroundWorker for each command required. The question is ... how do we wait for ALL the BackgroundWorkers to complete before we close the form. We understand how to wait for a single BackgroundWorker to complete but when we have X number of BackgroundWorkers operating, how do we wait until all are complete before closing the UI form?

    Read the article

  • Best ASP.NET Background Service Implementation

    - by Jason N. Gaylord
    What's the best implementation for more than one background service in an ASP.NET application? Timer Callback Timer timer = new Timer(new TimerCallback(MyWorkCallback), HttpContext, 5000, 5000); Thread or ThreadPool Thread thread = new Thread(Work); thread.IsBackground = true; thread.Start(); BackgroundWorker BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(DoMyWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DoMyWork_Completed); worker.RunWorkerAsync(); Caching like http://www.codeproject.com/KB/aspnet/ASPNETService.aspx (located in Jeff Atwood's post here) I need to run multiple background "services" at a given time. One service may run every 5 minutes where another may be once a day. It will never be more than 10 services running at a time.

    Read the article

  • How do I best write my own background-working and communicatible (sending progress updates and getti

    - by Ivan
    I have to develop a class which can run it's own hard-coded task in a background thread and communicate with container class sending him progress updates and taking messages from it. I believe I am going to extend BackgroundWorker class but it looks a bit weird to extend and I haven't managed to find a good example wit Google. Can you share a link to a good example of solving such a task? Please don't offer placing a BackgroundWorker visually on a form and reading the standard tutorial, it's not for my case. Maybe I'd better not extend but encapsulate a BackgroundWorker, but I hardly understand how to use it outide WinForms designer either :-(

    Read the article

  • How to dispose BackgroundWorkers the right way

    - by radbyx
    I've got a Windows Service that runs BackgroundWorker's, and I'm wondering if I'm doing the right thing when I stop my Windows Service. Is it enough to: Let the BackgroundWorker1_DoWork method complete (I have a while loop in it now, doing some tasks) Set the variable that holds the reference to the BackgroundWorker, to null Is there some kind of Dispose() method I need to call (In the same way the Timer class has Timer.Dispose();)?

    Read the article

  • Keeping the UI responsive while parsing a very large logfile

    - by Carlos
    I'm writing an app that parses a very large logfile, so that the user can see the contents in a treeview format. I've used a BackGroundWorker to read the file, and as it parses each message, I use a BeginInvoke to get the GUI thread to add a node to my treeview. Unfortunately, there's two issues: The treeview is unresponsive to clicks or scrolls while the file is being parsed. I would like users to be able to examine (ie expand) nodes while the file is parsing, so that they don't have to wait for the whole file to finish parsing. The treeview flickers each time a new node is added. Here's the code inside the form: private void btnChangeDir_Click(object sender, EventArgs e) { OpenFileDialog browser = new OpenFileDialog(); if (browser.ShowDialog() == DialogResult.OK) { tbSearchDir.Text = browser.FileName; BackgroundWorker bgw = new BackgroundWorker(); bgw.DoWork += (ob, evArgs) => ParseFile(tbSearchDir.Text); bgw.RunWorkerAsync(); } } private void ParseFile(string inputfile) { FileStream logFileStream = new FileStream(inputfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader LogsFile = new StreamReader(logFileStream); while (!LogsFile.EndOfStream) { string Msgtxt = LogsFile.ReadLine(); Message msg = new Message(Msgtxt.Substring(26)); //Reads the text into a class with appropriate members AddTreeViewNode(msg); } } private void AddTreeViewNode(Message msg) { TreeNode newNode = new TreeNode(msg.SeqNum); BeginInvoke(new Action(() => { treeView1.BeginUpdate(); treeView1.Nodes.Add(newNode); treeView1.EndUpdate(); Refresh(); } )); } What needs to be changed?

    Read the article

  • Web service performing asynchronous call

    - by kornelijepetak
    I have a webservice method FetchNumber() that fetches a number from a database and then returns it to the caller. But just before it returns the number to the caller, it needs to send this number to another service so instantiates and runs the BackgroundWorker whose job is to send this number to another service. public class FetchingNumberService : System.Web.Services.WebService { [WebMethod] public int FetchNumber() { int value = Database.GetNumber(); AsyncUpdateNumber async = new AsyncUpdateNumber(value); return value; } } public class AsyncUpdateNumber { public AsyncUpdateNumber(int number) { sendingNumber = number; worker = new BackgroundWorker(); worker.DoWork += asynchronousCall; worker.RunWorkerAsync(); } private void asynchronousCall(object sender, DoWorkEventArgs e) { // Sending a number to a service (which is Synchronous) here } private int sendingNumber; private BackgroundWorker worker; } I don't want to block the web service (FetchNumber()) while sending this number to another service, because it can take a long time and the caller does not care about sending the number to another service. Caller expects this to return as soon as possible. FetchNumber() makes the background worker and runs it, then finishes (while worker is running in the background thread). I don't need any progress report or return value from the background worker. It's more of a fire-and-forget concept. My question is this. Since the web service object is instantiated per method call, what happens when the called method (FetchNumber() in this case) is finished, while the background worker it instatiated and ran is still running? What happens to the background thread? When does GC collect the service object? Does this prevent the background thread from executing correctly to the end? Are there any other side-effects on the background thread? Thanks for any input.

    Read the article

  • Whats wrong with my backgroundwork method

    - by diver-d
    I am trying to get a background worker process working in a wpf application. it creates 2 files then crashes. BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += delegate(object s, DoWorkEventArgs args) { CreateFile(i.ToString()); }; worker.RunWorkerAsync(); private void CreateFile(string fileName) { string path = string.Format(@"{0}\{1}.txt", directory, fileName); using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine(fileName); } } I get this error " The requested operation cannot be performed on a file with a user-mapped section open." what am I doing wrong? Any help would be great

    Read the article

  • Thread Question

    - by Polaris
    I have method which create background thread to make some action. In this background thread I create object. But this object while creating in runtime give me an exception : The calling thread must be STA, because many UI components require this. I know that I must use Dispatcher to make reflect something to UI. But in this case I just create an object and dont iteract with UI. This is my code: public void SomeMethod() { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(Background_Method); worker.RunWorkerAsync(); } void Background_Method(object sender, DoWorkEventArgs e) { TreeView tv = new TreeView(); } How can I create objects in background thread? I use WPF application

    Read the article

  • WPF BackgroundWorker Execution

    - by Sanju
    Hi All, I've been programming C# for a while now (I'm a Computer Science major), and have never had to implement threading. I am currently building an application for a client that requires threading for a series of operations. Due to the nature of the client/provider agreement, I cannot release the source I am working with. The code that I have posted is in a general form. Pretty much the basic idea of what I am implementing, excluding the content specific source. The first Snippet demonstrates my basic structure, The Progress class is a custom progress window that is displayed as a dialog to prevent user UI interaction. I am currently using the code to complete database calls based on a collection of objects in another area of the application code. For example, I have a collection of "Objects" of one of my custom classes, that I perform these database calls on or on behalf of. My current set up works just fine when I call the "GeneralizedFunction" one and only one time. What I need to do is call this function once for every object in the collection. I was attempting to use a foreach loop to iterate through the collection, then I tried a for loop. For both loops, the result was the same. The Async operation performs exactly as desired for the first item in the collection. This, however, is the only one it works for. For each subsequent item, the progress box (my custom window) displays and immediately closes. In terms of the database calls, the calls for the first item in the collection are the only ones that successfully complete. All others aren't attempted. I've tried everything that I know and don't know to do, but I just cannot seem to get it to work. How can I get this to work for my entire collection? Any and all help is very greatly appreciated. Progress progress; BackgroundWorker worker; // Cancel the current process private void CancelProcess(object sender, EventArgs e) { worker.CancelAsync(); } // The main process ... "Generalized" private void GeneralizedFunction() { progress = new Progress(); progress.Cancel += CancelProcess; progress.Owner = this; Dispatcher pDispatcher = progress.Dispatcher; worker = new BackgroundWorker(); worker.WorkerSupportsCancellation = true; object[] workArgs = { arg1, arg2, arg3}; worker.DoWork += delegate(object s, DoWorkEventArgs args) { /* All main logic here */ foreach(Class ClassObject in ClassObjectCollection) { //Some more logic here UpdateProgressDelegate update = new UpdateProgressDelegate(updateProgress); pDispatcher.BeginInvoke(update, arg1,arg2,arg3); Thread.Sleep(1000); } }; worker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args) { progress.Close(); }; worker.RunWorkerAsync(workArgs); progress.ShowDialog(); } public delegate void UpdateProgressDelegate(arg1,arg2,arg3); private void updateProgress(arg1,arg2,arg3) { //Update progress }

    Read the article

  • c# opennetCF background worker stops after 100 iterations

    - by ikky
    Hi. I have a background worker that stops after 100 iterations. Like this: BackgroundWorker bgWorker = new BackgroundWorker(); bgWorker.WorkerReportsProgress = true; bgWorker.WorkerSupportsCancellation = true; bgWorker.DoWork += new OpenNETCF.ComponentModel.DoWorkEventHandler(this.bgWorker_DoWork); bgWorker.RunWorkerCompleted += new OpenNETCF.ComponentModel.RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted); bgWorker.ProgressChanged += new OpenNETCF.ComponentModel.ProgressChangedEventHandler(this.bgWorker_ProgressChanged); private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { for(i=0; i<300; i++) { bgWorker.ReportProgress(i, i); } } private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.labelProgress.Text = e.UserState.ToString(); } private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("finished loading..."); } What happens is that the labelProgress' value stops at 100, and the messagebox pops up and says "finished loading...". Anybody have an idea of what is wrong. Why does the thread stop at the 101 iteration? Thanks in advance.

    Read the article

  • C# Help with a basic pedagogic example of a BackGroundWorker process populating a DataGridView

    - by Roger
    Scenario: I have a windows form that holds a DataGridWiew with 3 pre-defined columns. I have 3 variables declared outside the function and assigned to inside the function. I have a function that enumerates stuff and puts it in the 3 columns, line by line: string VARIABLE1; string VARIABLE2; string VARIABLE3; private void FunctionEnumerateStuff() { foreach (StuffObject STUFF in StuffCollection) { VARIABLE1 = STUFF.SubStuff1.ToString(); VARIABLE2 = STUFF.SubStuff2.ToString(); VARIABLE3 = STUFF.SubStuff3.ToString(); DatagridWiew1.Rows.Add(VALUE1, VALUE2, VALUE3); } } What I want to do, is to execute this function from a BackGroundWorker process, so that the GUI of the application will be smooth and responsive. I have read up on backgroundworkers but I am having trouble relating, because all examples seems to be of entirely different scenarios and most of them are overwelmingly complex. San some helpful pedagogic soul help me and others with a very basic example of how to get this to work in the simplest way possible. Thanks.

    Read the article

  • Rate my C# code (~300 SLOC) using GDI+/Backgroundworker

    - by sebastianlarsson
    Hi, I want to get some feedback on my code! Below is some background info. I am taking a pre-certification course in C# (Sweden, 15 ECTS). The focus of the course is theoretical and only limited practical work. I dont find the assignments very hard at all to tell you the truth, but since I only have very limited work experience as a developer (I have worked 15h/week at Ericsson since November) I think I would benefit from having the certificate (70-536 and more probably). I am currently reading Martin Fowler's "Refactoring: Improving the design of existing code" and I tried to apply his techniques to my latest lab in the course. I have been on the lookout for a website which have the idea of providing feedback on code, but so far I have yet to discover any. Please take a look on my code and tell me what you think. It is only roughly 300 lines of code divided into a couple of classes. GDI+, backgroundworker and user controls are what the lab is about. I reckon you may have to spend as little as a couple of minutes on looking on the solution. Link to solution: http://www.filefactory.com/file/b18h7d5/n/Lab4_Lab5_SebastianLarsson.zip Regards and thank you, Sebastian

    Read the article

  • Do you know of a good F# and C# interop example available?

    - by Ivan
    I am going to write a C# WinForms application which will run a long data-crunching task in a BackgroundWorker, show progress in a ProgressBar and have buttons to start, pause, resume and cancel the operation. I'd like to write the calculation in F#. Do you know of any good examples or readings available in the Web which can help me?

    Read the article

  • WPF application in a one window

    - by lukas
    I would like to make an application in a one window using XAML. It should be like a slideshow with next and back button. One idea is to make 4 panels and have just one enable at the time. Is there any other way to do this? Like dynamic loading of other XAML? is it the BackgroundWorker mandatory to use with WPF (hence is DirectX rendered there's almost no GUI lags) ?

    Read the article

  • Do you know of a good F#&C# interop example available?

    - by Ivan
    I am going to write a C# WinForms application which will run a long data-crunching task in a BackgroundWorker, show progress in a ProgressBar and have buttons to start, pause, resume and cancel the operation. I'd like to write the calculation in F#. Do you know of any good examples or readings available in the Web which can help me?

    Read the article

  • c# opennetCF background worker - e.result gives a ObjectDisposedException

    - by ikky
    Hi! I'm new working with background worker in C#. Here is a class, and under it, you will find the instansiation of it, and under there i will define my problem for you: I have the class Drawing: class Drawing { BackgroundWorker bgWorker; ProgressBar progressBar; Panel panelHolder; public Drawing(ref ProgressBar pgbar, ref Panel panelBig) // Progressbar and panelBig as reference { this.panelHolder = panelBig; this.progressBar = pgbar; bgWorker = new BackgroundWorker(); bgWorker.WorkerReportsProgress = true; bgWorker.WorkerSupportsCancellation = true; bgWorker.DoWork += new OpenNETCF.ComponentModel.DoWorkEventHandler(this.bgWorker_DoWork); bgWorker.RunWorkerCompleted += new OpenNETCF.ComponentModel.RunWorkerCompletedEventHandler(this.bgWorker_RunWorkerCompleted); bgWorker.ProgressChanged += new OpenNETCF.ComponentModel.ProgressChangedEventHandler(this.bgWorker_ProgressChanged); } public void createDrawing() { bgWorker.RunWorkerAsync(); } private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { Panel panelContainer = new Panel(); // Adding panels to the panelContainer for(i=0; i<100; i++) { Panel panelSubpanel = new Panel(); // Setting size, color, name etc.... panelContainer.Controls.Add(panelSubpanel); // Adding the subpanel to the panelContainer //Report the progress bgWorker.ReportProgress(0, i); // Reporting number of panels loaded } e.Result = imagePanel; // Send the result(a panel with lots of subpanels) as an argument } private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.progressBar.Value = (int)e.UserState; this.progressBar.Update(); } private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error == null) { this.panelHolder = (Panel)e.Result; } else { MessageBox.Show("An error occured, please try again"); } } } Instansiating an object of this class: public partial class Draw: Form { public Draw() { ProgressBar progressBarLoading = new ProgressBar(); // Set lots of properties on progressBarLoading Panel panelBigPanelContainer = new Panel(); Drawing drawer = new Drawing(ref progressBarLoading, ref panelBigPanelContainer); drawer.createDrawing(); // this makes the object start a new thread, loading all the panels into a panel container, while also sending the progress to this progressbar. } } Here is my problem: In the private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) i don't get the e.Result as it should be. When i debug and look at the e.Result, the panel's properties have this exception message: '((System.Windows.Forms.Control)(e.Result)).ClientSize' threw an exception of type 'System.ObjectDisposedException' So the object gets disposed, but "why" is my question, and how can i fix this? I hope someone will answer me, this is making me crazy. Another question i have: Is it allowed to use "ref" with arguments? is it bad programming? Thanks in advance. I have also written how i understand the Background worker below here: This is what i think is the "rules" for background workers: bgWorker.RunWorkerAsync(); => starts a new thread. bgWorker_DoWork cannot reach the main thread without delegates - private void bgWorker_DoWork(object sender, DoWorkEventArgs e) { // The work happens here, this is a thread that is not reachable by the main thread e.Result => This is an argument which can be reached by bgWorker_RunWorkerCompleted() bgWorker.ReportProgress(progressVar); => Reports the progress to the bgWorker_ProgressChanged() } - private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { // I get the progress here, and can do stuff to the main thread from here (e.g update a control) this.ProgressBar.Value = e.ProgressPercentage; } - private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // This is where the thread is completed. // Here i can get e.Result from the bgWorker thread // From here i can reach controls in my main thread, and use e.Result in my main thread if (e.Error == null) { this.panelTileHolder = (Panel)e.Result; } else { MessageBox.Show("There was an error"); } }

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8  | Next Page >