|
Buying or selling?
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
|
|
|
|
|
Really? When you are hungry do you just grunt "food" at anyone?
|
|
|
|
|
Yes. Twice
"Console app" means homework; no one use the console for presentation, only for excercises to cut out complexity.
Don't get me wrong, I all in favor of posting your homework questions here, if you have questions. Just don't expect us to cheat for you.
Also, even with our help and cheating, this would take you some hours of work to bring it together. I try to encourage people to let us help with homework; meaning this site has enough pro's that can point you in a good direction; but we will not do what is assigned to you.
We can point, assist, show documentation, or ask what your code does. But there it stops.
So, from your question; my only reasonable response is you in trouble and not up to the task. That is even with international help, you still screwed.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Drop the course and look for something more in line with your skills and interest.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Good morning. As you may have gathered by the replies you have already had here, people are less than impressed with a question that appears to be asking for someone to give you a working program. In general, the people who answer questions here are professional programmers who earn their living writing code. You wouldn't go up to a plumber and say "leaking pipe in bathroom" and expect them to fix it for you, would you?
Let us put this one down to youthful folly and try to give you a bit of a hint as to what you should be doing. The first thing you have to consider is that your "question" is just a statement. It sketches out the bare minimum of information, namely what the title is and the language/library type behind it is. I can't believe that this is all you got but, if it is, you need to go back to the person who asked you for this and start asking questions.
As I said, the people who answer questions here are, generally, professional programmers. This means that they all approach program development using roughly similar techniques. The first thing that they will all do is look at what the requirements are - this is the starting point for any development - and look to see what they can know as facts in there, and where there are gaps that require assumptions to be made. Where there are assumptions, the professional developer will go back to the origin of the requirements and ask them questions to work out whether those assumptions are correct, or whether something else was meant. Here's a practical example of a requirement that leads to assumptions. First the requirement:
As a Business Owner
I want the customers details saved
So they can be retrieved later on
This is a typical requirement that raises a lot more questions than are answered and, as a developer, the first thing you need to do is identify the questions in there and get answers to them. Here are a list of some of the questions that this requirement pose.
What make up the customer details?
Can the record be saved regardless?
If we need to validate the record before saving, what are the rules?
When we say saved here, what does this mean? If we're saving this into a reporting system on AWS, this gives a different meaning to saving?
So, what are your next steps? Well, dare I say it, your next step is to start acting like a programmer. Write down what you know and what the assumptions are. Seek clarifications. Once you know what all your requirements are, it becomes an easier matter for you to start solving the problem you were presented with. All programming is, is breaking big problems down into small logical problems and then solving those small problems. It requires thought and analytical skills but now is the time to learn how to do this.
|
|
|
|
|
I have a multithreaded application.Each thread results some data which I need to display in chart/graph .
How can I do it? For example if there are 16 threads then there should be 16 small charts on winform in 4 rows and 4 columns. If 17 threads, then another chart added accordingly.
|
|
|
|
|
And?
What have you tried?
What happened when you did?
Where are you stuck?
What help do you need?
"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!
|
|
|
|
|
Member 14663811 wrote: How can I do it? Synchronziation. With the UI thread. Quite some articles on CP cover it. Most will not be using a graph as an example, but the principles remain the same.
Bastard Programmer from Hell
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
You don't need 16 threads; you just need one thread and 16 (concurrent) queues of data points (one for each series / source). The form would have 16 "chart controls" (or more) in a "wrap panel"; one for each queue / source.
If you're talking "millions" of data points, only some commercial software will do.
On the other end, I've used one thread to poll all source devices at intervals; so still only 2 threads in total.
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 have an XML file that each node contains three numbers joined using ','. I want to read them and put them into an integer array (separated by ','). I'm using this code:
for (int i = 0; i <= xmlnode.Count-1; i++)
{
int[] cellcolor = int.Parse(xmlnode[i].ChildNodes.Item(0).InnerText.Trim().Split(','));
}
But, inside the pranthesis has an error which says:
cannot convert from 'string[] to string'
an example of the XML content is:
<?xml version="1.0"?><dataGridView1><Row><CellColor>1,-65536,-65536</CellColor></Row><Row><CellColor>2,-16711872,-16711872</CellColor></Row><Row><CellColor>3,-16711872,-16711872</CellColor></Row><Row><CellColor>4,-16711872,-16711872</CellColor></Row><Row><CellColor>5,-16711872,-16711872</CellColor></Row><Row><CellColor>6,-16711872,-16711872</CellColor></Row><Row><CellColor>7,-16711872,-16711872</CellColor></Row><Row>
How can I solve this problem?
|
|
|
|
|
The String.Split method returns an array of strings. And you cannot pass an array to int.Parse . You first need to capture the array and then convert the numbers individually. Also, you should always use TryParse to convert strings to numbers, in order to avoid exceptions.
|
|
|
|
|
You put the .Split INSIDE the parameter of the Parse call. Split will return a string[] (string array), not a string.
You have to do the split outside of the Parse, then parse each value separately and create your int[] from those values.
Parse will only work on individual strings, not arrays of them.
|
|
|
|
|
|
A Linq based solution might be one line of code:
using System;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
int[] ints = "1,2,3,4,666".Split(',').Select(s => int.Parse(s)).ToArray();
foreach (int i in ints)
{
Console.WriteLine(i);
}
}
}
"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 have a project that needs the ability to modify dozens, hundreds, and potentially thousands of file modifications which are fairly intensive and affect associated resource files as well. To speed things up and maximize the ability to process multiple threads to take advantage of parallel processing I decided to use the System.Threads.Tasks.Parallel class to drive these file changes. There are a few things that I have learned and discovered along the way that I would like to better understand.
First, before I go any further, my project has a BIG need to track all changes in a log file that occur BEFORE they happen to minimize the risk of losing data when something goes wrong. That log file is then parsed for undo actions. This requires the chain of events to be tracked; and logging each change before it happens requires several sub-tasks that use .NET's await feature. A basic picture of the process used to change the files looks something like this:
public class MainFileType
{
internal async void DoSomeMajorChanges(RichTextboxBuilder builder, StreamWriter changeLog)
{
bool result;
await Task.Run(new Action(() => changeLog.LogAction(this))).ConfigureAwait(false);
await Task.Run(new Action(()=> result = coreFile.DoChanges())).ConfigureAwait(false);
builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Red));
foreach (ResourceFile file in this.AssociatedFiles)
{
await Task.Run(new Action(() => changeLog.LogAction(file))).ConfigureAwait(false);
await Task.Run(new Action(() => result |= file.DoChanges())).ConfigureAwait(false);
builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Blue));
}
return result;
}
}
This code is called by a UI that is shown any time a single or multiple files are modified. The UI regularly reports to the user:
public class ChangeManagerUI : Form
{
private bool processed;
object task;
StreamWriter parseableActionLog;
private void OnFormShown(object sender, EventArgs e)
{
if (!processed)
{
processed = true;
MainFileType file;
RichTextboxBuilder builder = null;
List<MainFileType> batch = task as List<MainFileType>;
Refresh();
if (batch != null)
{
RichTextboxBuilder.BeginConcurrentAppendProcess(this, batch.Count);
ReportCaption = "Conversion Progress";
progressBar.Visible = true;
progressBar.Maximum = batch.Count;
ConcurrentOutputManager.ConcurrentMode = true;
Task.Run(() =>
{
Parallel.For(0, batch.Count, i =>
{
file = batch[i];
builder = RichTextboxBuilder.BeginConcurrentAppend(i);
file.DoSomeMajorChanges(builder, parseableActionLog)
RichTextboxBuilder.EndConcurrentAppend(i);
});
});
if (builder != null)
{
builder.AppendNewLine();
builder.BeginColor(Color.Purple);
builder.AppendStyled("Batch Conversion Complete!", FontStyle.Bold);
builder.EndColor();
}
Finalize(false);
}
else
{
file = task as MainFileType;
ReportTextBuilder = new RichTextboxBuilder(this);
Finalize(file.DoSomeMajorChanges(mod, ReportTextBuilder).Result);
}
}
}
}
The things that I have noticed that I would like to understand are as follows:
1. If I remove the Task.Run( (line 24) that encapsulates the Parallel.For statement the program/UI locks up (stops responding) even though I am using ConfigureWait(false) in the await commands... I know that using await in the UI thread can lead to this kind of issue but as I understand, Parallel methods use separate threads.
2. Sometimes Parallel.For seems to run the action for the same item repeatedly rather than go to the next item. Do I need to explicitly code the increment? Because of this, I switched to the Parallel.Foreach method and that works more consistently.
3. I had a problem in that the code AFTER the Task.Run => Parallel.For block was run before the parallel tasks completed (which led to my discovery of 1). I fixed it by using an atomic integer that was initialized to the number of tasks and calling a method that decreased that number inside the Parallel.For each statement that would only run a block of code if the number hit zero. However, I would still like to know why this occurred and if there is another .NET mechanism for achieving the goal executing code after parallel tasks are completed built in.
|
|
|
|
|
Parallel.For doesn't return a Task which can be await ed; it has no choice but to block the current thread until the processing has been completed. By wrapping it in a Task.Run , you're blocking a background thread instead of the UI thread.
The Parallel methods also don't work well with async methods. The delegate you pass in is expected to run synchronously to completion.
You've declared your DoSomeMajorChanges method as async void . Your should avoid async void like the plague:
Avoid async void methods | You’ve Been Haacked[^]
The await Task.Run(...) lines in your DoSomeMajorChanges method serve no purpose. Since there are no other await s in that method, you can simply make it synchronous.
internal bool DoSomeMajorChanges(RichTextboxBuilder builder, StreamWriter changeLog)
{
bool result;
changeLog.LogAction(this);
result = coreFile.DoChanges();
builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Red));
foreach (ResourceFile file in this.AssociatedFiles)
{
changeLog.LogAction(file);
result |= file.DoChanges();
builder.Control.BeginInvoke(new Action() builder.NotifyUser("Some Change Occurred", Color.Blue));
}
return result;
}
The delegate you're passing to Parallel.For is referencing captured variables (file and builder ). This is not thread-safe, which is why you are seeing inconsistent results. Move the variable declarations inside the delegate:
Task.Run(() =>
{
Parallel.For(0, batch.Count, i =>
{
var file = batch[i];
var builder = RichTextboxBuilder.BeginConcurrentAppend(i);
file.DoSomeMajorChanges(builder, parseableActionLog)
RichTextboxBuilder.EndConcurrentAppend(i);
});
}); NB: You will not be able to refer to the last RichTextboxBuilder instance outside of the loop.
It's not clear what your RichTextboxBuilder methods are doing. They could potentially be harming your concurrency.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard. That helps; particularly the info about Parallel itself being syncronous. I knew about the async void issue from my research while solving issues. My real method is Task<bool> (just like yours). Being relatively new to asynchronous code, I just forgot about it when simplifying my code.
Just to clear things up:
So anything inside the Parallel.For/Foreach will run syncronously; except of course if it calls an asynchronous method correct?
|
|
|
|
|
pr1mem0ver wrote: So anything inside the Parallel.For/Foreach will run syncronously; except of course if it calls an asynchronous method correct?
It will run synchronously. If it calls an asynchronous method, the delegate will return and the loop will terminate before the asynchronous method has completed.
There are ways around that, but they tend to cause serious problems:
Should I expose synchronous wrappers for asynchronous methods? | .NET Parallel Programming[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I forgot to thank you for your help back in April. Thanks!
|
|
|
|
|
Multi-tasking "file operations" isn't as great as it sounds. Often the whole thing will run slower due to head and channel contention.
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
|
|
|
|
|
Say for example,
I wanted to send an array of integers from my C++ exe to C# at runtime
|
|
|
|
|
Well I just said it. Now what?
|
|
|
|
|
Did you consider using sockets or (named) pipes? Or some other IPCs?
|
|
|
|
|
i tried using namedpipes it just sends and receives but, My goal is to pass the elements from C++ and get that Elements at runtime in C# and process them. Like, Get n inputs in c++ and process that n elements in C#.
Eg: I am entering 5 inputs, say 1,2,3,4,5 in C++
where c# should get that inputs and process ( eg: inputelement + 1 )
the output would be 2,3,4,5,6.
C++ file
#include <windows.h>
#include <iostream>
HANDLE fileHandle;
int main()
{
fileHandle = CreateFileW(TEXT("\\\\.\\pipe\\pipesample"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
const char* msg = "hello from c++\r\n";
WriteFile(fileHandle, msg, strlen(msg), nullptr, NULL);
}
C# File
static void Main(string[] args)
{
var namedPipeServer = new NamedPipeServerStream("pipesample", PipeDirection.InOut, 1, PipeTransmissionMode.Byte);
var streamReader = new StreamReader(namedPipeServer);
namedPipeServer.WaitForConnection();
Console.WriteLine($"read from pipe client: {streamReader.ReadLine()}");
namedPipeServer.Dispose();
}
modified 23-Apr-21 6:19am.
|
|
|
|
|
I use SQL CE as a database file in my project. I need to load hidden ID numbers into my datatable. What is the command for accessing ID in SQL CE?
|
|
|
|
|