|
Hi, a lot of the types declared in the assembly I want to update at runtime are used both in this assembly and in other assemblies. And since a lot of people are working in the project at the same time it's not easy to make a bigger reorganization of the source files.
|
|
|
|
|
The traditional way to approach this would be to use Dependency Injection to inject the appropriate DLL at runtime. The best way to do this is to not hardcode your type references, but rather you would work against interfaces which the concrete classes would implement. There are many benefits to doing this such as creating more testable objects, improving the loose cohesion of the system, etc.
|
|
|
|
|
Hi,
I am currently writing a gui-application, which does some processing in a thread. I pass a reference to my mainwindow to the thread, in order to make the thread set some text inside the mainwindow. However, when I try to access any control in my mainwindow from my thread, I get:
"An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll"
Simple question: what is the best way to access mainwindo-gui elements from a thread?
This is what my code looks like:
public MainWindow()
{
InitializeComponent();
_ProcessHelperThread = new System.Threading.Thread(ProcessHelperThread);
_ProcessHelperThread.Start();
}
void ProcessHelperThread()
{
Processes.Init(this);
Processes.Startup();
}
Inside the thread (i.e. in my Processes-object), my app crashes when I try something like this:
public int Init(MainWindow window)
{
window.textBlock1.Text = Waittext;
}
What is a better way to do this?
|
|
|
|
|
I would have expected a cross thread exception rather than the one you got but the correct way to update gui elements from another thread is the call Invoke on the control. This is for WinForms not WPF.
public Form1()
{
InitializeComponent();
Thread workerThread = new Thread(WorkerThread);
workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}
private void UpdateWindowText(Form1 form)
{
form.Invoke((Action) (() => form.Text = "test"));
}
|
|
|
|
|
As a follow up this is how it is done in WPF.
public MainWindow()
{
InitializeComponent();
Thread workerThread = new Thread(WorkerThread);
workerThread.Start();
}
private void WorkerThread()
{
UpdateWindowText(this);
}
private void UpdateWindowText(Window window)
{
window.Dispatcher.Invoke((Action) (() => window.Title = "test"));
}
|
|
|
|
|
Thanks alot, this solution works perfectly!
|
|
|
|
|
hi,
How to add a blank row in the last index when click on the datagridview. When we type any character in any cell a new blank row is generated by default, likewise i need to generate in any click. AllowUsertoaddrow is set to true.
Thankyou
|
|
|
|
|
This will create a new row when the current new row gets clicked. I could see how this may be annoying for users though but I will assume you have your reasons.
public Form1()
{
InitializeComponent();
dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick);
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView dataGridView = sender as DataGridView;
if(dataGridView.Rows[e.RowIndex].IsNewRow)
{
int newRowIndex = dataGridView.Rows.Add();
dataGridView.CurrentCell = dataGridView.Rows[newRowIndex].Cells[e.ColumnIndex];
}
}
|
|
|
|
|
I have a Server, and several Client.
a meth receive data from client. then store these data in global variable temporary then deal.
e.g.
public void ReceiveMethod(object data)
{
//maybe Multiple client at the same time.
DealData(data);
}
public void DealData(object data)
{
//some values will store in global variable temporary.
//because Multiple client Send data,so the global variable's value is mistake. like mult-Thread without synchrony or lock.
//if this is Multiple Thread deal, I understand it.
//but I dont execute thread or asynchronization obvious.
//is Mult-Thread here?
}
|
|
|
|
|
|
Is the DealData Method a assistant Thread?
|
|
|
|
|
Dear All
What are the main reasons to freeze a multithreaded application? Is it due to thread asynchronous or thread dead lock? Could any one suggest me a good approach to debug multithreaded application?
Thanks
Regards
Alex
|
|
|
|
|
I see no reasons to freeze an app, multi-threaded or not. There may be causes why it is freezing nevertheless.
First thing to look for: are there one or more busy loops (is CPU load very high, say close to 100%, or maybe close to 50% on a dual-core, etc)? or is everything really idle (CPU load nearly zero)?
Debugging advice:
1. don't create a huge app with lots of problems and only then start fixing the problems; start small and make sure it is correct from the start.
2. improve observability by inserting logging statements, where each line shows DateTime.Now (up to ss.fff) and the thread ID.
|
|
|
|
|
|
Luc's advice is good. If you are still struggling, you could try taking a thread dump or memory dump when the application appears frozen and then you should be able to see what the different threads are doing.
There are a few tools that can help with this:
ADPlus[^]
Managed Stack Explorer[^]
Although Managed Stack Explorer is listed as a .NET 2 app, as far as I am aware it works with 3.0 and 3.5 also.
|
|
|
|
|
I forgot to mention one popular way to cause a random freeze: invalid cross-thread GUI operations (either in .NET 1.x, or by setting Control.CheckForIllegalCrossThreadCalls false); when in doubt, read this article[^].
|
|
|
|
|
I have a form with a lot of textboxes, etc... that are set to read-only or disabled. I have a "edit" button that allows the user to edit values on the form. I would like to add a "cancel" button that returns all of the controls back to their default state. I would like to know if there is a simple function that can be called similar to the initial load of the form. If not, I suppose I have to write a lot of code to change all of the controls back, one by one.
|
|
|
|
|
Two easy ways would be:
1. create a new Form
2. remove all top-level controls (this.Controls.Clear) and call InitComponent() again.
Both are more costly than is necessary.
Approach #3: You could enumerate all controls and reset them; in general this takes a recursive approach as TextBoxes could sit in Panels in SplitContainers, etc.
All these approaches have one thing in common: they are Form-agnostic, they don't really care exactly what Controls are present.
Approach #4 (More advanced): if your app is data-driven, chances are your Form represents one data object which has a class and probably also a DataRow, corresponding to a row in some DB table. If so, you are already using data binding, and could bind with a new (i.e. empty) object to clear everything.
FWIW a final remark: if your Form holds that many Controls that you see clearing them as a problem, then your users will probably not like it either for how it looks and how uneasy it is to use.
|
|
|
|
|
I have noticed that why anti virus, and other programs can gain administrator access when the app started when computer starts?
And why not .net created application? If I set to request administrator level in exe manifest, it will prompt user for permission, and it is very annoying since this application is automatically started at computer startup. How do I gain administrator permission (or at least the user login permission) without asking for password?
Why native exe can do it and why not .net exe?
Should we all move to C++ instead of supporting C#?
Any idea? or complains?
|
|
|
|
|
|
It is my understanding that apps like anti-virus programs appear to have admin access from their client tools because they talk to a windows service which is running with elevated privileges. The service is installed when you install the app (which you do get a UAC prompt for) after that point the client tools use IPC to communicate with the privileged service.
There may be (and most probably are) exceptions to this rule, I have not done much work in this area. So if your app really needs admin privileges (it probably shouldn’t) a workaround would be to install a windows service which performs the admin functions and communicate with that.
|
|
|
|
|
i see, my app is this one Writing a Folder Synchronization application[^]
which i think it should have admin privileges, since it is running in background doing synchronization works, which may include synchronizing folder in C or even program files
and recently i am having another project, which the application need to configure write its configuration files in its program files folder, but it does not have permission to do so, isn't this weird? for example, what if the application is extendable, where it can download new version or its plugin and put it in it's executable directory, shouldn't it have administrator privileges? Why microsoft need to limit the privileges of executable?
|
|
|
|
|
John Kenedy S.Kom wrote: Why microsoft need to limit the privileges of executable?
Any executable runs AS the user that launched it. There are no special restrictions on .EXE's of different types.
The "security" model you're assuming is not correct. When you install, say, an anti-virus engine, it installs a service that is actually doing all the work. It usually runs under the Local System account, which has pretty much admin priv's to the entire machine. Any UI program that you use to interact with this anti-virus app always runs as YOU. This app talks to the service and tells the service what it needs to do. The service runs as the local system, giving you the ILLUSION that what you ran has admin priv's.
John Kenedy S.Kom wrote: for example, what if the application is extendable, where it can download new version or its plugin and put it in it's executable directory
Writing to the Program Files fold is off-limits to normal users, but not admins. But, an .EXE can be installed to other locations that the user CAN write to. All it takes is for the updater to copy down the new .EXE to the correct location, but always the one you think it does. It's also possible that when the app was installed, the security for the apps folder under the Program Files folder was opened up to allow users to write to it.
Installers are written to run under administrator accounts, meaning they have free reign to configure the system, security, accounts, ... anything it needs to get the app installed and the rights it needs to do whatever it needs to.
|
|
|
|
|
I see, thanks Dave for the answer... Then I might consider to improve my app into a service instead and also learn about IPC, since these two are really new to me. Thanks!
Btw, what is the different between Local System account and the admin account? If a user is not an admin and installing the service, can the service then have the admin privileges, such as those own by anti virus?
|
|
|
|
|
John Kenedy S.Kom wrote: If a user is not an admin and installing the service,
If a user is not an admin, they're not installing the service. Also, even if they could, they cannot grant anything more rights than they themselves have. The only accounts that can grant admin permissions, are administrators.
The Local System is the account the, well, system uses. Even Windows has to provide credentials to Windows Security.
|
|
|
|