473,671 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling BackgroundWorke r synchronous

Hi,

this may be a stupid question:
How can I can call the DoWork-function of a BackgroundWorke r
synchronous?
Or in other words:
How can I extend the BackgroundWorke r class with a function
RunWorkerSync() ?
I want to write a class that I can call functions synchronous and
asynchronous.
e.g.
This is the code for running the function DoWork asynchronous
(incomplete);

void TestAsync()
{
BackgroundWorke r worker = new BackgroundWorke r();
worker.DoWork += DoWork;
worker.RunWorke rCompleted += RunWorkerComple ted;
...
worker.RunWorke rAsync(Param);
...
}

void DoWork(object sender, DoWorkEventArgs e)
{
Object Param = (Object) e.Argument;

// do something

e.Result = something;
}

void RunWorkerComple ted(object sender, RunWorkerComple tedEventArgs e)
{
m_Result = (Object) e.Result;
}

This is the code for running synchronous (incomplete);
void TestSync()
{
// call synchronous
m_Result = DoWork(Object Param);
}
How can I combime this?
Thanks for your help!

Feb 14 '06 #1
3 9976
If you don't mind polling to check if the process is completed, then
define your own class which inherits from BackgroundWorke r

public class MyBackgroundWor ker : BackgroundWorke r
{
public DoWorkSynchrono us(object Param)
{
DoWork(Param);

while(IsBusy)
sleep(100);

return;
}
}
DISCLAIMER: Code for display purposes, not tested
I hope this helped.

Sincerely,
Bobby

Feb 14 '06 #2
Hello, Pro1712!

Why do you need such a behavior, if you need sycnhrounous behavior then you can simply call DoWork method...

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 14 '06 #3
Hello, bo*********@gma il.com!

b> public class MyBackgroundWor ker : BackgroundWorke r
b> {
b> public DoWorkSynchrono us(object Param)
b> {
b> DoWork(Param);

b> while(IsBusy)
b> sleep(100);

b> return;
b> }
b> }
b> DISCLAIMER: Code for display purposes, not tested

b> I hope this helped.

IMO it is better to wait and not to poll.

public DoWorkSynchrono us(object Param)
{
DoWork(Param);

internalSyncEve nt.WaitOne(time out, false);
return;
}

DoWork(param)
{
//do work
internalSyncEve nt.Set(); //signal that the work is done
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1802
by: Al Christoph | last post by:
I have a program that mixes wizard like behavior with random access to the various dialogs in the wizard. I do this by having each step map to a toolstripmenuitem. Users can randomly choose the menu item and get the corresponding dialog. Alternatively at the completion of each dialog, I can "click" on the menu item and cause the appropriate dialog to come up. It works rather well but the way I've done it is a horrible kludge. I even left...
2
2936
by: Ulf | last post by:
Hi, I have some problems with a BackgroundWorker when calling a WebRequest : private void bgrdTask_DoWork(object sender, DoWorkEventArgs e) { e.Result = WebServerRequest((string)e.Arguments); }
5
14126
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in my FileIO class) I've established Public Event in my core logic classes along with RaiseEvents (that will updated a progress bar on the UI side). This all works great when I'm NOT using Threading (BackgroundWorker), however, as soon as I...
11
7321
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
1
3226
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a progress event. So I added a bw to the form. The Dowork Calls a method which instantiates a DT and calls its Dataprocessing method.
14
6385
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to the do work method, it stops for a long while and then executes the first line of the method and the doesn't do anything else. What am i doing wrong?
0
2471
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following exception when trying to access BackgroundWorker.CancellationPending or e.Cancel An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of...
9
18020
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a backgroundworker and then use the Show method and hide method as necessary? Anyone know of
9
3250
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. Example function test (int a, int b);
0
8400
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8823
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7441
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.