|
I will suggest you put a breakpoint on code line below and step through....
buttonTransfer.Enabled = true;
Your code may not be getting to that point as you assumed. If it gets to that point and your button is a physical object on your form, it must work.
|
|
|
|
|
Three comments:
1.
You should have told us where the code shown is sitting: in some event handler? in a BackGroundWorker's DoWork handler? where?
I can only hope it is not inside a DataReceived handler...
2.
Immediately displaying everything that ReadLine returns would probably be sufficient to find out what goes wrong and when.
3.
A simple "x" will not stop the loop, as ReadLine always waits for a line terminator (actually SerialPort.NewLine, which by default is said to equal Environment.NewLine). Is your peripheral sending "x\n" or "x\n\r" or some such?
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.
modified 12-Apr-22 13:15pm.
|
|
|
|
|
All our applications are Web applications that depend heavily on Web Forms.
We have very serious challenge moving our applications to Blazor (Razor Pages). We used Oledb objects and data tables a lot and migrating is a big issue. No web forms in latest visual studio versions.
Does anyone have a quick fix? I will appreciate your help.
|
|
|
|
|
Raphael Adeniji wrote: No web forms in latest visual studio versions. Which version(s) are you referring to? They are definitely available in VS2019.
|
|
|
|
|
I meant Dot Net Core, they are surely available in Frameworks, but not in Dot Net Core.
|
|
|
|
|
|
I am working with SharpCompress and creating a parser that creates objects mimicking real folder structures to use in various browsable controls. Since the IEntry and IArchiveEntry interfaces in SharpCompress have all the makings of an "info" class, I have created two custom Info classes derived from System.IO.FileSystemInfo. Because most of the properties are NOT virtual, I have to use the new keyword to override them so that I can grab the info from the Entry object instead of the file system.
I want to use the base class FileSystemInfo to supply much of the data for my virtual file system object class. However, I am not sure if using the FileSystemInfo object will work with the derived classes that hide the original properties. Check the relevant part of my code below:
public class IOObjectNode : TreeNode;
{
FileSystemInfo info;
FileInfo file;
DirectoryInfo folder;
ArchiveDirectoryInfo virtualFolder;
ArchiveFileInfo virtualFile;
IEntry entry;
public string FileName { get { return info.Name; } }
public string Path { get { return System.IO.Path.GetDirectoryName(info.FullName); } }
public string FullName { get { return info.FullName; } }
public DateTime LastWriteTime { get { return info.LastWriteTime; } }
public DateTime CreationTime { get { return info.CreationTime; } }
public long Size { get { if (file != null) return file.Length; else return 0; } }
public long Length { get { return Size; } }
public IOObjectNode() { }
public void Populate(IEntry entry) { SharpEntry = entry; }
public void Populate(FileSystemInfo info) { ShellInfo = info; }
public FileSystemInfo ShellInfo
{
get { return info; }
protected set
{
info = value;
file = value as FileInfo;
folder = value as DirectoryInfo;
virtualFolder = value as ArchiveDirectoryInfo;
virtualFile = value as ArchiveFileInfo;
UpdateResources();
}
}
public IEntry SharpEntry
{
get { return entry; }
protected set
{
entry = value;
UpdateResources();
}
}
}
Notice my use of "info" in some of the properties above. Will this work with my custom class instances when info is the base class?
|
|
|
|
|
Nevermind. Found the answer myself with a few test classes:
public class CoreClass
{
public int Value { get { return 1; } }
}
public class ClassA : CoreClass
{
public new int Value { get { return 2; } }
}
public class ClassB : CoreClass
{
public new int Value { get { return 3; } }
}
public class ClassC : CoreClass
{
public new int Value { get { return 4; } }
}
public class ClassD : CoreClass
{
public new int Value { get { return 5; } }
}
public class ContainerClass
{
public CoreClass CoreObject { get; set; }
public int CoreValue { get { return CoreObject.Value; } }
public ContainerClass(CoreClass instance) { CoreObject = instance; }
}
static void Main(string[] args)
{
ContainerClass a = new ContainerClass(new ClassA());
ContainerClass b = new ContainerClass(new ClassB());
ContainerClass c = new ContainerClass(new ClassC());
ContainerClass d = new ContainerClass(new ClassD());
int av = a.CoreValue;
int bv = b.CoreValue;
int cv = c.CoreValue;
int dv = d.CoreValue;
}
Bummer... all the int values in Main were 1. So looks like it calls the base class method and not the derived one. Looks like I will need to try a different approach.
|
|
|
|
|
|
Well you can't override non-virtual/abstract methods or properties and I can't change Microsoft's base classes. Pretty annoying because it would be a simple solution to a now a more complex problem. I am guessing I will have to make a wrapper.
|
|
|
|
|
When I pushed the Transfer button a second time, I get the this error message, System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.' Can someone help me with this problem?
private void buttonTransfer_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.CancelAsync();
MessageBox.Show("Running!");
}
richTextBoxViewStatus.Clear();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
port.Close();
backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
do some work
}
|
|
|
|
|
|
on top of what Richard said, even when your DoWork checks for cancel requests, calling CancelAsync will not immediately cancel the ongoing action, so you risk issuing a new RunWorkerAsync before the cancelling has happened (resulting in an error like the one you got) unless you implement a synchronization mechanism. There is a reason why the method is called CancelAsync .
If you don't mind some potential overlap of two DoWork executions, the easiest way could be to simply create a new BackGroundWorker every time you have a background job.
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.
|
|
|
|
|
You're running .RunWorkAsync() regardless of .IsBusy() on button click.
CancelAsync() implies .... async.
"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
|
|
|
|
|
Quote: IDE: Integrated development environment
C#, Visual Studio 2019, Windows10Pro in
2.1
Exception: An object reference does not point to an instance of an object.
Quote: using System;
using System.IO;
using System.Text;
// - using System.Text.StringBuilder;
using System.Globalization;
using System.Windows.Forms;
using System.Threading;
// using Microsoft.CSharp;
namespace WFA_BankPercent
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
//- string m_sWork44 = ""; // C# требует инициализации
private void Form7_Load(object sender, EventArgs e)
{
}
// below on forum www.codeproject.com 29032022
private void button21_Click(object sender, EventArgs e)
{
string line = System.String.Empty;
string s_sourse = System.String.Empty;
bool b_beforeRead = false;
bool b_afterRead = false;
string s_begin = "Begin";
string s_end = "End";
bool b_begin = false;
bool b_end = false;
int i_begin = 0;
int i_end = 0;
try
{
StreamReader sr = new StreamReader(@"D:\Input_my.txt");
StreamWriter sw = new StreamWriter("D:\\Out_my.txt", false, Encoding.UTF8);
line = sr.ReadLine();
byte k = 0; // temporary for debug
while (line != null)
{
// k++; // temporary
// //+if (k > 6) // read only the first 6 lines of the input file
////+ if (k > 17) // 15 lines were written to the output file, but there was no Exception
// if (k > 18) // 0 lines were written to the output file, Exception was
// goto Close_my;
if ((line.Trim().Length == 0))
goto label_read;
b_begin = line.Contains(s_begin);
b_end = line.Contains(s_end);
if (b_begin && b_end)
{
i_begin = line.IndexOf(s_begin);
i_end = line.IndexOf(s_end);
s_sourse = line.Substring(0, i_begin);
MessageBox.Show("in line found 'Begin' and 'End'\n" + line);
goto label_wr;
}
else
{
s_sourse = line;
goto label_wr;
}
goto label_read;
label_wr:
sw.WriteLine(s_sourse);
label_read:
b_beforeRead = line.Contains(s_begin);
line = sr.ReadLine();
b_afterRead = line.Contains(s_end);
} // end while (line != null)
Close_my:
line = sr.ReadLine();
MessageBox.Show("Does it get here?" + s_sourse);
sr.Close();
sw.Close();
} // end try
catch (Exception e1)
{
MessageBox.Show("2.1 Exception: " + e1.Message);
}
finally
{
MessageBox.Show("3.Executing finally block.");
}
} // end button21
}
}
|
|
|
|
|
|
 As I said last time you asked this:
Quote: This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.
Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterday's shirt when you took it off last night.
We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!
Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.
But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
Use the debugger. We can't do that for you!
"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!
|
|
|
|
|
label_read:
b_beforeRead = line.Contains(s_begin);
line = sr.ReadLine();
b_afterRead = line.Contains(s_end);
In the above code, if ReadLine hits the end of the input file then line will be null . But this code is so badly constructed it needs a complete rework.
|
|
|
|
|
...which JSOP wrote for him and he won't use it...
I smell a help vampire in the making.
|
|
|
|
|
Dave Kreskowiak wrote: help vampire My tech vocabulary just grew.
Software Zen: delete this;
|
|
|
|
|
We've had a few of them over the years.
|
|
|
|
|
Yes, I tested your code and saw the error. I do not think writing from one text file to another should require the volume of codes I saw, I may be wrong, even when you need to filter some lines.
If I know precisely what you want to do, I can help you, I did something similar before.
You may give detailed explanation of what you want to achieve.
|
|
|
|
|
//Try this procedure and manipulate it
public void RewriteFile(string SourceFileFullName, string TargetFileFullName, string UnWantedFilterString = "")
{
//UnWantedFilterString variable allows a line to be eliminated in the target file if it is contained in the line, this can be fine-tuned depending on your need.
//SourceFileFullName and TargetFileFullName should be full file path and name
if (!File.Exists(SourceFileFullName))
{
MessageBox.Show("Source File Does Not Exist, Confirm on the Disk.");
return;
}
string CurrentLine = "";
string CurrentLineTrimedUcase = "";
string UnWantedFilterUpperTrimed = UnWantedFilterString.Trim().ToUpper(); //This is string of anything you do not want to eliminate a line
try
{
StringBuilder sb2 = new StringBuilder("");
using (StreamReader sr = File.OpenText(SourceFileFullName))
{
while ((CurrentLine = sr.ReadLine()) != null)
{
CurrentLineTrimedUcase = CurrentLine.Trim().ToUpper().Replace("\t", ""); //Avoid tabs, trim and change to upper case
if (UnWantedFilterUpperTrimed == "") //Test for unwated Filter string
{
sb2.AppendLine(CurrentLine); // Append line string to StringBuilder if unwanted filter empty
}
else
{
if (CurrentLineTrimedUcase.IndexOf(UnWantedFilterUpperTrimed) < 0)
{
sb2.AppendLine(CurrentLine); // Only Append line string to StringBuilder if unwanted filter not in string
}
}
}
}
if (sb2.Length == 0)//Empty file will not be written
{
MessageBox.Show("Empty File will not be re-written");
return;
}
//Delete target file if it exists
if (File.Exists(TargetFileFullName))
{
File.Delete(TargetFileFullName);
}
//Write file now
using (StreamWriter sw = File.CreateText(TargetFileFullName))
{
sw.Write(sb2);
}
MessageBox.Show("File Written Successfully");
return;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + " --- File Not Written");
}
}
|
|
|
|
|
I'm trying to write an app that allowed me to copy data from an Android app to a C# app via USB. I will probably write the data to a json file and copy the file to my C# app, either by sending it from Android or copying it from C#.
I found this api but it looks old and I can't find any documentation.
Does anyone know how I can send or copy a file from Android app to a C# app via USB?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 31-Mar-22 1:02am.
|
|
|
|
|
If you connect the Android device to a PC via USB then it should appear as a new disk drive. A quick test with your device should confirm that and you can then see whether you can access the files.
|
|
|
|