Windows Service doesn't read settings.setting? - c#

I have a Setting.setting have a setting option : isSent - type int - value : 0
And my code :
if (Settings.Default.isSent =! 0)
{
var info = _text.ReadFile(Settings.Default.FilePath);
//Do something
} else
{
//Do something
}
I install my Windows Service, start and attach it to Visual to debug, and it always run to
var info = _text.ReadFile(Settings.Default.FilePath);
But not run to
} else
{
//Do something
}
like I expect :(
Anyone can explain it and know how to solve this for me ? Appreciate any answer :)

I just noticed "if (Settings.Default.isSent =! 0)". Should it be "if (Settings.Default.isSent != 0)". Otherwise visual studio will always report error.
by the way, have you tried to attach your source code to the Service and see the value of "Settings.Default.isSent" at runtime?

Related

Why is try catch sometimes ignored?

One example is this code:
try
{
string domain = o.SelectToken("response[" + i + "].domain").ToString();
...
}
catch(Exception)
{
continue;
}
Instead of just going on in the loop("continue"), vs halts and points at string domain = o.SelectToken("response[" + i + "].domain").ToString(); for an System.IndexOutOfRangeException.
Why is that?
You probably have 'break on all exceptions' selected in the Debug>Windows>Exception settings:
https://learn.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger?view=vs-2019
Unselecting this will let VS proceed.
You can do it by using two ways.
As suggested in MSDN, is to set it up in your Visual Studio (I believe it's 2019)
Debug > Windows > Exception Settings: Search for index and untick.
Please add exception to handle exception in your code..
catch(IndexOutOfRangeException e)
{
// handle it like logging it in file and continue
continue;
}
catch(Exception)
{
continue;
}

net 4.0 page validator undefined

Web form with .Net validators works perfectly on the development server.
On the production server, .Net seems to fail to generate client side script 'Page_ClientValidate' and browser throws 'Page_ClientValidate is undefined'
Can't seem to figure out what the issue is? Is there a setting in IIS or server level to fix the issue?
Please help, have been trying to resolve it for days with no luck.
function ValidateForm() {
var validForm = true;
Page_ClientValidate("formWrapperValidationGroup");
validForm = validForm && Page_IsValid;
if (validForm) {
$("#formWrapper .fieldSubmitButton input[type=submit]").css("display", "none");
$("#formWrapper .fieldSubmitButton .fieldSubmitButtonStatus").css("display", "inline-block");
}
return validForm;
}
Try this:
$('#Form1').submit(function () {
if (typeof (Page_ClientValidate) == 'function') {
Page_ClientValidate();
} else {
$(this).valid();
}
});
js.axd?path=%2fWebResource.axd - Redirect rule caused an issue with resolving the js.axd file path. Fixed it b fixing the redirect rule.

Cosmos custom OS, addmapping?

I am new to C# and is currently using COSMOS to make a simple FileSystem for my OS class. Currently I'm trying to implement a "reformat" function that, when the word "reformat" is typed into the console, the OS (emulated via QEMU), partitions the disk. Currently this is my code:
public static void console()
{
while (true)
{
Console.WriteLine("Console: ");
String input = Console.ReadLine();
if (input == "exit")
{
Cosmos.Sys.Deboot.ShutDown();
}
else if (input == "cpumem")
{
Console.WriteLine(Cosmos.Kernel.CPU.AmountOfMemory.ToString());
}
else if (input == "restart")
{
Cosmos.Sys.Deboot.Reboot();
}
else if (input == "devices")
{
var devices = Cosmos.Sys.FileSystem.Disk.Devices.ToArray();
}
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
}
}
Most important is this bit:
else if (input == "reformat")
{
try
{
Partition part = null;
for (int j = 0; j < Cosmos.Hardware.BlockDevice.Devices.Count; j++)
{
if (Cosmos.Hardware.BlockDevice.Devices[j] is Partition)
{
part = (Partition)Cosmos.Hardware.BlockDevice.Devices[j];
}
}
var fs = new Cosmos.Sys.FileSystem.FAT32.FAT32(part);
uint cluster = 100;
fs.Format("newCluster", cluster);
}
catch
{
//Do Something warn user.
}
}
Which is analogous to what is located here: http://cosmos-tutorials.webs.com/atafat.html
However, when I run it, I get this error:
I believe this is because I lack this line:
Cosmos.System.Filesystem.FileSystem.AddMapping("C", FATFS);
FATFileList = FATFS.GetRoot();
Located in the link above. Is there any other way to map? Or am I missing something completely? The COSMOS documentation doesn't really tell much, the source code is honestly confusing for a beginner like me as it has no comments whatsoever on how the functions work or what they do. I am using an older version of COSMOS (Milestone 4) as it's the only one that works for Visual Studio C# 2008. Newer versions run only in Visual Studio C# 2010.
Ah, I recognize this... had to debug a similar situation on a Cosmos project I'm working on myself (I'm using the VS2010-compatible Cosmos but the same situation might apply to older versions as well...)
This can happen if you try to call a method on a null object. Type 0x........, Method 0x........ is specifically mentioning the location in the compiled code where the call failed. "Not FOUND!" means that the method it is looking for cannot be found, presumably because you called it on a null reference.
I'm testing with VirtualBox myself, and found that if you're using a brand-new blank hard disk image, there will be no Partitions on it. Thus, the condition will never get satisfied, your Partition will never get set and then Cosmos will try to execute a method on the null Partition!
Look closely at how you set the Partition (it's initialized to null). For starters I would print a simple message each time the "if (block device is partition)" condition is satisfied... I would be willing to bet it will never print.
Hope this helps... I am still learning about Cosmos and custom kernels myself but fixing the null reference in my case solved my occurrence of the problem. If that's the problem, then the next step, of course, is figuring out why you're not getting any Partitions in the first place...
The rest of your code looks fine but I am not sure how you implemented the rest of your classes. Kernel debugging can be a nightmare, good luck to you!

Duplicate process (strange issue)

I am trying to prevent opening help file more than once.
This is the method I am using:
public void openHelp()
{
int count = 0;
string helpPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + #"\MyApp\Help\eHelp.chm";
System.Diagnostics.Process[] helpProcs = System.Diagnostics.Process.GetProcesses();
foreach (System.Diagnostics.Process proc in helpProcs)
{
if (proc.MainWindowTitle == "Sample App Help")
{
count++;
}
}
if (count == 0)
{
System.Diagnostics.Process.Start(helpPath);
}
else
{
}
}
The idea is, if you find the process with the same MainWindowTitle, then do not start a new one.
However, this is not reliable. In some cases it still starts the process, even though one is already running. Is there an issue with a logic?
Thank you.
P.S. Of course the MainWindowTitle is "Sample App Help", at least that is what I see while debugging.
Update:
Issue only occurs when user has minimised help file. So I suspect something happens in the system and I need to check something. Any suggestions?
The Remarks section in Process.MainWindowTitle contains the following note:
The main window is the window that currently has the focus; note that
this might not be the primary window for the process. You must use the
Refresh method to refresh the Process object to get the current main
window handle if it has changed.
Could this perhaps be the cause of your problem?
What about keeping the process id of a newly started help viewer and before starting another one, just check if the old one is still alive.
int id = ...
try
{
var proc = Process.GetProcessById(id);
}
catch
{
// no process running with that id
}

C# Unreachable code detected

I'm getting a "Unreachable code detected" message in Visual Studio at the point i++ in my code below. Can you spot what I've done wrong?
try
{
RegistryKey OurKey = Registry.CurrentUser;
OurKey.CreateSubKey("Software\\Resources\\Shared");
OurKey = OurKey.OpenSubKey("Software\\Resources\\Shared", true);
for (int i = 0; i < cmbPaths.Items.Count; i++) //<---- problem with i
{
OurKey.SetValue("paths" + i, cmbPaths.Items[i]);
break;
}
}
The problem is that this actually isn't a loop. You don't have any condition on the break so you could equivalently write something like
if(cmbPath.Items.Count > 0)
{
OurKey.SetValue("paths" + 0, cmbPaths.Items[0]);
}
Alternatively you have to correct with something like
for (int i = 0; i < cmbPaths.Items.Count; i++)
{
OurKey.SetValue("paths" + i, cmbPaths.Items[i]);
if(someConditionHolds)
break;
}
You're breaking out of the loop before the end of the first iteration.
The problem is that because you break; in the loop with no chance of it doing anything else, the increment of i (i++) will never be reached.
Although your problem is solved i need to tell you this,
you can just using the CreateSubKey() method for your purpose. I think It's a better choice.
:)
//Creates a new subkey or opens an existing subkey for write access.
var ourKey = Registry.CurrentUser.CreateSubKey("Software\\Resources\\Shared");
You can also end up getting unreachable code if you use say for example Entity Framework, and you didn't add that reference to that project.
Say you have several projects like A Data Layer Project, a Domain Classes, then you create a console app for testing or whatever and you reference where your dbcontext is at, but if you don't use say nuget and add in EF, you will get code unreachable when trying to write a loop etc...

Categories

Resources