Click here to Skip to main content
15,880,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are using the following code.

In base function (onfinishes), if I change JobSummaryobj.filename, sometimes the updated value of filename is not getting passed to launchRequest. Can someone please suggest what to do?

internal virtual onfinishes(object sender, EventArgs<batchjobresult> args)
{
    JobSummary.OutputLocation = JobSummary.BaseOutputFilePath;
     RunInBackground(JobSummary);
}

private void RunInBackground(JobSummary JobSummaryobj)
{
    var backGroundWorker = new BackgroundWorker();
    backGroundWorker.DoWork += (sender, aventArgs)=>runController.launchRequest(aventArgs.Argument as JobSummary);
    backGroundWorker.RunWorkerAsync(JobSummaryobj);
}

public void launchRequest(JobSummary JobSummaryobj)
{
    // sometimes i don't get updated value here .
}


What I have tried:

I tried passing the object by ref
Posted
Updated 9-Aug-19 0:30am
v2

1 solution

I don't have your classes, but a quick test with "abstracted code" from yours:
C#
private void DoIt()
    {
    var backGroundWorker = new BackgroundWorker();
    backGroundWorker.DoWork += (sender, aventArgs) => launchRequest(aventArgs.Argument as MyClass);
    backGroundWorker.RunWorkerAsync(new MyClass(1234));
    }

private void launchRequest(MyClass JobSummaryobj)
    {
    Console.WriteLine(JobSummaryobj.I);
    }
private class MyClass
    {
    public int I { get; set; }
    public MyClass(int i) { I = i; }
    }
Give me consistent results, as I'd expect.

I'd suggest that the problem is with your JobSummaryobj rather than the code you show, and I'd try looking closely at it and what happens to it before, during and after the setup for the BackgroundWorker. It may be that the main thread is changing the value to soon or too late and that shows up as an inconsistency in your secondary thread intermittently.

But without your whole system there is nothing we can do to help!
 
Share this answer
 
Comments
OriginalGriff 11-Aug-19 10:51am    
Then use the debugger and see what the value of RunMode is...
OriginalGriff 12-Aug-19 3:04am    
That doesn't mean that the value isn't being changed after RunInBackground is called, and before LaunchRequest is called by the new thread: you seriously need to use the debugger on your whole system, not just throw us a tiny fragment and hope we can fix it for you: we can't.

I have no idea how you "debugged already" but you may want to start looking at the more complicated functions that VS can offer, such as Data Breakpoints
https://devblogs.microsoft.com/cppblog/data-breakpoints-15-8-update/
and so forth.
We can't do that for you!
sapanagarud 12-Aug-19 3:58am    
thank you
OriginalGriff 12-Aug-19 5:16am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900