This question already has answers here:
How do I update the GUI from another thread?
(47 answers)
Closed 6 years ago.
Simple question, I had an application in console mode. Every thing would write in the console using Console.WriteLine. However, now I have a form and a listbox. I know easily how to do it, however, what is the proper way to do it?
You can access the UI from another thread using delegate, simple and neat:
this.Invoke((MethodInvoker)delegate
{
textBox1.Value = "your text";
});
Related
This question already has answers here:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
(22 answers)
Closed 2 years ago.
I'm a beginner with C# so maybe I don't really understand why I have this error.
System.InvalidOperationException: Cross-thread operation not valid:
Control 'lstb_files_r' accessed from a thread other than the thread it
was created on.
This is what I'm doing:
I drop X csv.
I read the first one (lstb_files_r.SelectedIndex = i;)
after finishing I would like to read the 2nd (i++). But I have this error...
Can someone explain to me how can I solve that please ?
When I asked my question I continued to look all the links and I've found something (maybe it's not the best way but I will find an other solution)
I use delegate
lstb_files_r.Invoke((MethodInvoker)delegate
I put all the code inside and it works.
You are performing the cross threaded opertaion which means you are in one thread (which is your algorithm) and want to perform something in another thread (here GUI is another thread) which is not allowed. For that you have to call GUI thread and perform the function. In C# the easiest way is to use backgraoudworker Use of backgroudworker can see here how to use background worker.
In backgroundworker use progressreport to perform the GUI operations. If you need further help I can help you.
This question already has answers here:
best practices to call methods in the parent form and access gui elements in parent form in c#
(3 answers)
Implementing MVC with Windows Forms
(6 answers)
Closed 4 years ago.
I've got a form class (Form1) and another class (Calculator).
In Calculator I've got this method:
public int Add(int number1, int number2)
{
return number1 + number2;
}
On the form, when you click a button, the Add method should be invoked and the text of a label should be updated to the result.
What is the correct way of updating the label? I'm not asking for the easiest way (I know tons of them), I wanna know how this is done in big Windows Form projects.
I've seen lots of things how to do it (with Threads, Delegates, and the Invoke() methods, etc), but I'm not quite sure how to correctly do it myself.
Thanks in advance!
EDIT: Oh noo the downvoting :( Please help me I'm desperate
label1.Text = calculatorinstance.Add(x, y).ToString();
or
label1.Text = $"{calculatorinstance.Add(x, y)}";
Your talk of Threads, Delegates, Invoke is only relevant if you plan on using background or long running processes. You don't mention that you are doing these long running processes so the above should suffice.
This question already has answers here:
How can run functions of powercfg by C# code?
(3 answers)
Closed 9 years ago.
From my C# program I want to open the power options from the control panel in Windows.
However I can't seem to find out how to do that, I tried making a shortcut and see where it goes but it is referring to the control panel.
Does anyone have an idea how to do that or where the exe is located to launch?
Try the following:
System.Diagnostics.Process.Start("powercfg.cpl");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Prevent multiple instances of a given app in .NET?
How can I enforce a single instance of my application?
I have a source then complie to App.exe file. I want if App.exe is running, then a person open it again, it know that it is running and close automaticly. Is there any way to do this?
**Edit: I want a code that insert to source of App.exe
Window Form**
You want to use a Mutex, like so:
bool bIsSingleInstance;
using (new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bIsSingleInstance)) {
if (bIsSingleInstance) {
// START APP HERE.
}
}
Note that you must NOT use 'using' if your winforms isn't blocking in the comment block.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.