|
Invoke is explained well in the article I provided. However:
If the big calculation (how long would it take on the GUI thread anyway?) is accessing a GUI Control all the time, then it makes little sense to delegate that to another thread, as you would need lots of Invoke operations (much code and much delay, switching a thread for just a few lines of calculations is silly).
There are two other approaches possible:
1. Use a DataTable of a DataSet; load those with the data, perform your calculations on them, then (re)display them somehow. I'm not familiar with the DevExpress sheet, I assume it knows how to display (or bind) to a DataTable. So you could show the original data, start computations in BGW, and have it pass the new state of the DataTable say every second, so the user sees progress (assuming it all takes several seconds, can't tell from what you have shown).
If you use binding (I don't!), chances are you must undo the bind before and redo it after the big computation (and also redo and undo it if relevant in progress handling).
2. Second alternative, very simple: Do everything on the GUI thread, start by showing a WaitCursor (so user knows and accepts he is not in charge for some time), then do what you want; if you want the user to see progress, call the Refresh method of the Control of interest (e.g. a ProgressBar); be careful to do that sporadically (say after dealing with 10 or 100 rows, roughly once a second would be enough), otherwise the execution will be extremely slow.
FYI: MessageBox is thread-safe, you can always call it, whatever thread you're on.
PS: No, I haven't read anything in your description that would warrant more than one BGW (or one extra thread).
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
Thank you. That was helpful. I tried what you mentioned. I directed all my final results into a datatable and then, used an assembly to bind datatable to cells. The total calculation takes less than a second now. Thank you very much.
|
|
|
|
|
Thank you for the feedback.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
|
Research the invoke pattern. It is well documented, and required if you want to access UI components from a thread.
And we have plenty of articles on CP explaining that concept.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello. I'm fairly new in C# .NET development, but I got a task to monitor data from the sensors using C# desktop application. I'm using ESP8266 to collect data from the sensors and I can send the data from the ESP to a remote MySQL database. I am free to graph the data obtained, using HTML, Javascript (Highcharts) and CSS - everything works fine. But now I have to display the received data from the ESP8266 in the windows desktop app remotely. Maybe someone could point me in the right direction? Thank you very much for the answers.
|
|
|
|
|
What have you tried?
Where are you stuck?
What help do you need?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I really don't know where to start. I don't know what things are used for this purpose. I know a TCP client is used for similar things in C#.
|
|
|
|
|
Is the "remote" MYSQL db on the same device as the "remote" Windows desktop app?
If it is, have the Windows app retrieve the (time stamped) data it needs.
Even if it isn't, since you know how to send to a remote MySQL db, I assume you should be able to read / poll it remotely also.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
Hello Friends,
I have a requirement and would like to know how to code for this in C#.net:
I receive two files, one in BAI2 format and one in AMI format.
1.) How do I take AMI formatted file and convert it into BAI2 format file?
2.) How to do edit BAI2 file to add a unique identifier at the end of each line in the file.
I have installed Visual studio 2019.
TIA.
Sasi
|
|
|
|
|
There is no shortcut: you will need to learn to read AMI format: Amazon Machine Image - Wikipedia[^] and BA12 format: BAI2 Format Specification - SEPA for Corporates[^] and then write a reader for one, and a writer for the other. Then you need to work out what parts of your AMI document you need to access and store as BA12, because from what I can see the two are completely unrelated ...
Good luck with that: AMI format does not look like fun to process at all!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I had a class ClusterHazardLocations which inherited some levels down from IEnumerable<IHazardLocation> . In case of an alarm, an AlarmInfo object is transferred from a sensor device via WCF to some other computer showing the information to the user. That AlarmInfo has an IHazardLocations property, and in this case it was a ClusterHazardLocations object. It used to work.
Then I added a simple function to that ClusterHazardLocations class: public void Add(IHazardLocation other) . The mere existence of the function - even with an empty body - causes the WCF communication to fail. I do not understand that at all. After all, IEnumerable does not have an Add method and the class does not derive from a List or similar base class.
After renaming the Add method to AddHazardLocation , everything worked again.
Could you please explain how this odd behavior of WCF was caused?
Edit:
"a small project" with WCF... Ehm. But a small interface /class hierarchy:
public interface IHazardLocation {}
public interface IHazardLocations : IEnumerable<IHazardLocation> {}
public interface IClusterHazardLocations : IHazardLocations { }
[Serializable]
public class ClusterHazardLocation : IClusterHazardLocation
{
public ICluster Cluster { get; }
[UsedImplicitly]
public ClusterHazardLocation()
{ }
public ClusterHazardLocation(ICluster _cluster)
: this()
{
Cluster = _cluster;
}
}
[Serializable]
public class ClusterHazardLocations : IClusterHazardLocations
{
[NotNull]
private readonly List<IClusterHazardLocation> m_Locations;
public ClusterHazardLocations()
{
m_Locations = new List<IClusterHazardLocation>();
}
public ClusterHazardLocations([NotNull] params IClusterHazardLocation[] _locations)
{
m_Locations = _locations.ToList();
}
public ClusterHazardLocations([NotNull] IEnumerable<IClusterHazardLocation> _locations)
{
m_Locations = _locations.ToList();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable<IHazardLocation>)m_Locations).GetEnumerator();
}
public IEnumerator<IHazardLocation> GetEnumerator()
{
return ((IEnumerable<IHazardLocation>)m_Locations).GetEnumerator();
}
public void AddHazardLocation(IHazardLocation _other)
{
if (_other is IClusterHazardLocation tmp)
{
m_Locations.Add(tmp);
}
}
}
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
modified 8-Feb-21 10:12am.
|
|
|
|
|
Is this something you can recreate with a simple dummy project? If it's something you could upload to github, I'd enjoy having a look at it.
|
|
|
|
|
Thanks for your help. Unfortunatey, it's too big...
Do you have some similar situation some where:
- a class which inherits IEnumberable, but does not derive from List/Array etc, instead contains a List
- can be transferred via WCF
- now add an "Add" method
I am not sure if those are already all the required steps.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
I meant a simple repro of the issue rather than the thing itself.
|
|
|
|
|
What Pete is suggesting is a dummy project to prove where the fault lies: create a minimal sample that doesn't have the problem, add the Add method and see if it fails. If it does, put it up on Github for examination.
If it doesn't ... then the Add addition is probably coincidental, and you broke something else at the same time!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: the Add addition is probably coincidental, and you broke something else at the same time
Absolutely sure: by renaming "Add" to "AddHazardLocation" the problem was solved.
I did the analysis step by step - tedious minimal steps. Nothing else was changed at that moment.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Creating "empty" interfaces seem questionable to start with.
public interface IHazardLocation {}
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I wrote some codes in DevExpress spreadsheet which gets information from a sheet and then put them into a dictionary and after that do some calculations and at the end creates a new sheet and fill the cells with calculated data.
I want to use a progress bar for the whole process. I created a progress bar and tested it with a sample value of 50. When the calculations start, progress bar is in freezed status and shows nothing. When the background calculations end, the progress bar unfreezed and show 50%.
How can I solve this problem?
modified 7-Feb-21 22:28pm.
|
|
|
|
|
Please explain how you're running the background task, and how you let it report progress updates to the progress bar.
|
|
|
|
|
I'm going to update the progress bar based on each row is filled at the new sheet. I tried to test it with a constant value of 50 and noticed that it freezes until the end of the calculations. The calculations consist of the following steps: 1. Data is read from a sheet into two Lists. One list is string type and another is integer type. String type list consists of values of 4 cells in each row.
2. Lists are put into a dictionary. Strings are keys and integers are values.
3. Values of the same keys are summed.
4. The mentioned two lists are cleared and fill by dictionary keys and values.
5. Dictionary keys and values are split into cells in a new sheet.
modified 7-Feb-21 22:38pm.
|
|
|
|
|
_ = Window.Current.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => {
} );
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
When you execute a lengthy operation (any computation exceeding say 50 msec, but also anything that might occasionally be slow, e.g. an internet access) in one of the GUI event handlers (e.g. a button's Click), it will block the main=GUI thread, as a result your GUI freezes.
There are a number of bad attempts to fix this. The good ones all involve another thread, but then, beware, other threads are not allowed to touch your GUI Controls. The recommended approach is by using one BackGroundWorker; it will perform the bulk of the job on a separate thread, yet it provides a progress reporting event which automatically does run on the main=GUI thread, so that is by far the number one way to keep your GUI alive and responsive.
When the BGW finishes up, it fires a Completion event which typically is used to report all mishaps that may have occurred (make sure to handle its Exception which unfortunately there is called Error), it could also be used to e.g. re-enable a Button; that event handler also runs on the main=GUI thread, again no worries.
If unfamiliar with BGW, look for (a) the documentation and (b) a good example, and study these first. It may seem complex, but it isn't; it is the easiest way into multi-threading, something most applications are bound to take advantage of.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
I have a C# console application that is launched from a WinForm application.
The output of the console application is well-formatted; however, user can run the console application directly. Is there a way to restrict/force the console application to be only run from the main application (WinForm)?
|
|
|
|
|
Pass a mystery parm to the console app that only the Windows Form knows about. No parm, no run.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|