|
Oh, ya know what... I wonder if the BackgroundWorker only allows ReportProgress to be called during the DoWork... Technically, your routine is running AFTER DoWork completes... I really don't think it's intended to be run that way.
The standard way to use a BackgroundWorker is to hook the DoWork EVENT, and put your code there. No need to subclass it, unless you really want to centralize those two boolean property settings.
Just guessing here, of course... I've never seen the BW subclassed... Never had any need to do that.
|
|
|
|
|
Ok I've been doing some more reading, would this be better using a "thread" vs a BGW? Now granted I don't need to put this in a seperate class, however for what ever reason when I use BGWs[0].RunWorkerAsync(); vs backgroundWorker1.RunWorkerAsync(); I'm not able to have the "function" in the same class (the Form) thus why I moved it to a seperate class. Plus when I was getting into more advanced someone reccomended keeping as little as you can on your main form and using classes for as much as possible. Is this a good habbit, bad habbit or just personal preference? Where I work there are two of us, and he does all the C++ stuff and I do the VB (6+ years, though .net in only the last year) and just starting with C# (about 6 months ago).
I have been able to subclass a BGW in VB but obviously not in C#, yet again if I don't have to not a big deal.
Basically what I need to do is be able to run the same function up to 3 times concurrently. I believe if my memory serves me correctally each "occurance" of the function will have it's own local variables. I also believe that if I use a thread vs a bgw I can lock what I'm writing to and instead of throwing a error, the threads will wait until the current one is done updating. Lets say it's just lblOutput.Text = e.UserState.ToString(); (as if it were a bgw perhaps in a thread you can use the variable directally)?
Does this help to explain what I'm trying to accomplish and maybe give you all a better idea of what path I should be taking?
|
|
|
|
|
MacRaider4 wrote: (as if it were a bgw perhaps in a thread you can use the variable directally)?
Nope, that limitation is universal to all types of multithreading in WinForms (And WPF, for that matter). No touching the GUI controls except from the GUI thread.
MacRaider4 wrote: Plus when I was getting into more advanced someone reccomended keeping as little as you can on your main form and using classes for as much as possible. Is this a good habbit, bad habbit or just personal preference?
That's generally a good idea, except in really small tools when it would be complete overkill. The form is your connection to the GUI, and business logic goes elsewhere... In your particular situation, I think I would probably handle it something like this:
(Consider this C#ish pseudocode, as I don't have VS installed on my home machine at the moment)
public static void SendMessagesAsync(ProgressChangedHandler progressCallback, params SomeClass[] messages)
{
for (int idx = 0; idx < messages.Length; idx++)
{
BackgroundWorker wkr = new BackgroundWorker();
wkr.WorkerSupportsProgress = true;
wkr.ProgressChanged += progressCallback;
wkr.DoWorkEventArgs += wkr_DoWork;
wkr.RunWorkerAsync(messages[idx]);
}
}
private static void wkr_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker wkr = sender as BackgroundWorker;
SomeClass args = e.Argument as SomeClass;
}
private void SomethingCalledFromYourForm()
{
MyStaticClass.SendMessagesAsync(wkr_ProgressChanged, new SomeClass[] {
new SomeClass() { Whatever = parameters, YouWant = toset },
new SomeClass() { Subject = someone, Body = "something else?", Recipient = "someone@somewhere.sometime" }
});
}
I don't know what you need to pass to the WriteMail function, but that's generally how you get information into a background worker... Make a class to hold it, and pass it as the argument to RunWorkerAsync().
|
|
|
|
|
Ian Shlasko wrote: I wonder if the BackgroundWorker only allows ReportProgress to be called during the DoWork
No, internally it uses a SendOrPostCallback delegate and an AsyncOperation . If the AsyncOperation is null (there is no active worker thread) then the delegate is called directly. If it's not null then the AsyncOperation 's Post method is called with the delegate and args as parameters which automatically invokes it on the syncronization context that RunWorkerAsync existed in.
public void ReportProgress(int percentProgress, object userState)
{
if (!this.WorkerReportsProgress)
{
throw new InvalidOperationException(SR.GetString("BackgroundWorker_WorkerDoesntReportProgress"));
}
ProgressChangedEventArgs arg = new ProgressChangedEventArgs(percentProgress, userState);
if (this.asyncOperation != null)
{
this.asyncOperation.Post(this.progressReporter, arg);
}
else
{
this.progressReporter(arg);
}
}
|
|
|
|
|
Hey guys,
is it possible to load a .jpg on a form and make it transparently to sketch it in paint or somewhere else?
The picture should always stay in the foreground, but I want interact to the window under it.
any suggestions?
|
|
|
|
|
How about...
- Set jpg as form BackgroundImage
- Set form Opacity to 50% (or whatever)
- Set FormBorderStyle to None (but you will have to handle Move another way)
- Set form TopMost to true
Illogical thoughts make me ill
|
|
|
|
|
yeah, thank you !
But 1 Problem remain...
How could I draw under it without set the focus to the Form?
|
|
|
|
|
Ahhh.. I knew it seemed too easy
Maybe this[^] will help
Illogical thoughts make me ill
|
|
|
|
|
Yeah its vb.net but I have no problem with it !
Thank you very much
|
|
|
|
|
I am using a windows service project i.e. winService.
Inside this project, I have placed an xml file.
This winService project, calls a class library project i.e. classLibrary.
Inside the classlibrary, there is a class i.e. myclass.cs which has to get the path of the xml file which as mentioned above is inside the winservice project and it is called test.xml
I am using the following code in the class myclass.cs which is inside the classlibrary project to get the path of the xml file:
string strPath = AppDomain.CurrentDomain.BaseDirectory + "test.xml";
Question:
The code above returns the path of the winservice\bin\debug
Is it correct to place the test.xml inside the bin\debug folder?
or Should the test.xml file be placed on the root of the winservice project instead?
and is the code ok?
Thanks
|
|
|
|
|
When you deploy your project, your executable will be located in a different directory to the one you build into. This means that your xml file should be in the executable directory or a subdirectory of that, so your code is fine(ish). When creating paths from variables, you should always look to use Path.Combine(pathroot, file); .
|
|
|
|
|
Hi,
By executing directory I think you mean my winservice project in this case.
If so, I have placed the test.xml file in this directory.
Am I right so far?
|
|
|
|
|
|
I see.
I will continue testing and will ask questions later if it is ok please.
Thank you.
|
|
|
|
|
As Pete says, paths are notorious for changing, keep them fluid.
------------------------------------
I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave
CCC League Table Link
CCC Link[ ^]
|
|
|
|
|
arkiboys wrote: string strPath = AppDomain.CurrentDomain.BaseDirectory + "test.xml";Question:
The code above returns the path of the winservice\bin\debug
Is it correct to place the test.xml inside the bin\debug folder?
It does?
Shouldn't it be AppDomain.CurrentDomain.BaseDirectory + "\\test.xml" or a variation thereof?
..and water fell from the sky like rain.
|
|
|
|
|
As I stated earlier, this method should not be used. Path.Combine is the best way to create the path structure.
|
|
|
|
|
if your app would need to create/modify the XML file, it would not be wise to put such data file in the EXE's folder as:
1. the folder is shared by all users;
2. the folder might be write-protected.
When in doubt, the better approach is to use one of the official folders as returned by Environment.GetFolderPath() .
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I never put settings files in an application directory; I put them in a settings directory -- that's what they're for.
For instance C:\Documents and Settings\All Users that way they can even be shared among different applications as well as different users.
|
|
|
|
|
Hi All,
I want to show some ongoing even on in the mean time when the connection is being opened.
When I tried to show the LABEL or tried to MODIFY the text of button, it doesn't happen till
the conn is opened. After conn opens - then only the text is changed as 'Connecting...' ,
which I want to show in the mean time when connection is getting opened.
Any help..?
Thanks and regards
- Ajay K
|
|
|
|
|
It sounds like you are trying to update the label and open the connection on the same thread. If you need them both appear to be happening at the same time, you should move the connection handling onto a background thread.
|
|
|
|
|
the following sequence could suffice:
label1.Text="Going to connect";
label1.Refresh();
Connect();
label1.Text="Connected";
label1.Refresh();
however it is always better to delegate long-winding or possibly long-winding operations to another thread.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
But what if it doesn't connect? Then the "Connecting..." text is a lie and you've just lied to your trusting user; that's poor customer relations. Don't promise something that you can't deliver. Change the text to "Attempting to connect... this usually works... but some times it doesn't... and if it doesn't, then I'll be just as sad as you will be and we can cry together."
|
|
|
|
|
Hi all,
I want to use a dotnetnuke login code in a c# application and the data is store in MSSQL database.
I am new to it ?
Can anyone tell me how can i use it.
Thanks in advance.
|
|
|
|
|
You could try here[^].
I must get a clever new signature for 2011.
|
|
|
|