|
The function that you give in the Thread constructor takes parameters, therefore ThreadStart is wanting those parameters. Looking at the documentation (HINT HINT HINT), you will see two variants of Thread: one with parameters in the target function and one without parameters. You want parameters, but the documentation says you can only pass ONE parameter. You will need a version of your CopyDirectory that takes one object containing your two parameters -- a class or struct. Something like this (my C# syntax may be off slightly since I'm not compiling this):
class CopyParameters
{
public string source;
public string target;
}
public void CopyDirectoryThreaded (Object obj)
{
string source = ((CopyParameters) obj).source;
string target = ((CopyParameters) obj).target;
CopyDirectory (source, target);
}
and then your calling code looks like
CopyParameters params = new CopyParameters ();
params.source = source.ToString ();
params.target = target.ToString ();
var bkpthread = new Thread(CopyDirectoryThreaded)
{
IsBackground = true
};
bkpthread.Start(params);
Note that you have no way of detecting when your threaded copy is done, nor any protection to prevent two threads from messing with the same directories. You should have both.
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
Use a delegate in the constructor
var bkpthread = new Thread(delegate() { CopyDirectory(source.ToString(), target.ToString()); })
{
IsBackground = true
};
|
|
|
|
|
Perfect. Thank you so much.
|
|
|
|
|
In my SQL database I have 4 tables with the exact same structure. I want to query them using a generic method.. I've trieed multiple approaches, but I can'te get it right. Here's what I have so far:
private string GetVendorInitials(string tableName, int jobId)
{
using (var db = GetDataContext())
{
var result = string.Empty;
var vendorNames = (from jhv in db.GetTable()
join c in db.Companies on jhv.VendorId equals c.Id
where jhv.JobId == jobId
select c.CompanyName).ToList();
foreach (var vendorName in vendorNames)
{
var vendorInits = new string(vendorName.Split(' ').Select(x => x[0]).ToArray());
result = result + vendorInits + ",";
}
result = result.Trim();
result = result.Substring(0, result.Length - 1);
return result;
}
}
Anyone know how to do this?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Can you pass the entity type instead of the table name?
You'll also need to have the common entities implement an interface.
public interface IVendorJob
{
int VendorId { get; }
int JobId { get; }
}
...
private string GetVendorInitials<TEntity>(int jobId) where TEntity : IVendorJob
{
using (var db = GetDataContext())
{
var vendorNames = from jhv in db.Set<TEntity>()
join c in db.Companies on jhv.VendorId equals c.Id
where jhv.JobId == jobId
select c.CompanyName;
var vendorInitials = vendorNames.AsEnumerable()
.Select(n => n.Split(' ').Select(x => x[0]).ToArray())
.Select(a => new string(a));
return string.Join(",", vendorInitials);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi,
I want to read the version information of an exe on FTP. I could not find a resource anywhere. Can someone with knowledge of the subject help me?
|
|
|
|
|
I think you would most likely need to download the file first.
|
|
|
|
|
Thank you for the answer. But I will use the file I want to check as an update file. The file size is up to 150-200Mb. At every update check, downloading the file over and over again poses a problem.
|
|
|
|
|
Then you need to find some other way. FTP allows only certain commands from the remote caller.
|
|
|
|
|
Put the version number in a text file in the same FTP directory, and update it every time you update the exe file.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
FTP is a file transfer protocol: it doesn't care about the content of the files it transfers other than to ensure that the copy is binary identical to the original.
It contains no commands to read a version number from an EXE file as it doesn't know that any file contains a version number, much less where it is.
if you must use FTP, then I'd suggest adding a separate versioning file which contains the version of all files in the folder: download that and you can decide what does and doesn't need updates.
Personally I wouldn't use FTP for updating - you generally need a "more intelligent" solution, although that may use FTP as a transport mechanism. Most update checks are performed by the app checking back to "the mothership", not the other way around - so it already knows what version it is, and communicates that to the remote server for checking.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thank you for your answer. What kind of update system can I use besides FTP? Is there anything you can offer me?
|
|
|
|
|
|
he application that I'm creating has a menu comprised of labels/headers. When you click on a menu item (a xaml Label) a drop down appears with subheaders related to that item. However, I need to make this menu customizable via XML. The idea is this, the XML outlines the header names as well as the subheader names for each menu item.
<MenuHeaders>
<Header Value="Something">
<NestedHeader1>NestedHeader1</NestedHeader1>
<NestedHeader2>NestedHeader2</NestedHeader2>
</Header>
<Header Value="Something Else">
<NestedHeader1>NestedHeader1</NestedHeader1>
<NestedHeader2>NestedHeader2</NestedHeader2>
</Header>
</MenuHeaders>
I am able to read the XML using XDocument and bind a Dependency Property to the label context. The problem is that I do not know how many menu items might be in the XML file. I wanted to avoid creating a ton of labels and dependency properties for the label content to bind to. Instead of creating labels in xaml and binding to a property in C#, can I create UI items in C# and populate them with elements from a list at run time? There's some code below of how I'm getting and binding the data to a preexisting label.
C#:
<pre>
public string HeaderName
{
get => (string)GetValue(HeaderNameProperty);
set => SetValue(HeaderNameProperty, value);
}
public static readonly DependencyProperty HeaderNameProperty =
DependencyProperty.Register("HeaderName", typeof(string), typeof(MainWindow));
private void LoadHeaders()
{
var msg = new List<string>(0);
XDocument doc = null;
try
{
doc = XDocument.Load("MenuHeaders.xml");
}
catch (Exception e)
{
msg.Add(string.Format("Unable to open file:{0}", ""));
msg.Add(e.Message);
}
if(doc != null)
{
var allHeaders = doc.Descendants("Header").ToList();
List<string> headers = new List<string>();
foreach(XNode node in allHeaders)
{
XElement element = node as XElement;
foreach(XAttribute attribute in element.Attributes())
{
headers.Add(attribute.Value);
}
}
HeaderName = headers[0];
}
}
XAML:
<stackpanel x:name="Stack">
(As you may have noticed, I'm only adding the Header attributes to the List and none of the subheaders. This code is not complete but it's what I currently have) I will appreciate any help.
|
|
|
|
|
You need to put a secondary loop into the Header loop to get the children of the header.
How do you intend to bind the actions to the menu item?
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
The XMLDataProvider might be a simpler option:
How to: Bind to XML Data Using an XMLDataProvider and XPath Queries - WPF | Microsoft Docs[^]
If you want to stick with code, you're going to need a class to represent the menu item. For example:
public class MenuHeaderItem
{
public string HeaderName { get; set; }
public IReadOnlyList<MenuHeaderItem> Children { get; set; }
}
public static IReadOnlyList<MenuHeaderItem> LoadMenu(XDocument document)
{
var result = new List<MenuHeaderItem>();
var allHeaders = document.Descendants("Header");
foreach (XElement node in allHeaders)
{
result.Add(new MenuHeaderItem
{
HeaderName = (string)node.Attribute("Value"),
Children = node.Elements().Select(el => new MenuHeaderItem
{
HeaderName = (string)el,
}).ToList(),
});
}
return result;
} Bind the loaded values to a read-only dependency property:
private static readonly DependencyPropertyKey MenuHeadersKey = DependencyProperty.RegisterReadOnly(
"MenuHeaders",
typeof(IReadOnlyList<MenuHeaderItem>),
typeof(YourContainingClass),
new PropertyMetadata(null)
);
public static readonly DependencyProperty MenuHeadersProperty = MenuHeadersKey.DependencyProperty;
public IReadOnlyList<MenuHeaderItem> MenuHeaders
{
get { return (IReadOnlyList<MenuHeaderItem>)GetValue(MenuHeadersProperty); }
private set { SetValue(MenuHeadersKey, value); }
}
private void LoadHeaders()
{
if (File.Exists("MenuHeaders.xml"))
{
XDocument document = XDocument.Load("MenuHeaders.xml");
MenuHeaders = LoadMenu(document);
}
} Then bind the MenuHeaders collection to your menu.
<Menu ItemsSource="{Binding Path=MenuHeaders}">
<Menu.ItemContainerStyle>
<Style TargetType="{x:Type MenuItem}">
<Setter Property="Header" Value="{Binding Path=HeaderName}" />
<Setter Property="ItemsSource" Value="{Binding Path=Children}" />
<Setter Property="Command" Value="{Binding Path=MenuClickCommand}" />
<Setter Property="CommandParameter" Value="{Binding Path=HeaderName}" />
</Style>
</Menu.ItemContainerStyle>
</Menu> (I've assumed that you have an ICommand instance on your view-model called MenuClickCommand which will receive the header name as its parameter and handle the menu click event.)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am creating an application using c# winform to create and mount ISO files like wincdemu. I am able to create the ISO file and mount it like a local drive. I want to mount the image with disc type CD-ROM/DVD-ROM. I am using AttachVirtualDisk() to mount the image.
Is there any API/library support to mount the image with disc type CD-ROM/CD-R/CD-RW/DVD-ROM/DVD-RAM etc. ?
I appreciate any advice.
|
|
|
|
|
|
How can I reverse strings in other processes in c#? Can this be done with SendMessage or WriteProcessMemory?
|
|
|
|
|
It would have to be WriteProcessMemory. You're going to need admin permissions to do that.
This is akin to writing a debugger. It's not so straight foreword to just read a string, what you THINK is a string anyway, and just write one back. There are a multitude of considerations involved, like PageProtection and race conditions.
No, I'm not going to tell you how to do this because it can be used for malicious purposes.
|
|
|
|
|
Hi guys trying to run a program in c# which needs to return the number of times the char occurs in string. the main is fine but the method name i created gives me an error which i don't know why.(in bold) your help will be appriciated here is my program:
static void Main(string[] args)
{
string n = "Banana";
int num=CountOccurrences(n,'a');
Console.WriteLine(num);
}
static int CountOccurrences(string text, char digit)
{
for (int i=0 ; i < text.Length; i++)
{
if (text[i] == digit)
{
i++;
}
return i;
}
|
|
|
|
|
Your CountOccurrences method has its return statement in the wrong place. Also you are incrementing the loop count in two places so your result will most likely be wrong. It should be:
static int CountOccurrences(string text, char digit)
int count = 0;
for (int i=0 ; i < text.Length; i++)
{
if (text[i] == digit)
{
count++;
}
}
return count;
}
|
|
|
|
|
We don't know what error you are getting, but I suspect it's not the method name that is causing the problem.
Check the error itself - it should give you a good clue as to what is going wrong.
I suspect that the error message is likely to be "not all code paths return a value" - and that means that the compiler can see a route through your code which doesn't execute a return value statement.
Try moving the return i; line outside your for loop, so it's the last instruction in the method!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You prefer a scope error?
|
|
|
|
|
That's his next problem!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|