|
|
Kindly understand that I really can't figure out how to get this neural network concept to work as code. Anyway, has anyone figured out how to create the C# source code implementation for this?
https://imgur.com/qN1QLHe
|
|
|
|
|
|
Probably yes.
But ... while we are more than willing to help those that are stuck: that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"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 tried to simulate a process in which by pressing a button, some calculations take place (I used a tutorial). Now, I want to test and learn how I can manipulate GUI from an another thread. I decided to change button1 text. I added its code to DoWork section. As I expected, it throws cross-thread exception. Please write me a code and use Invoke and Delegate to see how I can access GUI. I want to learn from your code. Thanks.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
button1.Text = "processing";
int sum = 0;
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
sum = sum + i;
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
e.Result = sum;
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = e.ProgressPercentage + "%";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
label1.Text = "Process canceled";
}
else if (e.Error != null)
{
label1.Text = e.Error.Message;
}
else
{
label1.Text = "Result: " + e.Result.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
if (!backgroundWorker1.IsBusy)
{
backgroundWorker1.RunWorkerAsync();
}
else
{
MessageBox.Show("Busy processing!. Please wait...");
}
}
private void button2_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
{
backgroundWorker1.CancelAsync();
}
else
{
label1.Text = "No operation is running to cancel.";
}
}
}
modified 11-Feb-21 14:05pm.
|
|
|
|
|
UWP, WPF or Windows Forms?
Seems optimistic to "report progress" when "cancelling" ... that's RunWorkerCompleted territory.
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
|
|
|
|
|
|
Consider that I have to keep order of my operations, so
button1.Text = "processing" needs to be in DoWork section.
|
|
|
|
|
No, you don't.
UI stuff like that should happen outside the BackgroundWorker, like right before you start it on its way.
|
|
|
|
|
1) Add reference to WindowsBase.dll
2) Add: using System.Windows.Threading;
3) Add delegate declaration OUTSIDE of method:
delegate void ProgressDelegate( int pct );
4) Replace (in this case):
backgroundWorker1.ReportProgress(i);
with:
ProgressDelegate progressDelegate = delegate ( int pct ) { this.progressBar1.Value = pct; };
System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Normal, progressDelegate, percentComplete );
Then it's just a matter of tailoring for various UI controls.
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
|
|
|
|
|
Isn't that the same question that you ask here :
Re: BackGroundWorker gives runtime error - C# Discussion Boards[^]
Did you give those links, I gave to you at that question, a try ?
If not - why not ?
If yes - where do you stuck ?
To Clarify :
Invoke means that you give an order to do something outside your Thread or Worker which originally happen inside the UI-Thread.
|
|
|
|
|
That basically is the same question once more. The answer hasn't changed.
You are wasting your and our time by repeating your question and ignoring the answers you've got.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
Luc Pattyn wrote: No, Master Luke.
(Ghhh.. I am your father, ghhhh)
Luc Pattyn wrote: You are wasting your and our time He has the right to do so; to be ignorant. There's always those looking for easy answers, unwilling to learn. This site is easy money for some. I learned a lot from you, mostly that I should be patient.
Want to tell him he's wasting money? This is voluntary, remember? If he insist on failing, I'd for one gladly help.
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.
|
|
|
|
|
I'm not wasting your time. I couldn't find out how to use the procedure in my own situation.
For example, based on the explanations in Towards Cleaner Code II, a C# GUI Invoke/Async Helper [^] I wrote the following code:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (button1.InvokeRequired)
{
button1.Invoke(delegate { updatetext("Processing"); });
}
else
{
updatetext("Processing");
}
int sum = 0;
for (int i = 0; i <= 100; i++)
{
Thread.Sleep(100);
sum = sum + i;
backgroundWorker1.ReportProgress(i);
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
e.Result = sum;
}
private void updatetext(string txt)
{
button1.Text = txt;
}
But,
button1.Invoke(delegate { updatetext("Processing"); }); says the method is not a delegate type and cannot be compiled.
modified 12-Feb-21 0:54am.
|
|
|
|
|
I found my own solution:
button1.Invoke((Action)delegate { updatetext("Processing"); });
or
button1.Invoke((MethodInvoker)delegate { updatetext("Processing"); });
|
|
|
|
|
Message Closed
modified 13-Feb-21 8:00am.
|
|
|
|
|
|
Good morning I have a problem that is making me desperate. I want to deserelize a Json but the content is always null. I pass you my code, if you could give me a hand I would appreciate it very much.
Regards and thank you very much in advance.
CLASS
public class RootContactos
{
public string contacto { get; set; }
public string email { get; set; }
public string telefono { get; set; }
public string notas { get; set; }
}
CODE
contac = JsonConvert.DeserializeObject<rootcontactos>(strJson);
JSON
{"20150630140000#1":{"contacto":"PATRICIA","telefono":"976185823","email":"","notas":""},"20150630140001#1":{"contacto":"FAX","telefono":"976180364","email":"","notas":""}}
|
|
|
|
|
Your JSON does not match the class, which is probably why you get a problem. If I run your JSON through a class creator (Convert JSON to C# Classes Online - Json2CSharp Toolkit[^]) then what I get is three classes:
public class _201506301400001 {
public string contacto { get; set; }
public string telefono { get; set; }
public string email { get; set; }
public string notas { get; set; }
}
public class _201506301400011 {
public string contacto { get; set; }
public string telefono { get; set; }
public string email { get; set; }
public string notas { get; set; }
}
public class Root {
[JsonProperty("20150630140000#1")]
public _201506301400001 _201506301400001 { get; set; }
[JsonProperty("20150630140001#1")]
public _201506301400011 _201506301400011 { get; set; }
} Which is what I would expect from that: two different classes with identical content but different names!
I'd start by looking at the data source - I'd expect an array of JSON data containing two elements, not two individual items.
"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!
|
|
|
|
|
Thank you very much for answering. The classes that he proposes to me, I cannot perform them because the "header" (_201506301400001) varies in each record of the database.
I have been testing and I have managed to do the deserilization in this way:
var result = JsonConvert.DeserializeObject <dynamic> (strJson);
But then I have not gotten the information from resutl.
Regards and thank you very much in advance.
|
|
|
|
|
Deserialize it into a Dictionary.
void Main()
{
var json =@"{""20150630140000#1"":{""contacto"":""PATRICIA"",""telefono"":""976185823"",""email"":"""",""notas"":""""},""20150630140001#1"":{""contacto"":""FAX"",""telefono"":""976180364"",""email"":"""",""notas"":""""}}";
<pre>
var contacDict = JsonConvert.DeserializeObject<IDictionary<string,RootContactos>>(json);
contacDict.Values.First().Dump();
contacDict["20150630140001#1"].Dump();
}
public class RootContactos
{
public string contacto { get; set; }
public string email { get; set; }
public string telefono { get; set; }
public string notas { get; set; }
}</pre>
Truth,
James
|
|
|
|
|
Thank you very much it has worked perfectly.
Regards, and thank you very much.
|
|
|
|
|
I'm working on Devexpress Spreadsheet application. My calculation are as below:
1. Data is read from a sheet into two Lists. One list is string type and another is integer type. String type list consists of values of 4 cells in each row.
2. Lists are put into a dictionary. Strings are keys and integers are values.
3. Values of the same keys are summed.
4. The mentioned two lists are cleared and fill by dictionary keys and values.
5. Dictionary keys and values are split into cells in a new sheet.
I want to show progress animation (comes with DevExpress tools) when calculations begin. I used following code:
<pre lang="C#"> private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
IWorkbook workbook = spreadsheetControl.Document;
Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
CellRange range = worksheet.GetDataRange();
int LastRow = range.BottomRowIndex;
var keys = new List<string>();
var values = new List<int>();
progressPanel1.Visible = true;
for (int i = 0; i < LastRow + 1; i++)
{
if (worksheet.Cells[i, 10].DisplayText == "خاتمه یافته")
{
keys.Add(string.Join(",", worksheet.Cells[i, 28].DisplayText, worksheet.Cells[i, 0].DisplayText, worksheet.Cells[i, 9].DisplayText,
worksheet.Cells[i, 15].DisplayText, worksheet.Cells[i, 31].DisplayText));
values.Add((int)worksheet.Cells[i, 32].Value.NumericValue);
}
}
var mydic = new Dictionary<string, int>();
for (int i = 0; i < keys.Count; i++)
{
if (mydic.ContainsKey(keys[i]))
{
mydic[keys[i]] += values[i];
}
else
{
mydic.Add(keys[i], values[i]);
}
}
if (worksheet.HasData)
{
if (workbook.Worksheets.Contains("Summarized") == false)
{
workbook.Worksheets.Add().Name = "Summarized";
}
Worksheet ws_summarized = workbook.Worksheets["Summarized"];
ws_summarized.Clear(workbook.Worksheets["Summarized"].GetUsedRange());
keys.Clear();
values.Clear();
foreach (var item in mydic.Keys)
{
keys.Add(item);
}
foreach (var item in mydic.Values)
{
values.Add(item);
}
for (int i = 0; i < mydic.Count; i++)
{
string text = keys[i];
string[] rewrite = text.Split(',');
workbook.Worksheets["Summarized"].Cells[i, 0].SetValue(rewrite[0]);
workbook.Worksheets["Summarized"].Cells[i, 1].SetValue(rewrite[1]);
workbook.Worksheets["Summarized"].Cells[i, 2].SetValue(rewrite[2]);
workbook.Worksheets["Summarized"].Cells[i, 3].SetValue(rewrite[3]);
workbook.Worksheets["Summarized"].Cells[i, 4].SetValue(rewrite[4]);
}
for (int i = 0; i < mydic.Count; i++)
{
int text = values[i];
workbook.Worksheets["Summarized"].Cells[i, 5].SetValue(text);
}
}
else
{
MessageBox.Show("خطای داده ورودی", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
When I run and press the dedicated button, the following runtime error occurs:
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'spreadsheetFormulaBarPanel' accessed from a thread other than the thread it was created on.'
The error in in line
progressPanel1.Visible = true;
I tried to delete that line and check whether it works. This time the following runtime error occurred:
System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.'
This error occurs in line
backgroundWorker1.RunWorkerAsync();
PLEASE HELP ME.
|
|
|
|
|