|
I have worker threads writing to my GUI right now with the following code. My issue is that I'm getting MDA errors (ContextSwitchDeadlock and DisconnectedContext) during debugging and when I close the program, the child threads are still trying to write to controls that are nonexistent anymore, which generates errors as well.
I'm guessing I need to learn the proper way to use a delegate with multithreading/guis, so I was wondering if someone could provide some guidance and the proper/efficient way to communicate to the main form thread, from a child thread.
Thanks for reading!
this.Invoke((MethodInvoker)delegate()
{
lvwServicesOutput.Items[selectedIndex].SubItems[2].Text = "service not present";
});
this.BeginInvoke((MethodInvoker)delegate()
{
tbxServerName2.Enabled = false;
tbxServerName2.Text = "";
tbxServiceName.Enabled = false;
tbxServiceName.Text = "";
serviceNameList = true;
});
|
|
|
|
|
Using Invoke (synchronous) or BeginInvoke (asynchronous) to communicate with the UI thread is the correct approach. However, it is possible for a control to disappear while the background thread is processing, so you need to catch that exception (I couldn't find a way to guarantee that it can never happen).
|
|
|
|
|
Hi Bob,
I stumbled upon this link in googling and in one of the comments it was mentioned to set child thread to .IsBackground equaling true, so I did so (see brief code snip) and that seems to have eliminated the errors when closing my program. I'm glad the invoke/begininvoke are the correct way to do things, although I would still like to learn more about delegates and their proper usage.
My limited understanding of them now is that they are (at least in my project) an intermediary with the control on the main form, so that you can instruct the main thread/form thread to do something. It appears to have the ability to insert an item into the main form thread queue of what it is doing. Is that correct?
serviceMonitorThread = new Thread(new ThreadStart(serviceMonitor));
serviceMonitorThread.IsBackground = true;
serviceMonitorThread.Start();
Updating Your Form from Another Thread without Creating Delegates for Every Type of Update[^]
|
|
|
|
|
If you set IsBackground, the worker thread will be killed when the main form is closed. That will solve the invoke-on-disposed-control issue but it is sometimes not what you want.
Delegates are simply typed function pointers. They are typically used to hook to events, which are some syntactic niceness around multicast delegates (i.e. the code calls what looks like one function, and several handler functions are run). They don't have anything directly to do with multithreading. Examples of non-threaded delegate usage include such obvious things as Click handlers on buttons (and all other UI events).
However, the way the Framework exposes threading functionality is either through Thread or BackgroundWorker. Thread takes a delegate to specify the method that should be run, and BackgroundWorker has several events which you can hook delegates onto.
turbosupramk3 wrote: It appears to have the ability to insert an item into the main form thread queue of what it is doing. Is that correct?
This is a decent description of the purpose of the Invoke or BeginInvoke methods. They ask the relevant thread to run a method (specified by a delegate) when appropriate.
|
|
|
|
|
You'd usually set them to be "background-threads"; a normal thread would live on if the UI-thread exits, a background-thread[^] does not - hence, it will not write to a non-existing form
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
<--- Forever learning new things, even the obvious ones
|
|
|
|
|
Hello friends;
I am writing in a listview data from an array. It is listed by the checkbox for each data alone. I would like to add by selecting a different TextBox. It's ip address in the TextBox.
in the textbox:
111.99.1.100
in the listview :
/xxx.asp?name=EMMA&Surname=ALIYEVA&SNumber=00121214512&RDate=2012.07.04
/xxx.asp?name=ANNA&Surname=MARIA&SNumber=00121334512&RDate=2012.07.03
etc.
i want - for example :
I chose the first record.
111.99.1.100/xxx.asp?name=EMMA&Surname=ALIYEVA&SNumber=00121214512&RDate=2012.07.04
#listview #checkbox #post #send c#
p.s. I'm sorry for my english sucks.
|
|
|
|
|
You may have better luck if you posted this in the ASP. NET forum[^]. If you do so, please edit your post and mark it as [Moved] so you don't get flamed for crossposting.
/ravi
|
|
|
|
|
I'm working, I'm in visual studio C# # Windows Forms application. This study is not web application.
|
|
|
|
|
OK, then this is the correct forum. But I still don't understand your problem.
/ravi
|
|
|
|
|
Do you want to send data to another computer with a given IP-Adress?
Well for the take a look at socket-programming:
Sockets in C#[^]
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
If I understand the question.
1. Learn how TCP and sockets work.
2. Write code to send a message via a socket.
3. Write code to receive a message via a socket.
4. Create a message that contains the data above. Note that the message contains it. It is not just the data.
5. Put all of the above together to solve your problem.
Note that steps 1-4 have NOTHING to do with GUI elements of any kind. So your code for 2-4 must not have any text boxes, nor list views nor check boxes.
|
|
|
|
|
Suppose structured
public struct stNhanVien
{
public WhatType? Picture;
public string FullName;
public DateTime BirthDay;
}
Can save images to a structured text file (note: do not store the path of the image), to save the image of the structure of text files will have style? one example of this?
|
|
|
|
|
Yes/no. "Saving" objects (and structs) is done via a technique called serialization[^]. The buil-in classes can serialize to Xml, or binary - and you can plugin your own format, if required.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
A 'structured text file' usually means fixed record length. That's not good for storing variable length data, like images.
However what you've posted there is a non-fixed-length structure. So I'm not quite sure what you're trying to do. Dumping a list of that struct to file would be 'structured', but not a 'text file'.
The Image class[^] is serialisable, so if you're using serialisation to persist and restore data, then you can just put an Image (or an Image subtype, e.g. Bitmap) into your struct.
|
|
|
|
|
Suppose I have the structure
public struct st Staff
{
public Bitmap Picture;
public string FullName;
public DateTime BirthDay;
}
in my save button code
Path = @"C:\data\database.dat";
FileStream fs;
try
{
fs = new FileStream(path, FileMode.Create);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
return;
}
BinaryWriter bw = new BinaryWriter(fs);
Staff = new Staff();
Staff.Picture = (Bitmap)pictureBox1.Image;
Staff.FullName = txtFullName.Text;
Staff.BirthDay = DateTime.Parse(mskNgaysinh.Text);
for (int i = 0; i < list.Count; i++)
{
Staff = (Staff)list[i];
bw.Write(Staff.Picture);
bw.Write(Staff.FullName);
bw.Write(Staff.BirthDay.ToBinary());
}
bw.Flush();
bw.Close();
fs.Close();
I get an error at this line
bw.Write(Staff.Picture); // warning error: The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
I wonder in the text file can save the Bitmap type? or to switch to the new chain store? if it is correct on how?
modified 21-Nov-12 22:34pm.
|
|
|
|
|
|
Errrm what? There seems to be a missing question there. Is it hiding behind the smiley? Tell it to come out so we can all have a go.
|
|
|
|
|
Which is the better place to declare variable ?
1>Inside try block
2>Outside before try block
|
|
|
|
|
Sibasis jena123 wrote: 1>Inside try block
2>Outside before try block
Based on theory, I'd say that it needs to be within the block, to limit it's scope as much as possible.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
|
if we want that variable inside the catch or final block we have to declare out side the try block .
If we want it only in try block then variable should be declared inside try block .
|
|
|
|
|
asuming you mean
if we want that variable outside the catch or final block we have to declare out side the try block .
If we want it only in try block then variable should be declared inside try block .
yes
|
|
|
|
|
The answer is that it really depends. If your variable doesn't need to be seen in the catch or finally block, and doesn't have scope that extends beyond the try/catch/finally block, then scope it inside the try part. If its scope can't be limited then you have no choice other than to declare it outside.
|
|
|
|
|
It depends on the usage , if we need our variable needed in catch then declare it outside else declare it inside the block
HariHaran.
Enjoy coding
|
|
|
|