|
Yep. Or string.IndexOf as well.
"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!
|
|
|
|
|
While I`m running/debugging a c# app I`m developing in VS that has a while loop with no exit, a program freeze takes place when the while statement is reached, at the same time the OS continues to be responsive, is that because the computer is running an 64 bit OS (with threads on multicore). Would a while statement like that cause a 32 bit OS to freeze?
Also if I`m running a recursive function with no exit from recursion the program will break with an `stack overflow` error. Why is the debugger interrupting the program in the case of the recursive function and doesn`t in the case of a while loop?
|
|
|
|
|
With a "stack overflow", even the debugger can't continue.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
with the right pair of boots and equipment for hostile environment you never know
|
|
|
|
|
A while loop isn't an error as far as the system is concerned - it just means that your display can't be updated and user input is ignored until the loop exist and the event handler is finished. The system doesn't "know" how long you will be looping for and has to assume that you intended that to happen!
Unbounded recursion is different: each recursive call uses up an amount of some special memory called the stack - and when that runs out (and it does, pretty quickly) you app has to stop running because it can't execute any more code; it needs the stack to do anything. So it throws an exception - Stack overflow - which the debugger picks up for you and stops you app so you can see what is happening.
You shouldn't use big loops in event handlers - they should be moved to a second thread so that the UI thread can continue to update the display and deal with user input. Have a look here: BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
"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!
|
|
|
|
|
Thanks Griff that`s a good theoretical explanation
|
|
|
|
|
You're welcome!
"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!
|
|
|
|
|
A while loop without some sort of exit will cause the application to freeze, but will leave the OS alone (as long as the stack is filled up, or a some sort of min/max value isn't reached).
This will execute forever, freezing the app, but leaving the OS responsive:
public void MethodX(string x)
{
while (true)
{
}
}
This will eventually crash the app with a stack overflow exception:
...
MethodX("");
...
public void MethodX(string x)
{
while (true)
{
MethodX("1234567890");
}
}
This will run forever:
public void MethodY()
{
int x = 0;
while (true)
{
x = x + 100000;
}
}
This will eventually crash the app with an OutOfMemoryException:
...
MethodZ("1234567890");
...
private static void MethodZ(string x)
{
while(true)
{
x = x + x;
Console.WriteLine(x.Length.ToString());
}
}
".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
|
|
|
|
|
Only for the record:
In a while loop you can call Application.DoEvents() from time to time. Not really nice but it will not completely freeze the app
|
|
|
|
|
I see a lot of Async/Await examples where an async method returns Task, but I'm confused on some things.
Let's says I create a class to do some processing:
public class Processor
{
public async Task Process()
{
await DoSomething();
}
private async Task DoSomething()
{
var customers = await GetSomeData();
await SaveResults(customers);
}
private async Task> GetSomeData()
{
List customers = null;
await Task.Run(() =>
{
customers = Repository.GetCustomers();
});
}
private async Task SaveResults(List customers)
{
await Task.Run(() =>
{
Repository.SaveCustomers(customers);
});
}
}
I then want to start the processing from a button click in a ViewModel:
public class MainWindowViewModel : _ViewModelBase
{
private ICommand _ProcessCommand;
public ICommand ProcessCommand
{
get
{
if (_ProcessCommand == null)
_ProcessCommand = new RelayCommand(p => ProcessExecuted(), p => ProcessCanExecute());
return _ProcessCommand;
}
}
private bool ProcessCanExecute()
{
return true;
}
private void ProcessExecuted()
{
Processor p = new Processor();
p.Process();
}
}
First Question
In the ViewModel's ProcessExecuted, the call:
p.Proccess
shows a compiler warning
Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
This method is from a ICommand, so do I really want to await this?
Second Question
Next, in the process class, all of the methods are marked as async because if I don't, then I get the same warning. It seems that you have to make every call async along the entire call stack. What I'm hoping to do is offload the Load/Save calls and let the UI keep going.
What is the right way to do this?
Third Question
When my window loads I want to get data for some comboboxes. Is this the right way to do this?
private async Task LoadLists()
{
var statuses = new List<Status>();
var states = new List<State>();
var employees = new List<Employee>();
var loadDataTasks = new Task[]
{
Task.Run(async () => statuses = await LoadStatuses()),
Task.Run(async () => states = await LoadStates()),
Task.Run(async () => employees = await LoadEmployees())
};
try
{
await Task.WhenAll(loadDataTasks);
}
catch (Exception ex)
{
}
finally
{
Statuses = new List(statuses);
States = new List(states);
Employees = new List(employees);
}
}
private async Task> LoadStatuses()
{
return Repository.GetStatuses();
}
private async Task> LoadStates()
{
return Repository.GetStates();
}
private async Task> LoadEmployees()
{
return Repository.GetEmployees();
}
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Return void instead of Task if that's what you intend.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I have the source in java that reads signature data from RSA (erxtracted from APK (Android Package) file) and it works flawlessly but i would like to convert to C# in order to use in my c# application. The issue is i'm getting different data from GetRawCertData() no matter what I have tried.
This is the result:
C#: AQAAADCCBYkwggNxoAMCAQICFQDmX5cziG0zO22ity1a/dKI6FnZyzANBgk...
Java: AQAABY0wggWJMIIDcaADAgECAhUA5l+XM4htMzttorctWv3SiOhZ2cswDQYJKoZIhvcNA...
I'm not familar with signatures, and i'm not even sure what excat format is it but I know it is signed with Java's KeyStore file.
What should I do to get it right? it is very close though. Executing .jar file would be a workaround but I would like to avoid executing something externally
This is my C# code
X509Certificate cert = X509Certificate.CreateFromSignedFile("BNDLTOOL.RSA");
MemoryStream ms = new MemoryStream();
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(1);
byte[] data = cert.GetRawCertData();
bw.Write(data);
bw.Write(data.Length);
byte[] buffer = ms.ToArray();
Debug.WriteLine(Convert.ToBase64String(buffer));
This is Java code from a project that I found online
PKCS7 pkcs7 = new PKCS7(StreamUtil.readBytes(zipFile.getInputStream(ze)));
Certificate[] certs = pkcs7.getCertificates();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.write(certs.length);
for (int i = 0; i < certs.length; i++) {
byte[] data = certs[i].getEncoded();
System.out.printf(" --SignatureHash[%d]: %08x\n", i, Arrays.hashCode(data));
dos.writeInt(data.length);
dos.write(data);
}
byte[] signatures = baos.toByteArray();
System.out.println(Base64.getEncoder().encodeToString(signatures));
modified 1-Jan-22 11:52am.
|
|
|
|
|
This...
mynametaken wrote: %08x
Is not even close to being the same as the following.
mynametaken wrote: ToBase64String
So presumably that is not where you see a difference.
|
|
|
|
|
%08x is not related to it. I have edited the question
|
|
|
|
|
Your statement
bw.Write(1); writes a 4-byte int with value 1, whereas your Java string "AQAABY0w..." starts with a byte of value 1. Maybe what you want is:
bw.Write((byte)1);
Luc Pattyn [My Articles]
The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.
|
|
|
|
|
This one in Java always returned 1
dos.write(certs.length);
So i used
bw.Write(1);
|
|
|
|
|
and when you read the documentation[^] you'll notice that silly Java write method takes an int and writes a byte!!!
Luc Pattyn [My Articles]
The Windows 11 "taskbar" is disgusting. It should be at the left of the screen, with real icons, with text, progress, etc. They downgraded my developer PC to a bloody iPhone.
|
|
|
|
|
I see but when I used
bw.Write((byte)1); , data comes out very different, not even close to Java one
|
|
|
|
|
mynametaken wrote: bw.Write(data);
bw.Write(data.Length);
That is not the same as the following. The order is different.
mynametaken wrote: dos.writeInt(data.length);
dos.write(data);
|
|
|
|
|
Hello everyone, I have a problem.
I have a project on Winform and it works great, now the customer wants to run this project as a Windows service.
The peculiarity of the project, I use a library (presumably written in C ++) that Winform uses, before calling the library I can translate the service into Windows, and I cannot translate the part where the library works. Is there a way to start a service with Winform support? I am considering all the options right up to the use of paid softs.
The system will be in Sign Out mode most of the time.
|
|
|
|
|
Services cannot have any Winforms support: they a=cannot have any interface or methods of interaction with the user.
What you would have to do is start a user process (a Winforms project) that communicates with the service and provides it with instructions.
You can't add Winforms to a service 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 managed to start from the Winform service but it doesn't work when the user is logged out
|
|
|
|
|
Member 12608039 wrote: it doesn't work when the user is logged out
Of course it won't - that's what services are for, and they don't have any user interface at all! Applications aren't running while a user is logged out, but services are.
To have a user interface, you need a user - and services are designer to run with or without a logged in user. Hence, you need a separate Winforms project to communicate with your service when there is a user there, and the service runs "alone" when the user isn't logged in.
"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!
|
|
|
|
|
You need to break out the core functionality into its own library assembly, and then create a windows service project that references that assembly. The windows service project can be compiled as a regular console app to make debugging easier, and you can also make the winform app reference that assembly (also easier to debug).
".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
|
|
|
|
|
Hello,
Please i would like to know how to declare an int from a dynamic given string.
Like this:
for (int i = 500; i >= 350; i--)
{
string combine = "offset_y_bar_" + i;
{
int "combine" = i;
}
}
|
|
|
|