|
Hello,when i start thread to plot in chart,but form is lose control, looks like thread is not in use.
private void button1_Click(object sender, EventArgs e)
{
Thread th=new Thread(new ThreadStart(fun1));
th.Start();
}
void fun1()
{
base.Invoke((MethodInvoker)delegate{
Series series1 = new Series();
for (int i = 0; i < 300; i++)
{
series1.Points.Add(new DataPoint(i, i));
Thread.Sleep(1000);
}
chart1.Series.Add(series1);
});
}
|
|
|
|
|
Ohm the thread works. The problem is that the code you're Invoking is being executed on the UI thread, not the thread you launched, because, well, THAT'S WHAT INVOKE DOES! And then you put the UI thread to sleep for a second. Once the Sleep is done and the delegate is done running, THEN you UI thread can get back to processing the message pump, like responding to WM_PAINT messages to repaint your window if needed.
You cannot touch a UI control properties or methods from anything other than the UI thread. You can create a new Series object and populate it with data in the background thread and THEN make the assignment to the chart object in delegated code. You're doing everything in the delegate, on the UI thread.
|
|
|
|
|
Hello, I have simply tried to create a static field on a class that will work like a counter for the instances ,but where I am trying to print the counter value is always on 0;
class Program
{
public static int Counter;
public Program()
{
Counter++;
}
static void Main(string[] args)
{
Program b;
Program c;
Console.WriteLine("{0}", Counter);
}
}
what is the problem ?
B.Is there a way to create a static var that is not field of the class and will work inside the class constructor (same idea as counter for the instances) ?
Thanks !
|
|
|
|
|
If you're talking about how many instances of your application are running, that's not going to work. You have to count the number of processes that are running using the process name of your application and the Process class.
If you're talking about how many instances of a class you have inside a single instance of a running application, that code may work.
|
|
|
|
|
I am talking just about the instances of the class,as in the answer below thanks !
|
|
|
|
|
There is no problem - the code is doing exactly what you've told it to!
You increment the Counter variable each time you create a new instance of the Program class. However, you never create an instance of the Program class - you just declare two variables which could potentially hold an instance of that class in the future.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
The problem is that in this case, you aren't creating any instances of your class, just the variables to hold instances. If Program was a struct rather than a class, then
Program b;
Program c;
would create instances - but they still wouldn't increment your counter, because value type constructions aren't implicitly called.
Try this:
Program b = new Program();
Program c = new Program();
And your counter will be incremented as you expect because you are creating instances.
A variable isn't the same as an instance: think of a variable as a "parking space" - it can hold any Car object, but you can't drive the space down to the shops! The parking space is the variable, the instance is the actual Car it holds.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
OriginalGriff wrote: value type constructions aren't implicitly called.
Parameterless value-type constructors aren't allowed:
CS0568 Structs cannot contain explicit parameterless constructors
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
B.Is there a way to create a static var that is not field of the class and will work inside the class constructor (same idea, as counter for the instances) ?
|
|
|
|
|
class GlobalVariables
{
public static int Counter;
}
class App
{
public App()
{
GlobalVariables.Counter++;
}
}
static void Main(string[] args)
{
var a = new App();
var b = new App();
Console.WriteLine(GlobalVariables.Counter);
Console.ReadLine();
}
Needless to say that abusing it as a global variable is an anti-pattern. I don't know what you are trying to do, but this seems like the wrong way.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
No: you have to something to "hold" the value, a Field, or a Property.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
A Console Application is not meant to be instantiated more than once; you need to separate out what you want to create multiple instances of from the Console App "framework:"
namespace ConsoleApplication1
{
class Program
{
public static int Counter = 0;
static void Main(string[] args)
{
SomeClass s1 = new SomeClass();
SomeClass s2 = new SomeClass();
}
}
public class SomeClass
{
public int MyID { set; get; }
public SomeClass()
{
MyID = Program.Counter++;
}
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
hello every one,
I have a problem with my program,
I can select a image from one folder in open dialog but I can't move the image to be processed in another directory.
so pleas help me..
this is my program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace Dir
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TransferFile transferFile = new TransferFile();
private void button1_Click(object sender, EventArgs e)
{
string cmd = "/c" + textBox1.Text;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = cmd;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
richTextBox1.Text = process.StandardOutput.ReadToEnd();
}
OpenFileDialog ofd = new OpenFileDialog();
private void button2_Click(object sender, EventArgs e)
{
string Filename = @"E:\MyDir";
string result;
result = System.IO.Path.GetFileName(ofd.FileName);
Console.WriteLine("GetFileName('{0}') returns '{1}'", Filename, result);
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox1.Text = " RGBImage.exe " + System.IO.Path.GetFileName(ofd.FileName);
}
}
|
|
|
|
|
Make it easy by using what .NET provides:
using System.IO;
string f1 = @"C:\test1";
string f2 = @"C:\test2";
string fname = "someJPG.jpg";
private void MoveFile(string folder1, string folder2, string filename)
{
string filePath1 = Path.Combine(folder1, filename);
if ((!File.Exists(filePath1)) || (! Directory.Exists(folder2)))
{
throw new ArgumentException("invalid file or folder path");
}
try
{
File.Move(filePath1, Path.Combine(folder2, filename));
}
catch (Exception ex)
{
Console.WriteLine("{0} error moving file", ex.Message);
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
hello,i have question as follows.
for example, I want to call fun1, then introduce vary x, and function as vary(fun2 or fun3), fun2 and fun3 have different varies. In fun1, can call fun2 or fun3 according to reference.
I think delegate can solve it ,but fun2 and fun3 have different number input, how to do it?
delegate void call() //?
main()
{
int x,y,z;
fun1(x,call)
}
fun1()
{
fun2(y) or fun3(y,z)?
}
fun2(int y);
fun3(int y,int z);
|
|
|
|
|
This code makes no sense: you call method 'fun1 with two arguments, but method 'fun1 takes no parameters. All your methods, and the delegate, are defined outside the scope of the Main method: what is the enclosing Class ?
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 30-Nov-15 4:45am.
|
|
|
|
|
class1()
{
main()
{
int x,y,z;
fun1(int x, fun2)
}
fun2(int y);
fun3(int y,int z);
}
class2()
{
static fun1(int x, fun ?)
{
fun2() or fun3()
}
}
|
|
|
|
|
Please, show us the code you really have - don't try reinterpreting your code as it makes it impossible to understand.
This space for rent
|
|
|
|
|
Your second code example also make no sense, and will never compile.
I believe you are very "lost" in C#: now, there's nothing wrong with that; everyone here was once a "beginner," and went through some degree of confusion.
Here's how to get "un-lost:"
Get the Visual Studio Edition, get the free book by Charles Petzold, "Dot Net Zero" [^].
Start at the beginning of that book and study the basics of C# carefully, start programming the examples in that book.
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
i am sorry,indeed i am a beginner.
I don't know how to meet my requirement. My requirement as follows:
I want to code an optimization algrithm function to calculate Minimized value. It can input function that needed optimization, like fun2 or fun3 ,etc... that i said. It means if i input fun2 to optimization function ,it can search fun2's Minimized value, if input fun3, it can search fun3's Minimized value.But fun2 and fun3 or other functions don't have same style input variable,their input variable style and number is not same.
how can i realize it?
|
|
|
|
|
Are you looking for something like this:
static void Main()
{
int x = 1;
int y = 2;
int z = 3;
fun1(x, () => fun2(y));
fun1(x, () => fun3(y, z));
}
static void fun1(int x, Action call)
{
call();
}
static void fun2(int y) { ... }
static void fun3(int y, int z) { ... }
Lambda Expressions (C# Programming Guide)[^]
C# in Depth : The Beauty of Closures[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you very much,it 's near my requirement.
But one more requirement is that:
static void fun1(int x, Action call)
{
call();
}
take fun3(y,z) for example, call() is run fun3(y,z).
but if i want to run fun3(x,z) ,how to realize it? you know ,i want to change y value in fun1 to call fun2(int y) or fun3(int y,int z),because my code use loop to change y value in fun1,to let fun2 or fun3 be a minimum value.
|
|
|
|
|
So something like this:
static void Main()
{
int x = 1;
int z = 3;
fun1(x, y => fun2(y));
fun1(x, y => fun3(y, z));
}
static void fun1(int x, Action<int> call)
{
call(x);
}
static void fun2(int y) { ... }
static void fun3(int y, int z) { ... }
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Great Job!!! Thank you very much.
And i have other question bothering me.
I have to use chart control to plot 10 series including 1000 points per series in one chartarea. But after ploting, mouse move or zoom in or ContextMenuStrip operation is very slow and delay. I think maybe paint problem.
When i ask before, i get the answer is point number is too big.
But i found that if i plot 200 series with 1 point for example as follows, the operation is also slow. How can treat it?
follows is for example:
Series series1;
for(int i=0;i<200;i++)
{
series1=new series();
series1.Points.Add(new DataPoint(i,i));
chart1.Series.Add(series1);
}
|
|
|
|
|