|
Another shortcut ctrl + alt + L
Do not go where the path may lead. Go instead where there is no path and leave a trail.
|
|
|
|
|
Click the drop down at the end of the Standard toolbar, and select "Add or Remove Buttons". It's in there.
|
|
|
|
|
Is there any need to use Visual Studio 2012, as there are some features that you won't find in VS 2012.
|
|
|
|
|
Member 10036172 wrote: Is there any need to use Visual Studio 2012, as there are some features that you won't find in VS 2012.
Like what?
Plus, this question has already been answered so your comment isn't particularly helpful.
|
|
|
|
|
I've come across with this progress bar using worker.ReportProgress(). I was hoping to get a real-time progress Bar value from outside Write_Tag method. In this code below, it does seem to have some 1-2 seconds delay before showing the progress bar status.
Any suggesion?
private void InitializeProgressBars()
{
for (int i = 0; i < DevicePath.Count; i++)
{
progressBar[i] = new ProgressBar();
progressBar[i].Location = new Point(3, 16);
progressBar[i].Size = new Size(605, 31);
progressBar[i].Maximum = 100;
}
gbProgressStatus0.Controls.Add(progressBar[0]);
gbProgressStatus1.Controls.Add(progressBar[1]);
gbProgressStatus2.Controls.Add(progressBar[2]);
gbProgressStatus3.Controls.Add(progressBar[3]);
}
private void InitializeBackgroundWorkers()
{
for (var f = 0; f < DevicePath.Count; f++)
{
bw[f] = new BackgroundWorker();
bw[f].WorkerReportsProgress = true;
bw[f].WorkerSupportsCancellation = true;
switch (f)
{
case 0:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork0);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted0);
break;
}
case 1:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork1);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted1);
break;
}
case 2:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork2);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted2);
break;
}
case 3:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork3);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted3);
break;
}
}
}
bw[0].ProgressChanged += (sender, e) => { progressBar[0].Value = e.ProgressPercentage; };
bw[1].ProgressChanged += (sender, e) => { progressBar[1].Value = e.ProgressPercentage; };
bw[2].ProgressChanged += (sender, e) => { progressBar[2].Value = e.ProgressPercentage; };
bw[3].ProgressChanged += (sender, e) => { progressBar[3].Value = e.ProgressPercentage; };
}
private void BackgroundWorkerFilesDoWork0(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
ProgressBar pb = new ProgressBar();
pb.Maximum = 100;
worker.ReportProgress(0);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int status = tag_device0.Write_Tag(0, writeDat, en_statusDat, pb);
worker.ReportProgress(pb.Value);
if (ReadTag)
{
worker.ReportProgress(0);
byte[] tagData = new byte[1024];
tag5_device0.Read_Tag(0, 0, tagData .Length, ref tagData , pb);
worker.ReportProgress(pb.Value);
}
sw.Stop();
string result = string.Format("Status: {0} Elapsed: {1}", status, sw.Elapsed.ToString());
e.Result = result;
}
|
|
|
|
|
Blubbo wrote: Any suggesion? It seems to do an update only twice?
int status = tag_device0.Write_Tag(0, writeDat, en_statusDat, pb);
worker.ReportProgress(pb.Value);
if (ReadTag)
{
worker.ReportProgress(0);
byte[] tagData = new byte[1024];
tag5_device0.Read_Tag(0, 0, tagData .Length, ref tagData , pb);
worker.ReportProgress(pb.Value);
}
FWIW; the more often you report progress, the longer it'll take to complete the task.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
(This looks like Windows Forms vs. WPF.)
In the ...DoWorkn() method you create a local ProgressBar that is passed to the Write_Tag and Read_Tag methods.
There's not a tight linkage between that ProgressBar and the one that ReportProgress() is updating, so the one that is actually visible is not updated very often.
If the Write_Tag and Read_Tag methods aren't careful to deal with possible cross-thread updating of the ProgressBar passed to them, then you're kind of out-of-luck. It doesn't appear that there's an event on the ProgressBar that could be used to redirect the updates to the .Value to use the worker.ReportProgress(..) .
If the Write_Tag and Read_Tag methods are dealing with the cross-thread updating correctly, then you can pass the visible ProgressBar into the BackgroundWorker with the argument to the RunWorkerAsync(progressBar[n]) and use the ReportProgress() when updating directly in the worker, and pass it into the Write_Tag and Read_Tag methods, counting on them to "do the right thing" when they update it. Something like:
private void InitializeProgressBars()
{
for (int i = 0; i < DevicePath.Count; i++)
{
progressBar[i] = new ProgressBar();
progressBar[i].Location = new Point(3, 16);
progressBar[i].Size = new Size(605, 31);
progressBar[i].Maximum = 100;
}
gbProgressStatus0.Controls.Add(progressBar[0]);
gbProgressStatus1.Controls.Add(progressBar[1]);
gbProgressStatus2.Controls.Add(progressBar[2]);
gbProgressStatus3.Controls.Add(progressBar[3]);
}
private void InitializeBackgroundWorkers()
{
for (var f = 0; f < DevicePath.Count; f++)
{
bw[f] = new BackgroundWorker();
bw[f].WorkerReportsProgress = true;
bw[f].WorkerSupportsCancellation = true;
switch (f)
{
case 0:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork0);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted0);
break;
}
case 1:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork1);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted1);
break;
}
case 2:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork2);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted2);
break;
}
case 3:
{
bw[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork3);
bw[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted3);
break;
}
}
bw[f].ProgressChanged += (sender, e) => { ((ProgressBar)e.Argument).Value = e.ProgressPercentage; };
}
}
private void BackgroundWorkerFilesDoWork0(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
worker.ReportProgress(0);
ProgressBar pb = (ProgressBar)e.Argument;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
int status = tag_device0.Write_Tag(0, writeDat, en_statusDat, pb);
if (ReadTag)
{
worker.ReportProgress(0);
byte[] tagData = new byte[1024];
tag5_device0.Read_Tag(0, 0, tagData .Length, ref tagData , pb);
}
sw.Stop();
string result = string.Format("Status: {0} Elapsed: {1}", status, sw.Elapsed.ToString());
e.Result = result;
}
bw[n].RunWorkerAsync(progressBar[n]);
Another design option, if you can change the Write_Tag and Read_Tag methods, would be to have them take an Action<int> delegate that deals with updating progress, instead of the ProgressBar . In this case, it would call worker.ReportProgress() .
|
|
|
|
|
I have came up with a solution before I saw this reply with Action<int>. I placed a new method in the class (same class with Read_Tag method in it). Works gracefully!
void UpdateProgressBar(ProgressBar pb, int value)
{
if (pb.InvokeRequired)
{
Action<ProgressBar, int> action = new Action<ProgressBar, int>(UpdateProgressBar);
object[] obj = new object[] { pb, value };
pb.BeginInvoke(action, obj);
}
else
pb.Value = value;
}
public int tag5_device0.Read_Tag(<parameters>, ProgressBar pb)
{
for (int i = 0; i < 44; i++)
{
UpdateProgressBar(pb, i);
}
|
|
|
|
|
I think you should just create the delegate once as it doesn't change. And the BeginInvoke 's second argument is params so it will assemble the object array automatically, so this might be easier to read:
private static Action<ProgressBar, int> UpdateProgressBarAction = UpdateProgressBar;
private static void UpdateProgressBar(ProgressBar pb, int value)
{
if (pb.InvokeRequired)
{
pb.BeginInvoke(UpdateProgressBarAction, pb, value);
}
else
pb.Value = value;
}
You could even use a lambda to define this self-referentially to avoid the separate UpdateProgressBarAction :
private static Action<ProgressBar, int> UpdateProgressBar = (ProgressBar pb, int value) => {
if (pb.InvokeRequired)
{
pb.BeginInvoke(UpdateProgressBar, pb, value);
}
else
pb.Value = value;
};
|
|
|
|
|
Thanks for the suggestion.
I'm still working on using the delegation of the code. I have never used it in my prior programs that I have created. Still learning new stuff.
Thanks again!
|
|
|
|
|
How To Calculate The Total Data Download in c#
|
|
|
|
|
Use a timer.
Use the best guess
|
|
|
|
|
|
Between us we could come up with some numbers.
Use the best guess
|
|
|
|
|
You need to create a small program in C# for calculating total downloaded data ?
|
|
|
|
|
Hello,
Im doing a program with Visual Studio in C #. I need to get information from another device by the Ethernet port, read that information, and if correct, run the program. Any idea how I could do or how to manage the Ethernet port?
Thank you.
|
|
|
|
|
The first thing to do is to check the manual for the device to see what protocol it uses to communicate. Assuming it is a network device then you can probably use network sockets, for which Google will find you lots of sample code.
Use the best guess
|
|
|
|
|
Hi! I made a library-project in C#.
I save the customers in one list and the books in one.
When I register a that a Customer borrows a book this is what happends: (Sorry for my english, I am Swedish)
First the customer (lender) is assigned to the book, so the book "knows" who has borrowed it.
bookToLend.BiblioteksKund = lender;
Then the book is added to a List-control owned by the customer-object so the customer-object remembers what books the customer has borrowed.
This works fine when the program is running. The problem occurs when I try to save the objects to disc and then retrieve them again when the program starts. I have asked for help and googled it but I can´t make it work.
The problem seems to be the cross-reference of objects.
I´ll be happy to send the project if someone wants to look at it.
I have tried to use Serialization but have not succeeded.
Perhaps Linq would be a better choise.
|
|
|
|
|
larsp777 wrote: Then the book is added to a List-control owned by the customer-object so the customer-object remembers what books the customer has borrowed.
This works fine when the program is running. The problem occurs when I try to save the objects to disc and then retrieve them again when the program starts. I have asked for help and googled it but I can´t make it work. Post that part of the code (in PRE tags), we'll have a look at it
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
List<Bok> BookList = new List<Bok>();
List<Kund> CustomerList = new List<Kund>();
static string dir = @"C:\Bibliotek\";
string path = dir + "Kunder.xml";
string path2 = dir + "Books.xml";
XmlSerializer xs = new XmlSerializer(typeof(List<Kund>));
XmlSerializer xs2 = new XmlSerializer(typeof(List<Bok>));
public void sparaKund()
{
using (Stream s = File.Create(path))
{
xs.Serialize(s,CustomerList);
}
using (Stream s2 = File.Create(path2))
{
xs2.Serialize(s2, BookList);
}
}
The file is created but is empty.
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(path))
{
using (Stream s = File.OpenRead(path))
{
CustomerList = (List<Kund>)xs.Deserialize(s);
}
}
if (File.Exists(path2))
{
using (Stream s2 = File.OpenRead(path2))
{
BookList = (List<Bok>)xs2.Deserialize(s2);
}
}
}
|
|
|
|
|
According to your code snippet, you save freshly generated lists - which are empty. So the files are expected to contain the XML header only. Or did you just not show us the code where you fill the objects into the lists?
When you run your program in Debug mode (e.g. from Visual Studio, or a debug build), you can also use an Assertion before saving the data:
System.Diagnostics.Debug.Assert(CustomerList.Count > 0);
|
|
|
|
|
Well I didn't show the code where I fill the lists, but I do fill them.
I kind of works saving but not reloding.
Here is the XML-file:
<arrayofkund xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
|
|
|
|
Looks good enough for me. Can you now post the definition of the Kund and Book-classes?
I hope that each book has a unique number, or GUID, and I hope to see a list of those numbers in the Kund-object that borrowed them. My guess is that you're saving the lists without the references.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Well, you set the unique number when you register a book (Bok) or a customer (kund).
It's not a Commercial application.
Book:
[Serializable()]
public class Bok : ISerializable
{
protected int isbn = 0;
protected string titel = null;
protected string author;
protected int price;
protected String isType = null;
protected Kund biblioteksKund = null;
public int ISBN
{
get
{
return isbn;
}
set
{
isbn = value;
}
}
public string Titel
{
get
{
return titel;
}
set
{
titel = value;
}
}
public string Author
{
get
{
return author;
}
set
{
author = value;
}
}
public int Price
{
get
{
return price;
}
set
{
price = value;
}
}
public String IsType
{
get
{
return isType;
}
set
{
isType = value;
}
}
public Kund BiblioteksKund
{
get
{
return biblioteksKund;
}
set
{
biblioteksKund = value;
}
}
public Bok()
{
}
public Bok(int isbn, string titel, string author, int price, SerializationInfo info, StreamingContext ctxt)
{
ISBN = isbn;
Author = author;
Titel = titel;
Price = price;
biblioteksKund = new Kund();
this.isbn = (int)info.GetValue("ISBN", typeof(int));
this.author = (string)info.GetValue("Author", typeof(string));
this.titel = (string)info.GetValue("Titel", typeof(string));
this.price = (int)info.GetValue("Price", typeof(int));
this.biblioteksKund = (Kund)info.GetValue("Kund", typeof(Kund));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("ISBN", this.isbn);
info.AddValue("Author", this.author);
info.AddValue("Titel", this.titel);
info.AddValue("Price", this.price);
info.AddValue("BiblioteksKund", this.biblioteksKund);
}
public virtual void skrivUt()
{
Console.WriteLine("Författare: {0}", Author);
Console.WriteLine("Titel: {0}", Titel);
Console.WriteLine("Pris: {0}", Price);
}
public virtual String getInfo()
{
return null;
}
public String getStatus()
{
if (BiblioteksKund != null)
return ("Utlånad till: " + BiblioteksKund.Name);
else
return ("Ej utlånad");
}
}
}
Customer:
[Serializable()]
public class Kund : ISerializable
{
private int personNr = 0;
private string name;
private List<Bok> loan = new List<Bok>();
public int PersonNr
{
get
{
return personNr;
}
set
{
personNr = value;
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public List<Bok> Loan
{
get
{
return loan;
}
}
public Kund()
{
}
public Kund(int personNr, string name)
{
PersonNr = personNr;
Name = name;
}
public Kund(SerializationInfo info, StreamingContext ctxt)
{
this.personNr = (int)info.GetValue("PersonNr", typeof(int));
this.name = (string)info.GetValue("Name", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("PersonNr", this.personNr);
info.AddValue("Name", this.name);
}
public virtual void skrivUt()
{
Console.WriteLine("Personnr: {0}", PersonNr);
Console.WriteLine("Namn: {0}", Name);
}
}
|
|
|
|
|
larsp777 wrote: It's not a Commercial application. That makes no difference.
larsp777 wrote: Well, you set the unique number when you register a book (Bok) or a customer (kund). What does 'skrivUt' mean?
There's no list on "who" borrowed "wich" book. Remember that a ISBN-number doesn't idenitfy a specific book, but a "title". One could have multiple copies of the same title
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|