|
I have a backgroundworker on my form, I start it from the UI thread.
Then it would start a worker thread to do network-consuming task.
private void btnOK_Click(object sender, EventArgs e)
{
DisableForm();
Object obj = GetObjFromUI();
BackgroundWorker saveAsWorker = new BackgroundWorker();
saveAsWorker.DoWork += new DoWorkEventHandler(saveAsWorker_DoWork);
saveAsWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(saveAsWorker_RunWorkerCompleted);
saveAsWorker.RunWorkerAsync(obj);
}
After the task completed, it would return to the UI thread and modify related UI, in the Complete event, it worked very well before.
But now I found some times, the method saveAsWorker_RunWorkerCompleted will return to other thread, which is nor the UI thread, neither the worker thread which run the network-consuming task. So the cross-thread exception will be thrown, when the system try to modify related UI in the method saveAsWorker_RunWorkerCompleted.
I have to write code below, to avoid the cross-thread exception.
void saveAsWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (InvokeRequired)
{
Invoke(new MyEventHandler(MyObject_MyEvent), new object[] { sender, e });
}
else{
}
}
But I do not want to call the "if (InvokeRequired)", as before, this method "saveAsWorker_RunWorkerCompleted" was indeed excuted in the UI thread, and I am sure the thread which constructed the Backgroundworker and called RunWorkerAsync() was indeed UI thread. Why does the complete method return to "the 3rd thread" now?
Who knows why? Is any system configuration, for windows and .NET framework?
modified on Monday, August 24, 2009 9:48 PM
|
|
|
|
|
Hi,
You have saveAsWorker declared as a local variable making it eligible for garbage collection as soon as btnOK_OnClick returns. Without knowing the internals of the object it's not possible to predict what would happen after GC if the worker thread was still running.
Declaring saveAsWorker as a class level field will probably solve the problem.
Alan.
|
|
|
|
|
Please use PRE tags to show code, it is part of the forum guidelines. I'm not reading your post as is.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
Assuming the two snippets belong inside a single Form-derived class, your code looks fine to me. Here[^] is a little article I wrote as a summary on the cross-threading issue.
IMO the only way to get into trouble with the code shown would be by creating some new Control(s) in another thread which you shouldn't do ever (e.g. inside the DoWork handler), and then touching them again in the Completed handler.
You may want to add some log("Inside XXX thread ID = "+Thread.GetCurrentThread().ManagedThreadID); lines to the different pieces of your code to better see which thread is doing what.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi,
I am getting mscorlib:Authentication Failure error while trying to invoke my applicaiton. My code is below
private void InitializeRemoting(out int RemotingPortNumber)
{
// Establish server channel sink provider
BinaryServerFormatterSinkProvider svrSinkProvider = new BinaryServerFormatterSinkProvider();
svrSinkProvider.TypeFilterLevel = TypeFilterLevel.Full;
// Create and register tcp server channel to listen on a port.
m_serverChannel = new TcpServerChannel("ITViewAppOperations", 0,
svrSinkProvider);
ChannelServices.RegisterChannel(m_serverChannel);
string PortNumberString = m_serverChannel.GetChannelUri().Split(':')[2];
RemotingPortNumber = Convert.ToInt32(PortNumberString);
// Register "BridgeFactory.rem" as singleton well know service types.
WellKnownServiceTypeEntry entry1 = new WellKnownServiceTypeEntry(
typeof(IdeWMakerBridge).FullName,
typeof(IdeWMakerBridge).Assembly.FullName, // "Arch.IDE.IdeWmBridge"
"IdeWMakerBridge.rem",WellKnownObjectMode.Singleton);
RemotingConfiguration.RegisterWellKnownServiceType(entry1);
// Get remoting singleton bridge instance.
m_ideWmBridge = Arch.IDE.WMakerBridge.Common.IaaIdeWmBridge)Activator.GetObject(typeof(Arch.IDE.IdeWmBridge.IdeWMakerBridge),
"tcp://localhost:" + RemotingPortNumber + "/IdeWMakerBridge.rem");
SetLifetime(m_ideWmBridge, TimeSpan.FromDays(365*100));
m_ideWmBridge.IdeProxy = m_ideProxy;
m_ideWmBridge.Name = "TestNameABC: " + m_ideProxy.GetType().ToString();
}
I am getting exception at SetLifetime function.
private void SetLifetime(object obj, TimeSpan LeaseTime)
{
((ILease)((MarshalByRefObject)obj).GetLifetimeService()).Renew(LeaseTime);
}
Can I know why this exception is happening and what could be the fix for this.
|
|
|
|
|
Here is what I have:
A calculator program. The user saves his/her settings, then the program calculates a price based on the settings selected. The 28 settings save with My.Settings. I have the user's settings in a property grid, that currently, lists everything in alphabetical order. I would like to have 8 categories, each with a certain number of the settings, mainly for organization. But I don't know how to do that, or where I need to start looking. I would also like to have a description for each item, that shows up at the bottom of the property grid.
Please help me out here. I really don't know where I need to start with this. I have tried:
PropertyGrid1.ControlCollection...
...but that's it. I can't figure out where to go from there.
|
|
|
|
|
You don't manipulate the property grid. Tag each property in your class with a Description attribute. For example, something like this could work:
<Description("Property description goes here")> _
Public ReadOnly Property MyStringProperty As String
Get
Return "Hello, world!"
End Get
End Property
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Thanks for the reply!
I didn't have a class for this...but I do now:
Imports System.ComponentModel
-DefaultPropertyAttribute("ID")- _
Public Class Prices
Private _tiered As Decimal = CakePriceCalculator.My.Settings.TieredServing
-Category("Base Prices")- _
-DescriptionAttribute("A four-inch Tiered Cake costs this much per serving.")- _
Public Property Tiered() As Decimal
Get
Return _tiered
End Get
Set(ByVal value As Decimal)
_tiered = value
End Set
End Property
End Class
...and on the main form's Load Event:
Dim Price As New Prices
'Setting the PropertyGrid1.SelectObject to Price
Me.PropertyGrid1.SelectedObject = Price
(the "-" signs are supposed to be "less than" or "greater than", but the code isn't showing with them.)
Now that I have that, everything is appearing in categories, having a description, and pulling the values from My.Settings...but I cannot edit or save the values. It seems like they are pulling from the My.Settings for the calculations, but they do not send any changed values back to My.Settings, or save them. When I change the setting like before, then try to calculate the "new" value, it reverts back to the old value.
Any ideas for this?
Thanks!
|
|
|
|
|
I got it:
Imports System.ComponentModel
-DefaultPropertyAttribute("ID")- _
Public Class Prices
Private _tiered As Decimal = CakePriceCalculator.My.Settings.TieredServing
-Category("Base Prices")- _
-DescriptionAttribute("A four-inch Tiered Cake costs this much per serving.")- _
Public Property Tiered() As Decimal
Get
Return _tiered
End Get
Set(ByVal value As Decimal)
_tiered = value
CakePriceCalculator.My.Settings.TieredServing = value
End Set
End Property
End Class
(Where the "-" are, replace with opening less than, and closing greater than signs)
From my class, after my Set statement I had to update everything from there.
|
|
|
|
|
Hello, This is Vikash Gohil.
I have a problem in taking Back-up of Database.
I am using SQL 2000 MSDE version.
Actually the database I am trying to back-up is of around 350 MB size (.mdf file)
The Code works properly on normal size databases.
I am getting error of Time-Out while running Back-Up query on this Large Database.
Can anyone please suggest me on how to take Back-up of Large databases through Code.
Any help would be appreciated.
Thanks in Advance.
|
|
|
|
|
Hello.
In my windows application I have a subclassed Form (MyWin) that is opened as a top level window with myWin.Show(). MyWin contains a RichTextBox where the user is supposed to type in text. When myWin is closed by the user the text is saved to a file. I implement this by overriding OnClosing in MyWin and save the text from the RichTextBox there. This works fine when the user explicitly close myWin. But since myWin isn’t the main window in the application myWin can be closed implicitly when the application exits. When this happends the overridden OnClosing function will not be called, so the text will not be saved in this case.
I have tried to add a delegate to Application.ApplicationExit and Application.ThreadExit from MyWin to save the text from there. But that doesn’t work because when these delegates are called the RichTextBox has already been destroyed and its Text field is empty. I want MyWin to be autonomous so I don’t want to be dependent on some Save function that must be called when the main window closes.
So, what I need is someway to detect when the application is about to close, something like Application.BeforeExit but I cannot find anyway to do this. I thought that if I could retrieve the main window in the application,from MyWin, I could add a delegate to its Closing event, but I cannot find any way to do this. I have searched and found Application.Current.MainWindow in PresentationFramework, which I don’t know what it is, but when I try to use this the Application.Current is always null.
So, if anyone knows how to detect when the application is about to be closed before anything is actually closed I would appreciate some help.
Regards
Peter
|
|
|
|
|
Hi,
[EDIT}Use the FormClosing event, not the Closing event![/EDIT]
if the MainForm opened some OtherForm which may hold dirty data, then MainForm.OnFormClosing should perform an OtherForm.Close(), and of course OtherForm.OnFormClosing should deal with the dirty data.
BTW: If keeping track of which forms got opened is too difficult, you could rely on Application.OpenForms and try and close those Forms one by one.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
modified on Sunday, August 23, 2009 4:24 AM
|
|
|
|
|
See you can add new Class and a Method in that Class.
Then you can add a Handler for Application.Exit event which references the above Class method.
See below Code.
Class File Code:
Public Class ShutDown
Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As System.EventArgs)
Try
Catch ex As Exception
Finally
End Try
End Sub
End Class
Form Code That Loads When Application Starts:
Sub New()
InitialiseComponents
AddHandler Application.ApplicationExit, AddressOf ShutDown.OnShutdown
End Sub
The Above Code would Catch the Application.Exit event and run the OnShutDown Procedure.
You can write the Code that you want to Execute in the OnShutDown Method.
Hope This Helps.
|
|
|
|
|
I think you may be looking for a specific Windows message... You can override PreProcessMessage in your form and basically "hook" all manner of unusual things. I wish I knew which one you were looking for exactly or if it even exists, but whenever I've had to handle unusual user actions that didn't really exist as ordinary events, this has worked for me. It's just been a matter of finding the correct combination of msg.Msg , msg.HWnd , and msg.LParam that you are looking for in the overriden PreProcessMessage routine.
|
|
|
|
|
Override OnClosed in MyWin instead of OnClosing. The RichTextBox.Text property will still be valid. MyWin.OnClosed does get called when the main form is closed, whereas MyWin.OnClosing does not.
Eagles my fly, but weasels don't get sucked into jet engines.
|
|
|
|
|
The Closed or FormClosed events are too late to interact with the user.
Use the FormClosing event, not the Closing event!
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Hi Guys,
I have created an assembly containing Tasks for use with MSBuild. In order to get the build working across all development machines, i have placed it on a shared directory. The assmebly works fine locally, but when i try to call it from the network drive, it failes with:
System.Security.SecurityException: That assembly does not allow partially trusted callers.
I have added the following line to the assmebly info:
[assembly: AllowPartiallyTrustedCallers()]
I have also signed the assembly with a strong key.
The issue still persists. Any thoughts?
Regards
Tristan Rhodes
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
The workstations themselves haven't been told to trust the network location or trust assemblies signed with a certain key.
|
|
|
|
|
Ah, i see!
Thank you
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
|
Weeeelllll, what's stopping you from wrapping the Add in a Try/Catch block and handling the problem yourself instead of blindly assuming that evey piece of data your adding/handling is good?? The problem isn't with Microsoft, it's with you.
|
|
|
|
|
Weeeelllll, what's stopping MS from throwing better exceptions? I've got some sample code I can give them on how to do it...
I don't want to have exception handling here as I believe a) that a majority of exceptions should be handled in the presentation layer and b) in this case it was a multi threading problem where we check if a key exists and add it if it does not exist but a second thread would insert causing the first to fail, making the need for explicit exception handling 'unobvious'. After fixing the multi threading problem the exception is fixed and I still don't have exception handling over the add method, won't have either.
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Well, you didn't mention the multithreading issue at all in your first post, so in that case I would have wrapped the add in a lock so no two threads could check for and add data at the same time. In either case, you're still blindly trusting the data you're using and now you just admitted to blindly assuming that every operation you're doing on this data works in a multithreaded environment.
I'm just of the opinion that if you're going to bash someone's code, start with your own.
|
|
|
|
|
I didn't realise this was a code bashing session, until now. You are going into semantics of the example, like I said I do not want to handle errors here (I want them to roll up to the presentation layer), and the lock statement is not the best solution here either. And yes, I am trusting the data I put into this as it is very controllod but I'm not doing it blindly, and I'm not blindly assuming anything because I'm expoecting the exception to be caught in the presentation layer. Now that I have found the problem and fixed it it works perfectly.
Another example: I've de-serialised some data and got an "Argument out of Range" exception, no idea which attribute of the object was out of range and no idea what the acceptable range is, and being Silverlight 3 the MSDN help files pointed me at an article for WPF which is not correct for SL 3 (the article mentioned an int where it was actually an Int16), if the message was "Argument 'asd' is out of range, maximum value is '32767'" it probably would have shaved 3 hours off my debugging.
I'm of the opinion that if you are going to suggest solutions make sure you understand the problem.
____________________________________________________________
Be brave little warrior, be VERY brave
|
|
|
|
|
Adriaan Davel wrote: if the message was "Argument 'asd' is out of range, maximum value is '32767'" it probably would have shaved 3 hours off my debugging.
And if the object doesn't have a string representation?? Also, how is the exception going to know what the possible values are for a property that doesn't have "values"??
You want the exception to tailor itself to the property it's throw in and that's just not practical. The exception that MS provides works in the widest array of cases possible, where your exception message only works where a property is a value type.
Adriaan Davel wrote: I'm of the opinion that if you are going to suggest solutions make sure you understand the problem
I do understand the problem, and what it would take to get an exception to do what you want.
The point is, if you want a more meaningful message, wrap the property in it's own exception handler and throw a more meaningful message yourself, then catch that exception in your presentation layer.
|
|
|
|
|