|
There is a method that background workers have that is called "ReportProgress", and a event called "ProgressChanged" that triggers when the ReportProgress method is called. You can access the ProgressChanged from the original thread, unlike trying to directly access the progress bar via progressbar.value = x .
|
|
|
|
|
gajatko wrote: progress.BeginInvoke
Do not use BeginInvoke, as that will queue the invocation. Invoke should be sufficient.
|
|
|
|
|
Application hangs when using Invoke instead of BeginInvoke . From debugger, it stops on call to the Invoke method.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
You're making it harder than it has to be.
Use a BackgroundWorker object to perform the calculation, and call the UpdateProgress method at the desired interval.
private BackgroundWorker myWorker = BackgroundWorker();
private void InitEncryptWorker()
{
myWorker.WorkerReportsProgress = true;
myWorker.WorkerSupportsCancellation = true;
myWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(myWorker_RunWorkerCompleted);
myWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(myWorker_ProgressChanged);
}
public void myWorker_DoWork(Object sender, DoWorkEventArgs e)
{
BackgroundWorker thisWorker = sender as BackgroundWorker;
int progress = 0;
for (int i = 0; i < 1000; i++)
{
if (!thisWork.CancellationPending)
{
thisWorker.ReportProgress(progress);
}
}
}
private void myWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
if (e.ProgressPercentage != this.progressBar.Value)
{
this.progressBar.Value = e.ProgressPercentage;
}
}
private void myWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
This way you use only one thread for calculations and utilize 25% of quad core processors
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
C'mon, be a programmer. If your calculations take so long that it's bothersome, put some Thread.Sleep(10) calls in the loop somewhere.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
John Simmons / outlaw programmer wrote: C'mon, be a programmer
Yeach and then post your code on a Coding Horrors forum before someone else does.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Yeah, I'll do that. In the meantime, you'll still be dickin' around with your code, and I will have finished this particular task moved on to something else.
On the other hand, maybe using 25% of the CPU ain't so bad, after all. Of course, we can't determine the trade-offs because you haven't given us all the requirements.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Parallel.For does not run the calculation in the background. It just splits the work to multiple threads, but the UI is blocked until the calculation has finished.
If you want multiple threads to calculate in the background, use BackgroundWorker+Parallel.For.
|
|
|
|
|
Frankly speaking I use my own implementation of Parallel to preserve .Net 2.0 compatibility but it works as you said, too. I will check if it works and post soon.
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
BackgroundWorker+Parallel.For combination doesn't work.
progress.Value = e.ProgressPercentage; throws an exception (Cross-thread operation not valid: Control 'progress' blah blah blah)
and
progress.Invoke(new Action(delegate {
progress.Value = e.ProgressPercentage;
})); hangs the program. Any ideas? I though that this situation is so common that there is at least one satisfactory solution for it...
Greetings - Gajatko
Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
|
|
|
|
|
Why is it hanging?
Take a look at what the threads are doing.
Why is the main thread blocked? What is it waiting for? Maybe you have a dead lock? (progress.Invoke waits for the main thread to finish any active event handlers, maybe the main thread is somehow still waiting for your calculation?)
|
|
|
|
|
Hello,
this is my code to retrieve the DeviceId of a windows mobile 5.0
[DllImport("coredll.dll")]
private static extern int GetDeviceUniqueID([In, Out] byte[] appdata, int cbApplictionData,
int dwDeviceIDVersion,
[In, Out] byte[] deviceIDOuput, ref uint pcbDeviceIDOutput);
private static string s_UniqueDeviceID;
public static string GetUniqueDeviceID()
{
if (string.IsNullOrEmpty(s_UniqueDeviceID))
{
// define AppString
string AppString = "MyAppString";
// get the byte array for the string
byte[] AppData = Encoding.ASCII.GetBytes(AppString);
// some parameters for the native call
int appDataSize = AppData.Length;
uint SizeOut = 20;
// our out parameter
byte[] DeviceOutput = new byte[20];
// Call the native GetDeviceUniqueID method from coredll.dll
GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, ref SizeOut);
// check that we got something
if (SizeOut > 0)
{
// build text from received bytes
StringBuilder resultText = new StringBuilder();
for (int i = 0; i < DeviceOutput.Length; i++)
{
resultText.Append(string.Format("{0:x2}", DeviceOutput));
}
// set up result
s_UniqueDeviceID = resultText.ToString();
}
}
return s_UniqueDeviceID;
}
}
The GetDeviceUniqueId() raise an error of type Cannot find an entry point .
Do someone can help me.
Best regards
dghdfghdfghdfghdgh
|
|
|
|
|
Wow!!! you get the most absurd question of the day award...
First, you didnt even have time to write your subject/question in detail.
Second, instead you posted your code completely (without explanataion) to make us guess.
Third, this is not Mobile Dev Forum...better look in Mobile Development[^]
Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.
|
|
|
|
|
It's a pinvoke question on C#. Not really offtopic.
|
|
|
|
|
Obviously GetDeviceUniqueID does not exist in coredll.dll. If you wrote coredll.dll yourself, did you export the function correctly?
|
|
|
|
|
Thats what I figured, but if you go down to pinvoke.net thats where it is. So somethings wrong.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Try this method instead:
private byte[] GetDeviceID(string AppString)
{
byte[] AppData = Encoding.Unicode.GetBytes(AppString);
int appDataSize = AppData.Length;
byte[] DeviceOutput = new byte[20];
uint SizeOut = 20;
GetDeviceUniqueID(AppData, appDataSize, 1, DeviceOutput, out SizeOut);
return DeviceOutput;
}
byte[] id = GetDeviceId("myAppName");
|
|
|
|
|
Oh - you should also declare the function like this:
[DllImport("coredll.dll")]
private extern static int GetDeviceUniqueID([In, Out] byte[] appdata,
int cbApplictionData,
int dwDeviceIDVersion,
[In, Out] byte[] deviceIDOuput,
out uint pcbDeviceIDOutput);
|
|
|
|
|
Hi. I've got a windows service that uses classes defined in it. Only thing is the dev environment does not pic the classes up. They are there but I get the following.
Error The type or namespace name 'clsUploadToMoss' could not be found (are you missing a using directive or an assembly reference?)
Can someone tell me why this is happening?
Stephen Lintott Bsc IT (RAU)
|
|
|
|
|
As the compiler error indicates, are you missing a using; directive or an assembly reference?
/ravi
|
|
|
|
|
Thats what I don't get. The Classes are in the same namespace(project) as where I'm calling them from. Is there a setting I am missing or something like that?
Stephen Lintott Bsc IT (RAU)
|
|
|
|
|
This might be a dumb question, but did you perhaps forget to add the source files to the project?
/ravi
|
|
|
|
|
No they are there.
Stephen Lintott Bsc IT (RAU)
|
|
|
|