C#

C

C#

We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.

Join

Can I catch an error from Task.Run to stop threads crashing main program?

I have a bot that is meant to support multiple platforms like (Twitch, Discord, Spotify, Etc). I do this by loading them all as .DLL files. I've tried my hardest to make sure that any errors in commands from modules doesn't crash the bot, but the issue is that if the Task Initializing has an error, it would cause the main program to crash because the Try/Catch that starts it doesn't work because it's on a separate thread I assume? Anyway I know I could catch an error in each modules Initializing method to prevent this and to end the task gracefully, but I'd like to make the bot impervious to being crashes by any modules. I don't want to rely on the modules themselves to do it. Is that possible? If so then how?...
No description

Can't assign a method to a list, is there a way to get a list to execute code?

So in this turn based video game I'm trying to develop, thanks to some help from users on this discord earlier, the characters can now be selected and so can the Monsters. My new roadblock now is I want to change the selection menu in the game from a switch statement to a similar selection I have going with hero and monster selection, this will make it so that the command list can be any size I want, where as a switch statement is fixed and hardcoded. Unfortunately I can't wrap my head around trying to put the options from the switch cases into a list. A list only accepts Int's and strings, not methods. So it can't execute code. I'm not sure if I can assign an int nor string to a method either. First image is a visual representation of what I want it to do on a whiteboard, second image is the method, third is the hero selection, fourth is the skills class and the fifth is what the switch statement looks like that I'd like to switch into a foreach. As far as I'm aware array's can't execute methods either so I can't just make it an array instead. Also I'm currently assigning the skills to each character via a set of int's (though it could be strings too). Then I will have the computer read each number to determine if the character has that skill or not. So first number determines if they can attack, next number is defending, third is dodging, etc... This doesn't seem like a good way to do it but I can't think of a better way to assign skills to each hero....
No description

Where did I go wrong in making a loop of this 2D array?

So I spent a while trying to loop through my 2D array and draw my texture for each place in the array, eventually making a 15x15 grid, and I got to it in the end eventually with help but don't understand what I did wrong in my original code?
No description

Loading Form crashing (Windows Form)

So im doing a pdf viewver using winform. I wanted to add a form that indicate the progression when loading the pdf pages. The problem is that the application crash during the for loop, and the loading form is not showing up before and its showing at the end of the loop but this is useless since the content is already loaded and it close immediately. I've tried many thing like using Task.Run but i cannot fix it. (pls help)

Connecting Lines in 3D space

Hey, I'm fairly a newbie to coding in general and first time asking for help on a forum so hopefully its a decent enough explanation. I have a list of Lines, and there will always be two outer vertical lines, which I can get easily enough. I'm now trying to create two lists of the upper lines and the lower lines. As shown in the images in blue/red. Any ideas on how I can best achieve this?...
No description

Apache dying on system cron job, but not everytime

Hello, i'm facing an issue with my apache 2 server crashing sometimes at 6 am due to a system cron, running at 6am to apt-upgrade, somestimes the apache2.service just cannot restart not much of the reason behind
No description

Pack URIs do my bloody head in - help needed

See image for setup. I'm getting a file not found on GreyscaleEffect.ps. As far as i can see and following the MS guidance, this path should be right. GrayscaleEffect.ps is set to Resource so should be being built into the assembly. Bane of my life these bloody Pack URIs!
No description

There was an error exporting the HTTPS developer certificate to a file

Hello everyone, I am facing the following issue with a brand new project (ASP.Net, SQL server and Angular: The only change that I ever did on the project was the connection to the SQl which worked. What I did so far was: dotnet dev-certs https --clean...
No description

How can I resolve this problem?

Hello Everyone, I am .NET learner. In the middle of one project, I have faced a problem and I can't resolve this alone so I post this. I can't run this project.I don't know this is right but in my thought it is because of .NET version. The project is using .NET 6.0 version but I installed .NET 8.0 in my laptop....

Entity navigation modelling so I can understand it better

I have 7 entities but i need help to configure the 3 entities so i can understand how it works better
No description

Custom msbuild task

hey i wrote a custom msbuild task and created a nuget for it. in the project Im using the task it seems to work, but in the CI it doesnt. i can reproduce the same error when cleaning my nuget folder locally. `[MSB4036] The "..." task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with <UsingTas...

/reference flag in C# compiler

What is the use of this flag?

Why does this not print the firstname and lastname

Yo, im trying to make a login function in c$. Logging in works but it does not want to print the first and last name it only prints welcome back. How come?
No description

Trying to get started with threads but UI thread giving cross thread exceptions?

I have a thread that runs infinitely and what i believe is happening is that the new thread means events run on it leading to cross threading exceptions but i dont understand why that thread cant run in the background and events stay running on the main ui thread? This is the test i am using for my new thread ``` private void CalculateEngineOutput() //Calculate some of the main outputs...

Metro MahApps checkbox content change color when hovered.

I have a checkbox that will change the color of the content inside. As the gif shows, you can see that, the content does change color, however, the color is also changed when I hover the mouse over it, I try to find a way to disable the hover functionality but it seems to be no use at all. If anyone knows anything or could give me some suggestions, i'd be very grateful
No description

Simple help in httpclient c#

I started to learn C# recently. I tried to do on postman and it was successfull. i wanna implement that feature with httpclient c#, but i face an issue. Who can help me?...

C# and native interop with JNI

So I've been tinkering with this personal project of mine to try to get C# to interop with Java using JNI (Java Native Interface). This project relies heavily on unmanaged wrappers for JNI calls that I call later using C# and DllInterop. Here is my issue, After the native call to JNI_CreateJavaVM() and returning the pointers to the JVM and ENV (pointer to native method interface) back to the C# managing object everything appears fine and working as expected. JVM initialized, I've got pointers to the important structures to create Java classes on hand and ready, but when i decide to utilize the jvm pointer in a very simple case , to destroy it, I'm getting system AccessViolation Exception. At first i thought this was happening due to a silly mistake on my end where i wasn't allocating the structs on the heap, so when i exit the native function call the memory addresses returned are invalid, but I am now using malloc() which should store the structs on the heap and I received the same error. Is there something I'm missing when passing void pointers in C# to native function calls? Please let me know if im not very clear, I'm self taught and I'm not the best at conveying information. I can share code if it'll help....

Copy file to build directory

How would I make it so a file I added to the program files would get copied to the build directory when the program is built?

How to prevent nulls using this approach for Object Initializers?

In C# I have this class: ```cs using System.Diagnostics.CodeAnalysis; using System.Net.Mail; ...

What type of 3 layered architecture should I use

I'm building a project with the following techstack: ASP.NET EF Core SQL Server React...
No description