|
Given the different actions the user can perform on the content of the TextBox ... paste, delete selected lines, etc. ...
I think your strategy should be to perform whatever content enumeration only when the textBox is not in use.
What is the reason you think you need to use a BackGroundWorker here ?
If you must use it, look at the use of 'ManualResetEvent, and use of 'Monitor and 'Lock.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I set the AssemblyInfo in my app to this:
[assembly: AssemblyVersion("1.0.*")]
Yet each time I compile it only increments the Revision, not the Build #. So I end up with something like "1.0.7404.29813", with only the last part ever being incremented.
I display the version info in the status bar of my app, and that's a lot for the users to read back to me during debugging. Is there a way to increment the Build portion each time I compile?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
According to the documentation[^], the default build number should increment daily. You should only ever get 7404 on 9th April 2020.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
suppose you have two lists of the same size, i.e. same number of elements
List<int> a, List<int> b
is it possible to traverse the two lists simultaneously and compare the values? Element 1 in list a to element 1 in list b, element 2 in list a to element 2 in list b, etc. I use a Foreach when traversing lists, but am unsure of how to do a side by side comparison of list b when I am traversing list a.
Thanks
|
|
|
|
|
Use a for instead of a foreach :
for (int i = 0; i < a.Length && i < b.Length; i++)
{
if (a[i] == b[i])
{
...
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Lists work just like arrays, so you can use either an enumerator (foreach) or you can use an index value:
for(int index = 0; index < someValue; ++index)
{
if (a[index] == b[index])
{
}
else
{
}
}
|
|
|
|
|
If the "lengths" of a and b are equal, you can maintain your own index:
int i = 0;
foreach ( int value in a ){
bool result = (value == b[i++]);
}
Note the suffix (++) operator in this case.
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
|
|
|
|
|
You could also use the Zip extension method, which takes a lambda with two arguments, which is called with the corresponding elements. It yields a IEnumerable of the results of those lambda calls.
var a = {1,2,3,4};
var b = {2,3,4,5}
var c = a.Zip(b, (aa, bb)=> aa * bb).ToList();
var d = {…};
var e = {…};
if (d.Zip(e, (dd, ee)=> dd==ee).All())
{
Truth,
James
modified 13-Apr-20 8:01am.
|
|
|
|
|
One
I have a WPF window which displays a datagrid of Project info. At the top, above the datagrid, are two combo boxes; one for Companies and the other for Project Status. The user can then filter the datagrid by either the Project's company, the Project's status, or both.
When the user opens the window, I want the combo boxes to load asynchronously so the window stays responsive. I want the window to appear, and then load the combobox data. So I have the Window_Loaded event bound to a command, which fires this:
private async Task<bool> WindowLoadedExecuted()
{
await LoadLists();
await LoadProjects();
return true;
}
private async Task<bool> LoadLists()
{
var companyHeaders = await AppCore.BizObject.GetCompanyHeadersAsync();
CompanyHeaders = new List<CompanyHeaderEntity>(companyHeaders);
var projectStatuses = await AppCore.BizObject.GetLookupsAsync(Constants.LookupCategoryProjectStatuses);
ProjectStatuses = new List<LookupEntity>(projectStatuses);
return true;
}
The question here is, is there anything wrong with simply returning a Task instead of Task<bool>? Neither of these methods are going to return anything here. I'm asking because I've seen some articles and posts that say it's not a good idea, but I don't see why. Anything wrong with returning Task instead of Task<t> if there's no return value?
Two
Both of the comboboxes above can trigger a requery. By selecting either a Company or a Status to filter by, I want to reload the datagrid. So, in the properties for both, in their getters, I do:
private CompanyHeaderEntity _SelectedCompanyHeader;
public CompanyHeaderEntity SelectedCompanyHeader
{
get { return _SelectedCompanyHeader; }
set
{
if (_SelectedCompanyHeader != value)
{
_SelectedCompanyHeader = value;
RaisePropertyChanged("SelectedCompanyHeader");
new Task(async () =>
{
await LoadProjects();
}).Start();
}
}
}
To me, this makes sense because selecting a company (instead of none for All), what the user is doing is filtering the list to only those companies. So changing the SelectedCompany property should fire this off.
Yet again, I see posts that seem to discourage this, like this one. Any real reason not to do this? Or is there a better way?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 9-Apr-20 0:26am.
|
|
|
|
|
If there's no return value, then it makes sense to return Task rather than Task<T> . I've never seen any problems doing that.
For the property setter, I'd be inclined to change it to:
set
{
if (_SelectedCompanyHeader != value)
{
_SelectedCompanyHeader = value;
RaisePropertyChanged(nameof(SelectedCompanyHeader));
_ = LoadProjects();
}
} following the advice from David Fowler:
AspNetCoreDiagnosticScenarios/AsyncGuidance.md at master · davidfowl/AspNetCoreDiagnosticScenarios · GitHub[^]
new Task(fn).Start() should generally be replaced with Task.Run(fn) . But in this case, LoadProjects already returns a Task , so there's no need to create another one.
If the method fails, it will raise the TaskScheduler.UnobservedTaskException[^] event rather than terminating your process.
If you didn't assign the result to a discard, you'd get a IDE0058 warning.
NB: If possible, I'd also be inclined to update your RaisePropertyChanged method to use the CallerMemberName attribute, so that you wouldn't need to pass in the property name when you call the method from a property setter.
protected void RaisePropertyChanged([CallerMemberName] string propertyName = default) { ... }
...
public CompanyHeaderEntity SelectedCompanyHeader
{
get { return _SelectedCompanyHeader; }
set
{
if (_SelectedCompanyHeader != value)
{
_SelectedCompanyHeader = value;
RaisePropertyChanged();
_ = LoadProjects();
}
}
} Caller Information (C#) | Microsoft Docs[^]
If for some reason you can't do that, at least use nameof instead of strings. That way, if you use a refactoring tool to rename the property, the value passed to the RaisePropertyChanged method will be updated to match.
nameof expression - C# reference | Microsoft Docs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks. I appreciate your input.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I am trying to make a bot for an external application, in first i used to sendkeys (^a ^c) To the application window after clicking In middle of chat , then get the text from my clipboard And store it in file then read it and answer by sendkeys , and i do all of That under timer control I do all these tasks every 1 second , recently i was searching on another way to do it Because using Sendkeys is taking my processor and pc is going to be more slow further I can't use my pc when my bot is working , after long Search i have found UI automation Which i can access control's another application and get/set text I have found way how to Do it with a notepad , but i don't know or it didn't work for my application I used the below code to get the text from a notepad ,i Want to know the changes I have to do in the code to work in my application , i have downloaded Autoit and spy++
<pre>private void btn_getText_Click(object sender, EventArgs e)
{
PropertyCondition idCond = new PropertyCondition(AutomationElement.AutomationIdProperty,"15");
AutomationElement nameTxt = mainWnd.FindFirst(TreeScope.Descendants, idCond);
if (nameTxt != null)
{
TextPattern txtPattern = nameTxt.GetCurrentPattern(TextPattern.Pattern) as TextPattern;
txt_output.Text = txtPattern.DocumentRange.GetText(-1);
}
}</pre>
I have found , if the application does not support ui textpatteren , it won't work but i don't know how to know that , i don't know The platform of the application made from.
The application i was trying to get text from it www.RankedGaming.Com
Here is a screen shot of the application https://prnt.sc/ruxo2m We type the textbot down and the messages are going to be shown In the top of chat , even i don't know what is that tool Its not going to be written inside its just for read.
|
|
|
|
|
How to read/load a certificate stored in a JKS (Java key stores) file format, in .NET (C#) ?
after loading JKS how to validate certificate in .NET (C#) ?
|
|
|
|
|
It's been a long time since I researched it and wrote code for it.
But I had to create a certificate, and use Linux OpenSSL to convert the certificate to a fully loaded PFX file.
Then wrote a config file called certificate.Development.json and certificate.Production.json so the right certificate would load based on deployment.
As far as validation goes, for me it was a matter of writing code to load, and fine tuning the certificate so it would load, or else it failed to load because it was invalid.
var certificateSettings = config.GetSection("certificateSettings");
var certificateFileName = certificateSettings.GetValue<string>("filename");
var certificatePassword = certificateSettings.GetValue<string>("password");
var certBuffer = File.ReadAllBytes(certificateFileName);
var serverCert = new X509Certificate2(certBuffer, certificatePassword);
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Hi,
I am reading pulses of nearly 40 sensors in my program. I want to calculate 40parameters based on real-time pulses and also nearly 1000 real-time parameters with calculation formula contained some constant and values calculated based on sensors pulses as the formula variables.
1- The first question is: Is it a good way to use from background running data tables in some datasets to perform all of these tasks at least every one second? what is the best way to perform the described affair?
2- I also want to make a link between data tables columns that when data in other tables inserted, they collect in a table based on one of the columns. For Example, I have the same column in all tables like an auto number, I want to collect data in a collector table automatically with joining (make relation among) tables.
Thank you in advance for your answers
|
|
|
|
|
Use a "one second timer". The timer event calls a method that samples each sensor and does any "aggregating". If it matters, you need to disable / enable the timer on each tick and compensate if necessary (in case of possible reentrancy).
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 would definitely disable the timer upon entry to the method!
Assuming you are not storing the sensor details every second.
Once you have done the processing store the results in the database, associated tables with foreign key relationships are a basic operation.
Store the primary record
Get the ID from the just inserted record
insert the related records with the primary key.
wrap the entire operation in a transaction and/or use a stored procedure
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
I wouldn't use a System.Timer because the timer events are the lowest priority events in Windows, so a busy system will ignore/skip events in favor of more important work. I think a thread for each sensor to accept/store new data in the database would be a better solution.
As far as visualizing the data, I would either make the user request new data (with a button click), or set up another thread that reads the new data at a regular interval. The length of the interval would be entirely dependent on the level of performance of your hardware and network (if your database is on a separate machine).
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I would have thought that 40+ threads on a single machine would have been an issue.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
really depends on the machine i think. On a reasonably powerful machine with a quad core cpu (or better) and at least 16gb of ram, it should be okay. You can always set the CPU affinity for each thread if you need to.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Ok, very basic to you guys but I'm hoping someone can offer some advice.
I'm making a parking lot windows forms project and have a class structure of:
Vehicle
Car:Vehicle
Van:Vehicle
etc.
Then a ParkingLot class which declares a private Dictionary collection of type <string, vehicle=""> to store all the instances. The key is a parking space code e.g. A1, A2, B1, B2 etc, which then returns the vehicle object associated with that slot in the parking lot.
I've made the dictionary private so that all of the functionality is contained in the parkinglot class and the form button presses etc only update the dictionary via public methods in that class for abstraction/separation of concerns (if that's the correct terminology!). The contents of the dictionary are currently displayed in a textbox by calling a parkinglot.displayinventory method which loops through the dictionary items and uses a stringbuilder to create and return the details of all the items to display in the textbox.
All was fine, however, now I want to change it to display the contents in a listbox rather than a text box and now I'm questioning whether using a dictionary object was a good idea (I was thinking of the efficiency of retrieval O(1) of dictionary vs O(n) of list and all that).
Should I just use a public List<vehicle> which I can then bind to the listbox? But then I thought from an abstraction point of view, having the list of items accessible from the form/view was a bad thing?? Should I stick with a private dictionary for efficient retrieval and create and return a readonlycollection object from the displayinventory method for binding (or something similar)?
It's been a while since I've worked with collections and classes and things like this and I've confused myself no end with something so simple! Any tips much appreciated
|
|
|
|
|
Hi,
I see no fundamental difference between displaying vehicular text in a TextBox and the same in a ListBox.
You never have to use binding, but you can.
Without using any binding, you can iterate your Dictionary and build a string for each Vehicle (I'd say your Vehicle.ToString method should serve that purpose), then add that string to the ListBox.Items
Or indeed you can set up a List of Vehicles and use binding; I know I wouldn't, as I don't like creating a second collection with all the risks of getting out of sync... You could consider using Dictionary.Values as the list of items to be displayed; depends on how you handle empty spots I guess.
PS: the one advantage of using a ListBox, if your app would need that, is that you can actually paint some graphics in a ListBox item (an icon, a picture, whatever); a ListBox collects objects, they don't have to be strings! It would require you to add the actual Vehicles to the ListBox.Items, your ListBox to be set to UserDrawn mode, and you to implement a DrawItem handler.
PS2: can you store a couple of bikes in one spot?
modified 4-Apr-20 18:03pm.
|
|
|
|
|
Thanks Luc. Yes, I could just iterate through the dictionary and populate the listbox but my concern is that I would be exposing the 'master' collection to the form. From an abstraction point of view I was trying to avoid this and only allowing a method (parkinglot.displayitems) to output the contents of the dictionary in a read only form (currently a string that is formatted ready to just display in the textbox)... this is where I've now got confused! Do I just return a copy of the dictionary from this method or some other collection (which I can then iterate through or bind to), or do I just expose the master dictionary object? I'm trying to follow/show good design practices with this project :-/
|
|
|
|
|
When you return a collection of objects basically it is an array of pointers. Making the collection read-only protects the array, not its elements (the objects it refers to). So whatever you do, as long as you export instances of Vehicle, they become public and modifiable.
There are two ways to remedy this:
1. Create and export a deep copy: build a new collection with copies of your Vehicles. Now if Vehicle contains references (say to a Manufacturer class), then these objects must be deep copied too. That soon gets very cumbersome.
2. Create and export exactly the information you need, in a data structure that isn't otherwise used in your ParkingLot class. In this case that would be a list of strings. And then you should create this data each time it is requested, you should not store it inside ParkingLot and just pass a pointer, as then one client could still falsify the information another client gets.
Either way, the clean approach is to provide a ParkingLot property or method that creates and returns the information required; and then you can use that any way you want, no harm can be done to the ParkingLot.
The one thing you probably would also need is a way to signal the outside world your ParkingLot has changed. An event seems appropriate here.
|
|
|
|
|
Ah, yes, of course they are. Completely forgot it just stores pointers to the objects. Thanks for pointing that out!!
As I said, it's been a while! Thanks for your help
|
|
|
|
|